按下后退按钮时播放歌曲
Playing song when pressed back button
我正在尝试播放服务中的歌曲,但是当我按下 后退按钮 它 停止 播放歌曲所以我覆盖了 onbackpress
方法,但当我这样做时,它直接将我带到主屏幕。我在不同的片段中显示多个歌曲类别,并通过界面传递数组列表和位置。这部分工作正常,但我如何在返回时播放歌曲按下按钮,我也尝试重写 onpause 方法,但出现了一些错误。实现此目的的正确方法是什么。这是我的播放方法。
@Override
public void onFragmentInteraction(ArrayList<Songfileinfo> songarr, int position) {
playAudio(songarr, position);
Log.v(""+songarr.size(),"I GOT THE SIZE");
}
private void playAudio(ArrayList<Songfileinfo> arrayList, int audioIndex) {
//Check is service is active
if (!serviceBound) {
//Store Serializable audioList to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
Intent playerIntent = new Intent(this, MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Store the new audioIndex to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
//Service is active
//Send a broadcast to the service -> PLAY_NEW_AUDIO
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
}
}
当我覆盖 backpress methor 时它会播放歌曲。但是从任何片段它都会把我带到主屏幕
您的 Service
已绑定到您的 Activity。
在这里阅读:
https://developer.android.com/guide/components/bound-services.html
玩的时候要开服务前台。这样服务就不会在关闭 activity 时被销毁。并在不播放任何歌曲时从前台删除服务。您不需要覆盖后退按钮。
我正在尝试播放服务中的歌曲,但是当我按下 后退按钮 它 停止 播放歌曲所以我覆盖了 onbackpress
方法,但当我这样做时,它直接将我带到主屏幕。我在不同的片段中显示多个歌曲类别,并通过界面传递数组列表和位置。这部分工作正常,但我如何在返回时播放歌曲按下按钮,我也尝试重写 onpause 方法,但出现了一些错误。实现此目的的正确方法是什么。这是我的播放方法。
@Override
public void onFragmentInteraction(ArrayList<Songfileinfo> songarr, int position) {
playAudio(songarr, position);
Log.v(""+songarr.size(),"I GOT THE SIZE");
}
private void playAudio(ArrayList<Songfileinfo> arrayList, int audioIndex) {
//Check is service is active
if (!serviceBound) {
//Store Serializable audioList to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
Intent playerIntent = new Intent(this, MediaPlayerService.class);
startService(playerIntent);
bindService(playerIntent, serviceConnection, Context.BIND_AUTO_CREATE);
} else {
//Store the new audioIndex to SharedPreferences
StorageUtil storage = new StorageUtil(getApplicationContext());
storage.storeAudio(arrayList);
storage.storeAudioIndex(audioIndex);
//Service is active
//Send a broadcast to the service -> PLAY_NEW_AUDIO
Intent broadcastIntent = new Intent(Broadcast_PLAY_NEW_AUDIO);
sendBroadcast(broadcastIntent);
}
}
当我覆盖 backpress methor 时它会播放歌曲。但是从任何片段它都会把我带到主屏幕
您的 Service
已绑定到您的 Activity。
在这里阅读:
https://developer.android.com/guide/components/bound-services.html
玩的时候要开服务前台。这样服务就不会在关闭 activity 时被销毁。并在不播放任何歌曲时从前台删除服务。您不需要覆盖后退按钮。