1600509018
一、播放器截图
当某一首歌播放的时候使用绿色为他的底部,同时我这个音乐播放器支持进度条拖拽快进。
二、播放器关键代码
设置绑定功能
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
四、软件运行视频