我目前正在使用exoplayer只是为了在surfaceview(http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_50mb.mp4)上播放示例视频,通过设置这个设置:
player = (SurfaceView)rootView.findViewById(R.id.player_surface_view);
Allocator a = new DefaultAllocator(1);
DataSource ds = new DefaultUriDataSource(getActivity(),"a");
SampleSource sampleSource = new ExtractorSampleSource(Uri.parse("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_50mb.mp4"), ds, a,1,null);
MediaCodecSelector mcs = MediaCodecSelector.DEFAULT;
//VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING or VIDEO_SCALING_MODE_SCALE_TO_FIT
TrackRenderer videoRenderer = new MediaCodecVideoTrackRenderer(getActivity(),sampleSource,mcs, MediaCodec.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
TrackRenderer audioRenderer = new MediaCodecAudioTrackRenderer(sampleSource,mcs);
exoPlayer = ExoPlayer.Factory.newInstance(2,10000,50000);
exoPlayer.prepare(videoRenderer, audioRenderer);
exoPlayer.addListener(this);
exoPlayer.sendMessage(videoRenderer, MediaCodecVideoTrackRenderer.MSG_SET_SURFACE, player.getHolder().getSurface());
exoPlayer.setPlayWhenReady(true);
在听众身上,我正在记录exoplayerstate:
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
Log.i("exoplayerstate","onPlayerStateChanged"+" "+playWhenReady+ " "+playbackState);
}
结果:
exoplayerstate:onPlayerStateChanged true 1
exoplayerstate:onPlayerStateChanged true 2
exoplayerstate:onPlayerStateChanged true 3
exoplayerstate:onPlayerStateChanged true 3
通过查看exoplayer文档,州代码为:
STATE_IDLE = 1; STATE_PREPARING = 2; STATE_BUFFERING = 3; STATE_READY = 4; STATE_ENDED = 5;
顺便说一句,我认为不相关,但我的视图的xml是:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.exoplayer.AspectRatioFrameLayout
android:id="@+id/video_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<SurfaceView
android:id="@+id/player_surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"/>
</com.google.android.exoplayer.AspectRatioFrameLayout>
有谁可以告诉我为什么我从未达到州= 4(STATE_READY)以便视频可以开始播放?
答案 0 :(得分:1)
两个观察结果:
DefaultAllocator()
的参数是缓冲区中段的大小(以字节为单位)。 细分是指您要传输的媒体文件的一部分。通过将其设置为1,您的流缓冲区将设置为1个字节。因此,ExoPlayer尝试一次一个字节地下载和播放MP4文件,因此, long 时间将达到READY状态。
将段大小(参数设置为DefaultAllocator()
)设置为更大的值,例如64 * 1024,如示例代码中所示。
类似地,ExtractorSampleSource()
的第四个参数确定下载缓冲区大小(以字节为单位)。它还需要设置为更大的值,例如256 * 64 * 1024,如示例代码中所示。最后,我建议通过省略此函数的第五个参数来使用默认提取器(即,不要传递null
)。