我在我的應用程序中製作音頻播放器。它正在使用一個視圖列表,所有工作都很完美。我可以開始暫停,下一個音樂流。我正在加載一個網址來播放音樂。當我關閉應用時,音樂停止播放。我需要繼續玩。我已經閱讀了關於音頻焦點的內容。但我不完全相信如何將它添加到代碼中。我有我的清單wake_lock。當應用程序關閉時,Android Studio音頻停止播放
我對android很新穎,所以它是線索和錯誤,但是這個我只是無法弄清楚。另外,當我關閉應用程序並在音樂播放時再次啓動並啓動它時,應用程序崩潰。
public void controlMusicPlayer(String musicUrl) {
final String playUrl = musicUrl;
Log.d("Song url received:", playUrl);
if (!mediaPlayer.isPlaying()) {
playercontrol.setVisibility(View.VISIBLE);
btn.setBackgroundResource(R.drawable.pause);
new Player()
.execute(playUrl);
playPause = true;
Log.d("Play State:", "Starting song");
} else {
Log.d("Play State:", "Next song is being started");
playPause = true;
mediaPlayer.reset();
btn.setBackgroundResource(R.drawable.pause);
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
playPause = false;
new Player()
.execute(playUrl);
}
}
public View.OnClickListener pausePlay = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!playPause) {
btn.setBackgroundResource(R.drawable.pause);
if (intialStage)
new Player()
.execute(Urlmusic);
else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
} else {
btn.setBackgroundResource(R.drawable.play);
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
playPause = false;
}
}
};
/**
* preparing mediaplayer will take sometime to buffer the content so prepare it inside the background thread and starting it on UI thread.
* @author piyush
*
*/
class Player extends AsyncTask<String, Void, Boolean> {
private ProgressDialog progress;
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean prepared;
try {
mediaPlayer.setDataSource(params[0]);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
intialStage = true;
playPause=false;
btn.setBackgroundResource(R.drawable.play);
mediaPlayer.stop();
mediaPlayer.reset();
}
});
mediaPlayer.prepare();
prepared = true;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
Log.d("IllegarArgument", e.getMessage());
prepared = false;
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
prepared = false;
e.printStackTrace();
}
return prepared;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (progress.isShowing()) {
progress.cancel();
}
Log.d("Prepared", "//" + result);
mediaPlayer.start();
intialStage = false;
}
public Player() {
progress = new ProgressDialog(MainActivity.this);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
this.progress.setMessage("Buffering...");
this.progress.show();
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mediaPlayer != null) {
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
}
////////////////////////////////// end music play list /////////////////////////////////////////
感謝您的幫助。
您應該使用服務播放音頻,而不是僅在活動中播放 –