havlenapetr-FFMpeg OnVideoSizeChangedListener接口的实现
转载自:http://blog.csdn.net/yarpee/article/details/6910877
moruite在他的文章http://blog.csdn.net/moruite/article/details/6305944里说到了用havlenapetr-FFMpeg播放视频花屏的问题,需要自己实现OnVideoSizeChangedListener接口。网上没有找到相关的资料,自己研究了下。秉着OpenSource的观念,取之于havlenapetr-FFMpeg,还给世界。
FFMpegPlayer中添加如下接口和变量:
// Add by yarpee start /** * Interface definition of a callback to be invoked when the * video size is first known or updated */ public interface OnVideoSizeChangedListener { /** * Called to indicate the video size * * @param mp the MediaPlayer associated with this callback * @param width the width of the video * @param height the height of the video */ public void onVideoSizeChanged(FFMpegPlayer mp, int width, int height); } /** * Register a callback to be invoked when the video size is * known or updated. * * @param listener the callback that will be run */ public void setOnVideoSizeChangedListener(OnVideoSizeChangedListener listener) { Log.d(TAG, "setOnVideoSizeChangedListener"); mOnVideoSizeChangedListener = listener; } private static OnVideoSizeChangedListener mOnVideoSizeChangedListener; // Add by yarpee end
FFMpegPlayer中修改如下函数:
private static void postEventFromNative(Object mediaplayer_ref, int what, int arg1, int arg2, Object obj) { switch(what) { case MEDIA_INFO_FRAMERATE_VIDEO: Log.d(TAG, "Video fps:" + arg1); break; case MEDIA_INFO_FRAMERATE_AUDIO: Log.d(TAG, "Audio fps:" + arg1); break; case MEDIA_SET_VIDEO_SIZE: Log.d(TAG, "Video width:" + arg1 + " height:" + arg2); if(mOnVideoSizeChangedListener != null) { Log.d(TAG, "mOnVideoSizeChangedListener != null"); mOnVideoSizeChangedListener.onVideoSizeChanged(null, arg1, arg2); } else { Log.d(TAG, "mOnVideoSizeChangedListener == null"); } } }
FFMpegMovieViewAndroid中修改如下函数:
private void openVideo(SurfaceHolder surfHolder) { try { mPlayer.setDisplay(surfHolder); // Add by yarpee // 必须在prepare前设置listener mPlayer.setOnVideoSizeChangedListener(new FFMpegPlayer.OnVideoSizeChangedListener() { public void onVideoSizeChanged(FFMpegPlayer mp, int width, int height) { // 注意此处不能用mp,mp为null int videoWidth = mPlayer.getVideoWidth(); int videoHeight = mPlayer.getVideoHeight(); if(videoHeight != 0 && videoWidth != 0) { Log.d(TAG, "setFixedSize width:" + videoWidth + " height:" + videoHeight); getHolder().setFixedSize(videoWidth, videoHeight); } } }); mPlayer.prepare(); } catch (IllegalStateException e) { Log.e(TAG, "Couldn't prepare player: " + e.getMessage()); } catch (IOException e) { Log.e(TAG, "Couldn't prepare player: " + e.getMessage()); } }
C代码修改Mediaplayer.cpp中的如下函数:
status_t MediaPlayer::prepare() { status_t ret; mCurrentState = MEDIA_PLAYER_PREPARING; av_log_set_callback(ffmpegNotify); if ((ret = prepareVideo()) != NO_ERROR) { mCurrentState = MEDIA_PLAYER_STATE_ERROR; return ret; } if ((ret = prepareAudio()) != NO_ERROR) { mCurrentState = MEDIA_PLAYER_STATE_ERROR; return ret; } mCurrentState = MEDIA_PLAYER_PREPARED; // Add by yarpee notify(5, mVideoWidth, mVideoHeight); return NO_ERROR; }