简单音乐播放器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简单音乐播放器相关的知识,希望对你有一定的参考价值。

界面如图,采用service服务组建,实现后台播放。包括 播放,暂停,重播,停止功能
技术分享

技术分享

下面就让我们一起看看它的实现吧(上图路径中要有音频文件)。
 
 
 
1.界面布局就不介绍了
2.service代码
public class PlayerService extends Service {
     
     private MediaPlayer player;
     
     @Override
     public void onCreate() {
            super.onCreate();
           Log. i("" , "onCreate" );
     }
     
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
           Log. i("" , "onStartCommand" );
            return super.onStartCommand(intent, flags, startId);
     }
     
     /**
      * 当activity绑定服务时执行此方法,返回IBinder实现(个人理解,相当于组件间的媒介)
      */
     @Override
     public IBinder onBind(Intent intent) {
           Log. i("" ,"onBind" );
            return new PlayerBinder();
     }
     
     /**
      * 之所以用implements Player,是因为如果PlayerBinder扩展对外的功能时,不会影响Player的调用,实现解耦
      */
     private class PlayerBinder extends Binder implements Player{
 
            @Override
            public boolean play(String path) {
                 boolean result = false;
                 try {
                      player = new MediaPlayer();
                      player.setAudiostreamType(AudioManager. STREAM_MUSIC);
                      player.setDataSource(path);
                      player.prepare();
                      player.start();
                     result = true;
                } catch (Exception e) {
                      //IllegalArgumentException, SecurityException, IllegalStateException, IOException(简化处理)
                      return false;
                }
                 return result;
           }
 
            //播放,暂停切换
            @Override
            public String switchState(String path) {
                String result = "";
                 if( player== null){
                      if(play(path)){
                           result = "暂停";
                     } else {
                           result = "播放";
                     }
                } else if( player.isPlaying()){
                      player.pause();
                     result = "播放";
                } else{
                      player.start();
                     result = "暂停";
                }
                 return result;
           }
 
            @Override
            public String replay() {
                String result = "";
                 if( player!= null){
                      player.seekTo(0);
                      player.start();
                     result = "暂停";
                }
                 return result;
           }
 
            @Override
            public String stop() {
                String result = "";
                 if( player!= null){
                      player.stop();
                      player.release();
                      player = null;
                     result = "播放";
                }
                 return result;
           }
 
            @Override
            public String getPlayerState() {
                String result = "";
                 if( player!= null&& player.isPlaying()){
                     result = "暂停";
                }
                 return result;
           }
     }
     
     @Override
     public void onDestroy() {
            super.onDestroy();
           Log. i("service" , "onDestory" );
     }
}

 

Interface文件
public interface Player {
     
      public boolean play(String path);
      public String switchState(String path);
      public String replay();
      public String stop();
      public String getPlayerState();
 
}

 

 
3.最后贴上activity代码
 
public class MainActivity extends Activity implements OnClickListener {
 
      private Player playerInter;
      private EditText etMainPath;
      private PlayerConnection conn = new PlayerConnection();
      private Button btMainPlayPause, btMainReplay, btMainStop;
 
      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
           setContentView(R.layout. activity_main);
            etMainPath = (EditText) findViewById(R.id.et_main_path );
            btMainPlayPause = (Button) findViewById(R.id.bt_mian_play_pause );
            btMainReplay = (Button) findViewById(R.id.bt_mian_replay );
            btMainStop = (Button) findViewById(R.id.bt_mian_stop );
           
            btMainPlayPause.setOnClickListener( this);
            btMainReplay.setOnClickListener( this);
            btMainStop.setOnClickListener( this);
 
            //service只能被创建一次,当activity销毁后,再次创建activity不会重新创建service
           Intent startIntent = new Intent( this, PlayerService.class );
           startService(startIntent);
 
           Intent bindIntent = new Intent( this, PlayerService.class );
           bindService(bindIntent, conn, BIND_AUTO_CREATE);
     }
 
      private class PlayerConnection implements ServiceConnection {
 
            //当连接成功时执行此方法,获得播放器操作接口
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                 playerInter = (Player) service;
                String state = playerInter.getPlayerState();
                 if(!state.equals( "")){
                      btMainPlayPause.setText(state);
                }
           }
 
            @Override
            public void onServiceDisconnected(ComponentName name) {
 
           }
 
     }
 
      //每次结束时解绑
      @Override
      public void finish() {
            super.finish();
            // 每次结束activity时候,解绑服务连接
           unbindService( conn);
           Log. i("" , "disconnect" );
     }
 
      @Override
      public void onClick(View v) {
            switch (v.getId()) {
            case R.id. bt_mian_play_pause:
                String playState = playerInter.switchState( etMainPath.getText().toString().trim());
                 if(!playState.equals( "")){
                      btMainPlayPause.setText(playState);
                }
                 break;
            case R.id. bt_mian_replay:
                String replayState = playerInter.replay();
                 if(!replayState.equals( "")){
                      btMainPlayPause.setText(replayState);
                }
                 break;
            case R.id. bt_mian_stop:
                String stopState = playerInter.stop();
                 if(!stopState.equals( "")){
                      btMainPlayPause.setText(stopState);
                }
                 break;
            default:
                 break;
           }
     }
     
      @Override
      protected void onDestroy() {
            super.onDestroy();
           Log. i("activity" , "onDestory" );
     }
 
}

 

以上是关于简单音乐播放器的主要内容,如果未能解决你的问题,请参考以下文章

Audio Ducking 后音乐更柔和?

源码分享mui实现简单的手机音乐播放器

简单的音乐播放器

最简单的向HTML网页添加背景音乐的方法

简单音乐播放器

使用Service组件实现简单的音乐播放器功能 --Android基础