1. 程式人生 > >Android第三次作業

Android第三次作業

一、播放器截圖

當某一首歌播放的時候使用綠色為他的底部,同時我這個音樂播放器支援進度條拖拽快進。

 

 

二、播放器關鍵程式碼

設定繫結功能

public int getPlayPosition() {
        return playPosition;
    }

    public void setPlayPosition(int playPosition) {
        this.playPosition = playPosition;
    }

    public MyBinder(MusicService musicService) {
        this.musicService = musicService;
    }

    public List<String> getSimpleMusics() {
        return simpleMusics;
    }

    public void setSimpleMusics(List<String> simpleMusics) {
        this.simpleMusics = simpleMusics;
    }

    public void setMusicService(MusicService musicService) {
        this.musicService = musicService;
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    public MusicService getMusicService() {
        return musicService;
    }

    public List<String> getMusics() {
        return musics;
    }

    public void setMusics(List<String> musics) {
        this.musics = musics;
    }

    public int getCurMusicIndex() {
        return curMusicIndex;
    }

    public void setCurMusicIndex(int curMusicIndex) {
        this.curMusicIndex = curMusicIndex;
    }

    public int getPausePosition() {
        return pausePosition;
    }

    public void setPausePosition(int pausePosition) {
        this.pausePosition = pausePosition;
    }

    public int getTotalMusicTime() {
        return totalMusicTime;
    }

    public void setTotalMusicTime(int totalMusicTime) {
        this.totalMusicTime = totalMusicTime;
    }

  

播放功能實現

 1 public void play(){
 2         player.reset();
 3         try{
 4             player.setDataSource(musics.get(binder.getCurMusicIndex()));
 5             player.prepare();
 6             player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
 7                 @Override
8 public void onPrepared(MediaPlayer mediaPlayer) { 9 player.start(); 10 } 11 }); 12 player.seekTo(binder.getPausePosition()); 13 binder.setTotalMusicTime(player.getDuration()); 14 }catch (IOException e){
15 e.printStackTrace(); 16 } 17 }

暫停功能

 if (player.isPlaying()){
            player.pause();
            binder.setPause(true);
            binder.setPausePosition(player.getCurrentPosition());
        }

下一首功能

if (curMusicIndex >=musics.size()){
            binder.setCurMusicIndex(0);
            binder.setPausePosition(0);
            play();
        }
        else{
            binder.setCurMusicIndex(curMusicIndex);
            binder.setPausePosition(0);
            play();
        }

上一首功能

1 if (curMusicIndex<0) {
2             binder.setCurMusicIndex(musics.size() - 1);
3             binder.setPausePosition(0);
4             play();
5         }else{
6             binder.setCurMusicIndex(curMusicIndex);
7             binder.setPausePosition(0);
8             play();
9         }

實現進度條以及進度條快進

1 handler = new Handler(){
2             @Override
3             public void handleMessage(Message msg) {
4                 String playTime = msg.getData().getString("playTime");
5                 String totalTime = msg.getData().getString("totalTime");
6                 textCur.setText(playTime);
7                 textTotal.setText(totalTime);
8             }
9         }

實現音樂目錄的遍歷

 1 private void initLiseView(){
 2         String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/music/";
 3         File mp3dir = new File(path);
 4         if (mp3dir.isDirectory()){
 5             File[] files = mp3dir.listFiles();
 6             for(File f:files){
 7                 musics.add(f.getAbsolutePath());
 8                 simpleMusics.add(f.getName());
 9             }
10         }
11         binder.setMusics(musics);
12         binder.setSimpleMusics(simpleMusics);
13     }

三、GitHub以及apk檔案的網址

Github:https://github.com/JusperLee/player.git

GitHub_APK: https://github.com/JusperLee/player/blob/master/app/release/app-release.apk