基于Dragonboard410c的智能音箱(四)

来源:互联网 发布:人工智能在制造业 编辑:程序博客网 时间:2024/04/30 00:30

底层的驱动代码和相关的DTS文件都已经配置好了,开始实现应用的代码了,我们先来实现音乐播放器的基础功能。

实现一个音乐播放器,可以直接扫描板子上的所有音乐并列出歌曲清单,同时可以控制进行音乐播放、停止、上一首、下一首。

public class MainActivity extends Activity implements View.OnClickListener {    private Button puase, stop, last, next;    private RadioButton start;    private ListView listView;    private TextView totle, current_time;    private SeekBar progressBar;    public final static int PLAY = 1;    public final static int STOP = 2;    public final static int NEXT = 3;    public final static int LAST = 4;    private ArrayList<String> save;    private PlayMusicBackgoundService service;    private ServiceConnection connection;    private boolean isstart = true;    private Timer timer;    private int onplay = 0;    private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            SimpleDateFormat format = new SimpleDateFormat("mm:ss");            progressBar.setMax(msg.arg1);            progressBar.setProgress(msg.arg2);            totle.setText(format.format(msg.arg1));            current_time.setText(format.format(msg.arg2));        }    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initvariable();        request();        timer = new Timer();        scanFile();    }    private void request() {        requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);    }    private void initvariable() {        start = findViewById(R.id.start);        last = findViewById(R.id.last);        next = findViewById(R.id.next);        listView = findViewById(R.id.music);        totle = findViewById(R.id.count_time);        current_time = findViewById(R.id.current_time);        progressBar = findViewById(R.id.advance);        start.setOnClickListener(this);        last.setOnClickListener(this);        next.setOnClickListener(this);        save = new ArrayList<>();        Intent intent = new Intent();        intent.setAction("android.intent.action.BUND");        intent.setPackage("com.thundersoft.boardapk");        intent.putExtra("all", save);        connection = new ServiceConnection() {            @Override            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {                service = (PlayMusicBackgoundService) ((PlayMusicBackgoundService.MyServices) iBinder).getServices();            }            @Override            public void onServiceDisconnected(ComponentName componentName) {            }        };        bindService(intent, connection, BIND_AUTO_CREATE);    }    private void scanFile() {        save.removeAll(save);        String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Song";        File file = new File(path);        for (File file1 : file.listFiles()) {            if (file1.getName().endsWith(".mp3")) {                save.add(file1.getName());            }        }        ListAdapter adapter = new ListAdapter(save, this);        listView.setAdapter(adapter);    }    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.start:                if (isstart) {                    start.setText("Pause");                    isstart = false;                }                break;            case R.id.last:                if (onplay == 0) {                    action(LAST, save.get(save.size() - 1));                    onplay=save.size() - 1;                } else {                    Log.i("LAST",onplay+"");                    action(LAST, save.get(onplay -1));                    onplay=onplay-1;                }                getdataback();                start.setText("Pause");                isstart=false;                break;            case R.id.next:                if (onplay == (save.size() - 1)) {                    action(NEXT, save.get(0));                    onplay=0;                    Log.i("onlpay",""+onplay);                } else {                    action(NEXT, save.get(onplay + 1));                     onplay=onplay+1;                }                getdataback();                start.setText("Pause");                isstart=false;                break;        }    }    private void getdataback() {        service.setCallback(new PlayMusicBackgoundService.Callback() {            @Override            public void get(int time, int current) {                Message message = new Message();                message.arg1 = time;                message.arg2 = current;                handler.sendMessage(message);                Log.i("FUCK", "PLAY");            }        });    }    public void action(int action, String path) {        Intent intent = new Intent(this, PlayMusicBackgoundService.class);        intent.putExtra("action", action);        intent.putExtra("path", path);        startService(intent);    }    @Override    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {        if (requestCode == 1) {            scanFile();        }    }    @Override    protected void onDestroy() {        super.onDestroy();        unbindService(connection);    }}

再来实现音乐播放器的后台播放功能

public class PlayMusicBackgoundService extends Service {    private MediaPlayer mediaPlayer;    private boolean isStop = true;    private Callback callback;    private Handler handler;    private int current, count;    public boolean ispause = false;    private Timer timer;    public ArrayList<String> list;    public void setCallback(Callback callback) {        this.callback = callback;    }    @Override    public IBinder onBind(Intent intent) {        list=intent.getStringArrayListExtra("all");        return new MyServices();    }    @Override    public void onCreate() {        super.onCreate();    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        if (intent != null) {            int action = intent.getIntExtra("action", 0);            switch (action) {                case MainActivity.PLAY:                    if (isStop) {                        if (!ispause) {                            mediaPlayer = MediaPlayer.create(this, Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/Song/" + intent.getStringExtra("path")));                        }                        Log.i("Media", mediaPlayer + "");                        timer = new Timer();                        timer.schedule(new TimerTask() {                            @Override                            public void run() {                                count = mediaPlayer.getDuration();                                current = mediaPlayer.getCurrentPosition();                                callback.get(count, current);                            }                        }, 0, 1000);                        mediaPlayer.start();                        isStop = false;                    } else if (!isStop && mediaPlayer.isPlaying() && mediaPlayer != null) {                        timer.cancel();                        mediaPlayer.pause();                        isStop = true;                        ispause = true;                    }                    break;                case MainActivity.STOP:                    if (!isStop) {                        mediaPlayer.stop();                        mediaPlayer.release();                        isStop = true;                    }                    break;                case MainActivity.NEXT:                    if (mediaPlayer!=null){                        mediaPlayer.reset();                        mediaPlayer.stop();                        mediaPlayer.release();                        mediaPlayer=null;                    }                    mediaPlayer=MediaPlayer.create(this,Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/Song/" + intent.getStringExtra("path")));                    Log.i("NEXT",intent.getStringExtra("path"));                    mediaPlayer.start();                    Timer timenext = new Timer();                        timenext.schedule(new TimerTask() {                            @Override                            public void run() {                                Log.i("MEDIA",mediaPlayer+"");                                count = mediaPlayer.getDuration();                                current = mediaPlayer.getCurrentPosition();                                callback.get(count, current);                            }                        }, 0, 1000);                    isStop=false;                    break;                case MainActivity.LAST:                    if (mediaPlayer!=null){                        mediaPlayer.reset();                        mediaPlayer.stop();                        mediaPlayer.release();                        mediaPlayer=null;                    }                    mediaPlayer=MediaPlayer.create(this,Uri.parse("file://" + Environment.getExternalStorageDirectory().getPath() + "/Song/" + intent.getStringExtra("path")));                    Log.i("LAST",intent.getStringExtra("path"));                    mediaPlayer.start();                    Timer timelast = new Timer();                        timelast.schedule(new TimerTask() {                            @Override                            public void run() {                                count = mediaPlayer.getDuration();                                current = mediaPlayer.getCurrentPosition();                                callback.get(count, current);                            }                        }, 0, 1000);                    isStop=false;                    break;            }        }        return super.onStartCommand(intent, flags, startId);    }    public class MyServices extends Binder {        public Service getServices() {            return PlayMusicBackgoundService.this;        }    }    public interface Callback {        public void get(int time, int current);    }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 性功能时间不长怎么办? 打坐时间不长怎么办 余利宝无法开通怎么办 qq群视频进不去怎么办 qq空间状态异常怎么办 手机qq空间异常怎么办 手机被恶意骚扰怎么办 织布机开不起来怎么办 座机键盘锁了怎么办 邮箱登录锁死怎么办 标志408油耗高怎么办 gucci白鞋掉皮怎么办 深圳摇不到车牌怎么办 车辆换牌照需要怎么办 商标申请有异议怎么办 唯品会账号被冻结怎么办 易直播回放收费怎么办 手机屏幕被摔了怎么办 xp调分辨率黑屏怎么办 公司logo被盗用怎么办 商标注册证掉了怎么办 商标注册证书丢了怎么办 市场监督管理局罚款怎么办 东莞居住证掉了怎么办 工商注销了税务怎么办 楼道自来水爆了怎么办 工商证没年检怎么办 工行信用卡被锁怎么办 外地卡密码锁了怎么办 营业执照年审过期了怎么办 工商营业执照吊销了怎么办 小规模企业工商年检怎么办 血流变检查偏高怎么办 信誉卡没有邮箱怎么办 税务年报没报怎么办 工商忘记年审了怎么办 营业执照脱审了怎么办 公司年审没有弄怎么办 车检标志丢了怎么办 机动车年检丢了怎么办 汽车保险标志丢了怎么办