Android實(shí)現(xiàn)本地Service方法控制音樂播放
問題現(xiàn)象描述:在Activity中控制播放時,按返回鍵退出應(yīng)用后,音樂可在后臺繼續(xù)播放。重新進(jìn)入app,音樂無法停止,重新點(diǎn)擊開始播放音樂,出現(xiàn)重復(fù)的音樂同時播放的現(xiàn)象(多個同時播放)。如何解決?
解決方法:使用本地Service的方式來控制音樂的播放,app返回退出了,重新進(jìn)入App也可以正常終止。
1、主Activity控制音樂 的開始、暫停、停止、退出空能,(具體實(shí)現(xiàn)在下面MusicService.java中實(shí)現(xiàn))
/** * Activity播放廣播,返回鍵返回后,重新進(jìn)入無法停止 * * 通過start啟動服務(wù)的方式 控制音樂播放 */public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_main_play; private Button btn_main_stop; private Button btn_main_pause; private Button btn_main_exit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_main_play = findViewById(R.id.btn_main_play); btn_main_stop = findViewById(R.id.btn_main_stop); btn_main_pause = findViewById(R.id.btn_main_pause); btn_main_exit = findViewById(R.id.btn_main_exit); btn_main_play.setOnClickListener(this); btn_main_stop.setOnClickListener(this); btn_main_pause.setOnClickListener(this); btn_main_exit.setOnClickListener(this); } /** * 按鈕點(diǎn)擊監(jiān)聽事件 將點(diǎn)擊的類型傳給服務(wù)進(jìn)行判斷 * @param v */ @Override public void onClick(View v) { Intent intent = new Intent(this,MusicService.class); switch ( v.getId()){ case R.id.btn_main_play://播放 intent.putExtra('action','play'); startService(intent); break; case R.id.btn_main_stop://停止 intent.putExtra('action','stop'); startService(intent); break; case R.id.btn_main_pause://暫停 intent.putExtra('action','pause'); startService(intent); break; case R.id.btn_main_exit://退出并關(guān)閉音樂 //停止服務(wù) stopService(intent); finish(); break; default: } }}
2、activity_main.xml布局代碼省略,效果圖如下:
3、創(chuàng)建音樂播放器處理流程的服務(wù) MusicService.java:
/** * 通過服務(wù)控制音樂的播放 */public class MusicService extends Service { public MusicService() { } //創(chuàng)建播放器對象 private MediaPlayer player; @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException('Not yet implemented'); } @Override public int onStartCommand(Intent intent, int flags, int startId) { //獲取MainActivity中 按鈕的點(diǎn)擊類型:根據(jù)不同類型處理不同事件 String action = intent.getStringExtra('action'); if ('play'.equals(action)) { //播放 playMusic(); } else if ('stop'.equals(action)) { //停止 stopMusic(); } else if ('pause'.equals(action)) { //暫停 pauseMusic(); } return super.onStartCommand(intent, flags, startId); } /** * 播放音樂 */ public void playMusic() { if (player == null ) { player= MediaPlayer.create(this,R.raw.kkth_myr); } player.start(); } /** * 暫停播放 */ public void pauseMusic() { if (player != null && player.isPlaying()) { player.pause(); } } /** * 停止播放 */ public void stopMusic() { if (player != null) { player.stop(); player.reset();//重置 player.release();//釋放 player = null; } } @Override public void onDestroy() { super.onDestroy(); //在服務(wù)死亡之前停止音樂 stopMusic(); }}
創(chuàng)建服務(wù)看一下配置文件AndroidManifest.xml中是否添加了對應(yīng)的<Service>標(biāo)簽。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 實(shí)現(xiàn)圖片修復(fù)(可用于去水印)2. Android自定義控件實(shí)現(xiàn)方向盤效果3. asp讀取xml文件和記數(shù)4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. XHTML 1.0:標(biāo)記新的開端6. Java 生成帶Logo和文字的二維碼7. xml中的空格之完全解說8. python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件9. 怎樣才能用js生成xmldom對象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?10. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)
