长驱直入,傻瓜式操作就是改造VideoView源码,谁让这个控件就是经典Mediaplayer搭配SurfaceView使用,天然教程。
没有设置AudioStremType,因为我的文件没有音频轨道。
记得先准备好音视频文件。
一、VideoView源码
梳理一下VideoView源码,就会发现,删除了多余的subtitle和MediaController外,videoview就只剩下了mediaplayer和surfaceview。
剩下的根据需要改就行了,这里我就改了两个地方:
- MediaPlayer播放assets目录文件
AssetFileDescriptor afd = this.getContext().getAssets().openFd(mFileName);
mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
- setDataSource后不openVideo
为了更贴合人工造的自定义VideoView,只保留mSHCallback里的surfaceCreated里的openVideo。
二、VideoView使用教程:
添加videoview控件到窗口上, 准备好assets目录下的音频文件,然后请往下看。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.nio.player.VideoView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vii_scene"/>
</RelativeLayout>
public SceneView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
//封装surfaceview为自定义控件时, root 必须设置,否则没办法添加surfaceview到窗口上
View mRootView = LayoutInflater.from(contex