我尝试在服务中播放歌曲,但当我按下“返回”按钮时,它就停止播放。因此,我重写了onbackpress
方法,但这样做直接将我带到了主屏幕。我在不同的片段中显示多个歌曲类别,并通过接口传递ArrayList和位置。这部分没有问题,但我该如何在按下返回按钮时播放歌曲呢?我也尝试重写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);
}
}
我重写了返回键的方法后,它播放了音乐。但是在任何片段中都会将我带回主屏幕。