如何在视图分页器中的片段内处理MediaPlayer对象 [英] How to handle MediaPlayer object inside a fragment inside view pager
问题描述
我有一个viewpager来保存片段,每个片段都具有MediaPlayer对象,即每个片段都具有附加的音频.我的要求是,如果我滑动Viewpager,当前片段媒体播放器应停止播放,而下一个片段媒体播放器应播放.如果我向后滑动viewpager,则当前的片段媒体播放器应停止并且先前的片段媒体播放器应启动.不必在每个片段上都附加音频,这就是为什么每个片段都具有自己的媒体播放器对象,而我已经检查了音频以处理它.到目前为止,我已经尝试过以下代码
I have viewpager which holds fragments, each fragment is having MediaPlayer object i.e each fragment is having audio attached to it. My requirement is if i swipe the viewpager,current fragment media player should stop and next fragment media player should play. If i swipe viewpager backwards then current fragment media player should stop and previous fragment media player should start. It is not necessary that each fragment should have a audio attached to it so thats why each fragment is having own media player object and i have put the check for audio to handle it. so far i have tried below code
在每个片段中进行可见性检查并相应地播放音频,
In each fragment is having a visibility check and play the audio accordingly,
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
if (mPlayer != null ) {
mPlayer.start();
}
} else {
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.pause();
mPlayer.seekTo(0);
}
}
}
如果我在片段的OnStop中释放媒体播放器对象,那么如果我再次遇到相同的片段,音频将停止工作.
If i release media player object in OnStop of fragment then audio stop working if i comes again to same fragment.
@Override
public void onStop() {
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.stop();
mPlayer.reset(); // Might not be necessary, since release() is called right after, but it doesn't seem to hurt/cause issues
mPlayer.release();
mPlayer = null;
}
super.onStop();
}
该如何解决?我不知道如何在viewpager片段的内部控制媒体播放器.
How to resolve this ? I dont know how to control media player inside fragment of a viewpager.
推荐答案
最好在片段中重写onPause()和onResume().并且我们需要为媒体播放器保存当前位置.因此,我正在使用共享首选项来保存媒体播放器的位置.
在onCreateView, mediaPlayer_position = getActivity().getSharedPreferences("PLAY_PAUSE", Activity.MODE_PRIVATE).getInt("CHECK_PLAY_PAUSE", 0);
It is better to override onPause() and onResume() in fragment. And we need to save the current position for media player. So, I am using the shared preference to save the position for media player.
In onCreateView, mediaPlayer_position = getActivity().getSharedPreferences("PLAY_PAUSE", Activity.MODE_PRIVATE).getInt("CHECK_PLAY_PAUSE", 0);
@Override
public void onResume() {
super.onResume();
int position = getActivity().getSharedPreferences("PLAY_PAUSE", Activity.MODE_PRIVATE).getInt("CHECK_PLAY_PAUSE", 0);
if(position > 0) {
try {
mediaPlayer.setDataSource(mp3_link); //mp3_link from url to mediaplayer data source
mediaPlayer.prepare();
} catch (Exception e) {
e.printStackTrace();
}
mediaPlayer.seekTo(position);
mediaPlayer.start();
}
}
@Override
public void onPause() {
super.onPause();
if(mediaPlayer.isPlaying()) {
mediaPlayer_position = mediaPlayer.getCurrentPosition();
getActivity().getSharedPreferences("PLAY_PAUSE", Context.MODE_PRIVATE).edit().putInt("CHECK_PLAY_PAUSE", mediaPlayer_position).apply();
mediaPlayer.pause();
}
}
参考:" https://www.hrupin.com/2011/02/example-of-streaming-mp3-mediafile-with-android-mediaplayer-class "
这篇关于如何在视图分页器中的片段内处理MediaPlayer对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!