Android ExoPlayer 填坑之路


自从上次做完视频播放器调研以后,心里就知道,肯定以后这块东西都是我做,果不其然,公司对视频播放这块不断的优化。我就悲催的无限填坑,话说英语差,看国外文档真的很吃力。
简单讲一下项目中遇到的问题。

  • 创建和基本使用
    这个不多讲,最简单使用就是布局里
 <com.google.android.exoplayer2.ui.PlayerView
                android:id="@+id/videoview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:surface_type="texture_view"
                app:use_controller="false" />

接下来创建轨道,播放器等,如果你播放的时候发现有时候黑屏。这是可能你的VideoCache不是单例模式。 下面是我自己写的Manger和VideoCache

public class ExoPlayerManger {
    private static final String TAG = "ExoPlayerManger";
    private Context mContext;
    private  BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    // 创建轨道选择工厂
    private TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    // 创建轨道选择器实例
    private TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    private SimpleExoPlayer simpleExoPlayer;
    private DataSource.Factory dataSourceFactory;
    private String mVideoUrl;
    private SimpleCache simpleCache;
    private Uri playVideoUri;
    private ExtractorMediaSource mediaSource;


    /**
     * @param context 传入context
     */
    public void setBuilderContext(Context context) {
        mContext = context;
        dataSourceFactory = new DefaultDataSourceFactory(mContext, "seyed");
    }

    /**
     * @param videoUrl 传入视频路径
     */
    public void setVideoUrl(String videoUrl) {
        this.mVideoUrl = videoUrl;
        simpleCache = VideoCache.getInstance(mContext);
        playVideoUri = Uri.parse(mVideoUrl);
    }


    /**
     * @return 返回exoPlayer对象
     */
    public SimpleExoPlayer create() {
        try {
            simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);
            dataSourceFactory = new CacheDataSourceFactory(simpleCache, dataSourceFactory);
            mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(playVideoUri);
            simpleExoPlayer.prepare(mediaSource);

        } catch (Exception e) {

        }
        return simpleExoPlayer;
    }


}


/**
 * @author :leo on 2018/12/17 17:58
 * <p>
 * 方法用途 :视频缓存单例模式
 */
public class VideoCache {
    private static SimpleCache sDownloadCache;

    /**
     * @param context
     * @return
     */
    public static SimpleCache getInstance(Context context) {
        if (sDownloadCache == null) {
            sDownloadCache = new SimpleCache(new File(getMediaCacheFile(context), "StoryCache"), new LeastRecentlyUsedCacheEvictor(512 * 1024 *1024));

        }
        return sDownloadCache;
    }

    public static File getMediaCacheFile(Context context) {
        String directoryPath = "";
        String childPath = "exoPlayer";
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            // 外部储存可用
            directoryPath = File.separator + context.getExternalFilesDir(childPath).getAbsolutePath();
        } else {
            directoryPath = File.separator + context.getFilesDir().getAbsolutePath() + File.separator + childPath;
        }
        File file = new File(directoryPath);
        //判断文件目录是否存在
        if (!file.exists()) {
            file.mkdirs();
        }

        return file;
    }


}

紧接着 只需要

ExoPlayerManger exoPlayerManger = new ExoPlayerManger();
        exoPlayerManger.setBuilderContext(mContext);
        exoPlayerManger.setVideoUrl(playVideoUrl);
        simpleExoPlayer = exoPlayerManger.create();
        //设置音量  测试期间设置为0
        simpleExoPlayer.setVolume(10);
        videoView.setPlayer(simpleExoPlayer);
        //赋值给显示时间
        simpleExoPlayer.addListener(this);
      //开启播放
        simpleExoPlayer.setPlayWhenReady(true);

播放器有个监听 Player.EventListener ,这就讲几个状态

@Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        if (isFullScreen) {
            fullScreenDialog.setPlayState(playbackState);
        }
        switch (playbackState) {
        //缓冲状态
            case 2:
                break;
            //播放状态
            case 3:
             
                break;
            //播放完成
            case 4:
               
            default:
                break;
        }

  • 如何播放raw下文件
    RawResourceDataSource.buildRawResourceUri(R.raw.login_bg_video);
        ExoPlayerManger exoPlayerManger = new ExoPlayerManger();
        exoPlayerManger.setBuilderContext(getContext());
//设置从raw下读取的文件路径
   exoPlayerManger.setVideoUrl(RawResourceDataSource.buildRawResourceUri(R.raw.login_bg_video).toString());
        simpleExoPlayer = exoPlayerManger.create();
        simpleExoPlayer.setVolume(0);
        simpleExoPlayer.setRepeatMode(1);
        playerView.setPlayer(simpleExoPlayer);

