基於Dragonboard410c的智能音箱(四)

底層的驅動代碼和相關的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);
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章