首页 > 代码库 > Music播放器
Music播放器
1.开启服务实现销毁Activity后能实现继续播放
1 package text.baidumusic; 2 3 import android.app.Service; 4 import android.content.Intent; 5 import android.media.MediaPlayer; 6 import android.os.Binder; 7 import android.os.Bundle; 8 import android.os.IBinder; 9 import android.os.Message; 10 import android.support.annotation.Nullable; 11 12 import java.util.Timer; 13 import java.util.TimerTask; 14 15 /** 16 * Created by liuwei on 7/8/17. 17 */ 18 19 public class MusicService extends Service { 20 private MediaPlayer mediaPlayer; 21 private class myBinder extends Binder implements myMusicService{ 22 @Override 23 public void startPlay() { 24 playMusic(); 25 } 26 27 @Override 28 public void pausePlay() { 29 pauseMusic(); 30 } 31 32 @Override 33 public void stopPlay() { 34 stopMusic(); 35 } 36 @Override 37 public void repaly() { 38 replayMusic(); 39 } 40 41 @Override 42 public void seekTo(int position) { 43 seekTo(position); 44 } 45 } 46 @Nullable 47 @Override 48 public IBinder onBind(Intent intent) { 49 return new myBinder() ; 50 } 51 public void playMusic(){ 52 System.out.println("音乐播放"); 53 try { 54 mediaPlayer.setDataSource("/mnt/sdcard/1.mp3"); 55 mediaPlayer.prepare(); 56 mediaPlayer.start(); 57 updateSeekBar(); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 } 62 63 private void updateSeekBar() { 64 //获取当前播放总进度 65 final int duration=mediaPlayer.getDuration(); 66 //使用timer 67 Timer timer=new Timer(); 68 TimerTask task=new TimerTask() { 69 @Override 70 public void run() { 71 int currentPosition=mediaPlayer.getCurrentPosition(); 72 Message msg=Message.obtain(); 73 Bundle bundle=new Bundle(); 74 bundle.putInt("duration",duration); 75 bundle.putInt("currentPosition",currentPosition); 76 msg.setData(bundle); 77 MainActivity.handler.sendMessage(msg); 78 } 79 }; 80 timer.schedule(task,100,1000); 81 } 82 83 public void replayMusic(){ 84 mediaPlayer.start(); 85 } 86 public void pauseMusic(){ 87 System.out.println("音乐暂停"); 88 mediaPlayer.pause(); 89 } 90 public void stopMusic(){ 91 System.out.println("音乐停止"); 92 mediaPlayer.stop(); 93 onDestroy(); 94 onCreate(); 95 } 96 public void seekTo(int position){ 97 mediaPlayer.seekTo(position); 98 } 99 @Override 100 public void onCreate() { 101 System.out.println("音乐服务创建"); 102 mediaPlayer = new MediaPlayer(); 103 super.onCreate(); 104 } 105 106 @Override 107 public void onDestroy() { 108 System.out.println("音乐服务销毁"); 109 super.onDestroy(); 110 } 111 }
2.通过暴露接口的方法实现在活动中操作服务
1 public interface myMusicService { 2 public void startPlay(); 3 public void pausePlay(); 4 public void stopPlay(); 5 public void repaly(); 6 public void seekTo(int position); 7 }
3.对music音乐通过开启绑定服务的方式进行播放,暂停,继续,停止操作
1 package text.baidumusic; 2 3 import android.Manifest; 4 import android.content.ComponentName; 5 import android.content.Intent; 6 import android.content.ServiceConnection; 7 import android.content.pm.PackageManager; 8 import android.media.MediaPlayer; 9 import android.os.Handler; 10 import android.os.IBinder; 11 import android.os.Message; 12 import android.support.annotation.NonNull; 13 import android.support.v4.app.ActivityCompat; 14 import android.support.v4.content.ContextCompat; 15 import android.support.v7.app.AppCompatActivity; 16 import android.os.Bundle; 17 import android.view.View; 18 import android.widget.SeekBar; 19 import android.widget.Toast; 20 21 import java.io.File; 22 import java.io.IOException; 23 24 public class MainActivity extends AppCompatActivity { 25 private myMusicService iMusicService; 26 private MyConn conn; 27 private static SeekBar seekBar; 28 public static Handler handler=new Handler(){ 29 public void handleMessage(Message msg) { 30 //获取数据 31 Bundle bundle=msg.getData(); 32 int duration=bundle.getInt("duration"); 33 int currentPosition = bundle.getInt("currentPosition"); 34 seekBar.setMax(duration); 35 seekBar.setProgress(currentPosition); 36 } 37 }; 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != 43 PackageManager.PERMISSION_GRANTED){ 44 ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1); 45 } 46 Intent intent=new Intent(this,MusicService.class); 47 startService(intent); 48 conn=new MyConn(); 49 bindService(intent,conn,BIND_AUTO_CREATE); 50 //找到seekBar 51 seekBar=(SeekBar)findViewById(R.id.sb); 52 } 53 public void play(View v){ 54 iMusicService.startPlay(); 55 } 56 public void replay(View v){ 57 iMusicService.repaly(); 58 } 59 public void pause(View v){ 60 iMusicService.pausePlay(); 61 } 62 public void stop(View v){ 63 iMusicService.stopPlay(); 64 } 65 private class MyConn implements ServiceConnection { 66 @Override 67 public void onServiceConnected(ComponentName componentName, IBinder iBinder) { 68 iMusicService=(myMusicService)iBinder; 69 } 70 71 @Override 72 public void onServiceDisconnected(ComponentName componentName) { 73 74 } 75 } 76 @Override 77 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 78 switch (requestCode){ 79 case 1: 80 if (grantResults.length<0&&grantResults[0]!=PackageManager.PERMISSION_GRANTED){ 81 Toast.makeText(this,"不授权将无法使用",Toast.LENGTH_LONG).show(); 82 } 83 } 84 } 85 }
4.添加通过拖动进度条调整播放进度
4.1在服务当中监视当前歌曲播放进度,并通过handler.sendMessage方法进行把数据传递当MainActivity中
1 private void updateSeekBar() { 2 //获取当前播放总进度 3 final int duration=mediaPlayer.getDuration(); 4 //使用timer 5 final Timer timer=new Timer(); 6 final TimerTask task=new TimerTask() { 7 @Override 8 public void run() { 9 int currentPosition=mediaPlayer.getCurrentPosition(); 10 Message msg=Message.obtain(); 11 Bundle bundle=new Bundle(); 12 bundle.putInt("duration",duration); 13 bundle.putInt("currentPosition",currentPosition); 14 msg.setData(bundle); 15 MainActivity.handler.sendMessage(msg); 16 } 17 }; 18 timer.schedule(task,100,1000); 19 //当歌曲执行完毕后取消 20 mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 21 @Override 22 public void onCompletion(MediaPlayer mediaPlayer) { 23 timer.cancel(); 24 task.cancel(); 25 } 26 }); 27 }
4.2在handler类中调用handlermessage方法,对于服务所传递过来的数据进行操作
public static Handler handler=new Handler(){ public void handleMessage(Message msg) { //获取数据 Bundle bundle=msg.getData(); int duration=bundle.getInt("duration"); int currentPosition = bundle.getInt("currentPosition"); seekBar.setMax(duration); seekBar.setProgress(currentPosition); } };
4.3通过设置对进度条的监听事件调用MediaPlayer类中的seekTo方法
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { //当进度改变调用 } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { //拖动停止调用 iMusicService.seekToPlay(seekBar.getProgress()); } });
Music播放器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。