界面如图,采用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" ); } }
时间: 2024-10-11 02:59:28