当我开始播放并旋转设备时,位置会向前跳跃约6秒,并且(必要时)音频会在发生这种情况时切断.然后,播放继续正常进行.我不知道是什么导致这种情况发生.
根据要求,这是代码:
public class MainFragment extends Fragment implements SurfaceHolder.Callback,MediaController.MediaPlayerControl { private static final String TAG = MainFragment.class.getSimplename(); AssetfileDescriptor mVIDeoFd; SurfaceVIEw mSurfaceVIEw; MediaPlayer mMediaPlayer; MediaController mMediaController; boolean mPrepared; boolean mShouldResumePlayback; int mBufferingPercent; SurfaceHolder mSurfaceHolder; @OverrIDe public voID onInflate(Activity activity,AttributeSet attrs,Bundle savedInstanceState) { super.onInflate(activity,attrs,savedInstanceState); final String assetfilename = "test-vIDeo.mp4"; try { mVIDeoFd = activity.getAssets().openFd(assetfilename); } catch (IOException ioe) { Log.e(TAG,"Can't open file " + assetfilename + "!"); } } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); // initialize the media player mMediaPlayer = new MediaPlayer(); try { mMediaPlayer.setDataSource(mVIDeoFd.getfileDescriptor(),mVIDeoFd.getStartOffset(),mVIDeoFd.getLength()); } catch (IOException ioe) { Log.e(TAG,"Unable to read vIDeo file when setting data source."); throw new RuntimeException("Can't read assets file!"); } mMediaPlayer.setonPreparedListener(new MediaPlayer.OnPreparedListener() { @OverrIDe public voID onPrepared(MediaPlayer mp) { mPrepared = true; } }); mMediaPlayer.setonBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() { @OverrIDe public voID onBufferingUpdate(MediaPlayer mp,int percent) { mBufferingPercent = percent; } }); mMediaPlayer.prepareAsync(); } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { super.onCreateVIEw(inflater,container,savedInstanceState); VIEw vIEw = inflater.inflate(R.layout.fragment_main,false); mSurfaceVIEw = (SurfaceVIEw) vIEw.findVIEwByID(R.ID.surface); mSurfaceVIEw.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { mMediaController.show(); } }); mSurfaceHolder = mSurfaceVIEw.getHolder(); if (mSurfaceHolder == null) { throw new RuntimeException("SufraceVIEw's holder is null"); } mSurfaceHolder.addCallback(this); return vIEw; } @OverrIDe public voID onVIEwCreated(VIEw vIEw,Bundle savedInstanceState) { super.onVIEwCreated(vIEw,savedInstanceState); mMediaController = new MediaController(getActivity()); mMediaController.setEnabled(false); mMediaController.setMediaPlayer(this); mMediaController.setAnchorVIEw(vIEw); } @OverrIDe public voID onResume() { super.onResume(); if (mShouldResumePlayback) { start(); } else { mSurfaceVIEw.post(new Runnable() { @OverrIDe public voID run() { mMediaController.show(); } }); } } @OverrIDe public voID surfaceCreated(SurfaceHolder holder) { mMediaPlayer.setdisplay(mSurfaceHolder); mMediaController.setEnabled(true); } @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { // nothing } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { mMediaPlayer.setdisplay(null); } @OverrIDe public voID onPause() { if (mMediaPlayer.isPlaying() && !getActivity().isChangingConfigurations()) { pause(); mShouldResumePlayback = true; } super.onPause(); } @OverrIDe public voID onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @OverrIDe public voID onDestroyVIEw() { mMediaController.setAnchorVIEw(null); mMediaController = null; mMediaPlayer.setdisplay(null); mSurfaceHolder.removeCallback(this); mSurfaceHolder = null; mSurfaceVIEw = null; super.onDestroyVIEw(); } @OverrIDe public voID onDestroy() { mMediaPlayer.release(); mMediaPlayer = null; try { mVIDeoFd.close(); } catch (IOException ioe) { Log.e(TAG,"Can't close asset file..",ioe); } mVIDeoFd = null; super.onDestroy(); } // MediaControler methods: @OverrIDe public voID start() { mMediaPlayer.start(); } @OverrIDe public voID pause() { mMediaPlayer.pause(); } @OverrIDe public int getDuration() { return mMediaPlayer.getDuration(); } @OverrIDe public int getCurrentposition() { return mMediaPlayer.getCurrentposition(); } @OverrIDe public voID seekTo(int pos) { mMediaPlayer.seekTo(pos); } @OverrIDe public boolean isPlaying() { return mMediaPlayer.isPlaying(); } @OverrIDe public int getBufferPercentage() { return mBufferingPercent; } @OverrIDe public boolean canPause() { return true; } @OverrIDe public boolean canSeekBackward() { return true; } @OverrIDe public boolean canSeekForward() { return true; } @OverrIDe public int getAudioSessionID() { return mMediaPlayer.getAudioSessionID(); }}
onPause方法中的if块未被命中.
更新:
在进行了一些调试之后,删除与SurfaceHolder的交互会导致问题消失.换句话说,如果我没有在MediaPlayer上设置显示,则在配置更改期间音频将正常工作:无暂停,无跳过.看起来设置MediaPlayer上的显示有一些时间问题会让玩家感到困惑.
另外,我发现在配置更改期间删除它之前必须隐藏()MediaController.这样可以提高稳定性,但不能解决跳过问题.
另一个更新:
如果您在意,AndroID媒体堆栈如下所示:
MediaPlayer.java -> androID_media_MediaPlayer.cpp -> MediaPlayer.cpp -> IMediaPlayer.cpp -> MediaPlayerService.cpp -> BnMediaPlayerService.cpp -> IMediaPlayerService.cpp -> *ConcreteMediaPlayer* -> *BaseMediaPlayer* (Stagefright,NuPlayerDriver,MIDi,etc) -> *real MediaPlayerProxy* (AwesomePlayer,NuPlayer,etc) -> *RealMediaPlayer* (AwesomePlayerSource,NuPlayerDecoder,etc) -> Codec -> HW/SW decoder
在检查AwesomePlayer时,看起来这个很棒的玩家在setSurface()时可以自由地暂停自己:
status_t AwesomePlayer::setNativeWindow_l(const sp<ANativeWindow> &native) { mNativeWindow = native; if (mVIDeoSource == NulL) { return OK; } ALOGV("attempting to reconfigure to use new surface"); bool wasplaying = (mFlags & PLAYING) != 0; pause_l(); mVIDeoRenderer.clear(); shutdownVIDeoDecoder_l(); status_t err = initVIDeoDecoder(); if (err != OK) { ALOGE("Failed to reinstantiate vIDeo decoder after surface change."); return err; } if (mLastVIDeoTimeUs >= 0) { mSeeking = SEEK; mSeekTimeUs = mLastVIDeoTimeUs; modifyFlags((AT_EOS | AUdio_AT_EOS | VIDEO_AT_EOS),CLEAR); } if (wasplaying) { play_l(); } return OK;}
这表明设置表面将导致玩家破坏以前使用的任何表面以及视频解码器.将曲面设置为null不应导致音频停止,将其设置为新曲面需要重新初始化视频解码器并且播放器要搜索视频中的当前位置.按照惯例,寻求永远不会超过你的要求,也就是说,如果你在寻求时超过一个关键帧,你应该落在你超越的框架上(而不是下一个).
因此,我的假设是,AndroID MediaPlayer不遵守此约定,并在搜索时跳转到下一个关键帧.这与具有稀疏关键帧的视频源相结合,可以解释我正在经历的跳跃.不过,我没有看过AwesomePlayer的搜索实现.有人提到,跳过下一个关键帧是需要发生的事情,如果您的MediaPlayer开发时考虑了流式传输,因为流可以在消费后立即丢弃.重点是,可能不会认为MediaPlayer会选择向前跳而不是向后跳.
最后更新:
虽然我仍然不知道为什么在将新Surface作为MediaPlayer的显示器附加时播放会跳过,但由于接受了答案,我在播放期间让播放无缝.
解决方法 感谢natez0r的回答,我设法让设置描述起作用.但是,我使用的方法略有不同.我将在此详细介绍以供参考.我有一个片段,我标记在配置更改中保留.此片段处理媒体播放(MediaPlayer)和标准TextureVIEw(提供视频缓冲区转储的SurfaceTexture).只有在我的Activity完成onResume()并且SurfaceTexture可用后,我才会初始化媒体播放.一旦我收到对SurfaceTexture的引用,我只是在我的片段中调用setSurfaceTexture(因为它是公共的)而不是子类化TextureVIEw.配置更改发生时保留的唯一两件事是MediaPlayer引用和SurfaceTexture引用.
我已经将source of my sample project上传到了Github.随意看看!
总结以上是内存溢出为你收集整理的android – MediaPlayer在旋转时向前跳过大约6秒全部内容,希望文章能够帮你解决android – MediaPlayer在旋转时向前跳过大约6秒所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)