//        simpleExoPlayer.prepare(audioSource);

        simpleExoPlayer.setPlayWhenReady(true);
  • 列表中点击切换到全屏
    这里有很多方法,我选择的方法不是最好的。科学上网看了很多老外写的例子,大部分都是弹出一个Dialog,将item中的播放PlayerView,remove出来,放到Dialog里面。然后更改PlayerView的布局大小就可以了。
    但是这里会有一个卡顿问题,老外同学们基本没讲,如果你按照直接removeView然后addView to Dialog, 我测试基本会卡1-5s。这时候可以做一个骚操作,就是向前或者向后seekto一下,可以基本做到秒开。当你切换窗口的时候,可以将PlayerView 还回去就可以了,记得设置原来的宽高和大小。
 //从当前布局移除播放view
        ViewGroup parent = (ViewGroup) videoView.getParent();
        if (parent != null) {
            parent.removeView(videoView);
        }

//加入到Dialog
 ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
        layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT;
        layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
        videoView.setLayoutParams(layoutParams);
        rlShow.addView(playerView);

其实也可以尝试使用,simpleExoPlayer.setVideoTextureView();切换画布,但是我在测试的时候,会有卡顿,有时间的同学可以试试,希望你有别的办法可以给我留言,谢谢。还有很多细节的东西,等我想起来再补上,连续加班,今天才有时间写点东西,就先到这里 。


最近公司大佬提出建议,说来回切换全屏的时候有卡顿经过一番百度谷姐,终于找到解决办法。
使用TextureView 作为ExoPlayer播放画布。
TextureView 在切换状态的时候会经历销毁重建过程。
那么之前TextureView中播放的SurfaceTexture也会销毁。
建议在从列表到全屏之前调用TextureView.getSurfaceTexture()
保存当前状态,在设置完新的宽高后,将保存的SurfaceTexture重新设置进去。
这里会有一个坑,就是设置进去也会卡在那里。
在调用TextureView.getSurfaceTexture()之前,给TextureView设置setSurfaceTextureListener 并在onSurfaceTextureDestroyed方法中返回false
在onSurfaceTextureAvailable中重新调用textureView.setSurfaceTexture(mSurfaceTexture);
建议try一下,因为可能会报SurfaceTexture IsReleased 异常。
简单代码如下

 windowTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
 //获取当前缓存的帧
        SurfaceTexture surfaceTexture = windowTextureView.getSurfaceTexture();
//跳转全屏
        fullScreenDialog.setFullScreenRes(windowTextureView, surfaceTexture);

//在全屏页面中
            textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
                @Override
                public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

                    if (mSurfaceTexture == null) {
                        mSurfaceTexture = surface;
                    }
                    try {
                        textureView.setSurfaceTexture(mSurfaceTexture);
                    } catch (Exception e) {

                    }


                }

                @Override
                public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

                }

                @Override
                public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                    return false;
                }

                @Override
                public void onSurfaceTextureUpdated(SurfaceTexture surface) {

                }
            });

经过测试基本可以实现无卡顿切换。


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 229,908评论 6 541
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 99,324评论 3 429
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 178,018评论 0 383
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 63,675评论 1 317
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 72,417评论 6 412
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,783评论 1 329
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,779评论 3 446
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,960评论 0 290
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 49,522评论 1 335
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 41,267评论 3 358
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 43,471评论 1 374
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 39,009评论 5 363
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,698评论 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 35,099评论 0 28
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 36,386评论 1 294
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 52,204评论 3 398
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 48,436评论 2 378

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,491评论 0 17
  • 一、简历准备 1、个人技能 (1)自定义控件、UI设计、常用动画特效 自定义控件 ①为什么要自定义控件? Andr...
    lucas777阅读 5,231评论 2 54
  • 其实之前我想了很久要不要回复的,还是跟你说清楚吧。 我一直以为当自己发现被删了之后是不会来问清楚原因的,毕竟我们已...
    羽琳澪阅读 168评论 0 0
  • 一次次拼劲全力的射门,一次次关键时刻的助攻,一次次折回中场坚韧的回抢... 虽则得球的机会不多,虽则每次都有建功,...
    单子老师阅读 277评论 0 2
  • MyBatis Generator (MBG) 可以用如下方式运行。 参考:http://www.mybatis....
    nerowu阅读 585评论 1 1