MediaPlayer + SurfaceView 傻瓜式操作

长驱直入,傻瓜式操作就是改造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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值