Android ExoPlayer 填坑之路


自從上次做完視頻播放器調(diào)研以后,心里就知道,肯定以后這塊東西都是我做,果不其然,公司對(duì)視頻播放這塊不斷的優(yōu)化。我就悲催的無(wú)限填坑,話說(shuō)英語(yǔ)差,看國(guó)外文檔真的很吃力。
簡(jiǎn)單講一下項(xiàng)目中遇到的問(wèn)題。

  • 創(chuàng)建和基本使用
    這個(gè)不多講,最簡(jiǎn)單使用就是布局里
 <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" />

接下來(lái)創(chuàng)建軌道,播放器等,如果你播放的時(shí)候發(fā)現(xiàn)有時(shí)候黑屏。這是可能你的VideoCache不是單例模式。 下面是我自己寫(xiě)的Manger和VideoCache

public class ExoPlayerManger {
    private static final String TAG = "ExoPlayerManger";
    private Context mContext;
    private  BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
    // 創(chuàng)建軌道選擇工廠
    private TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);
    // 創(chuàng)建軌道選擇器實(shí)例
    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對(duì)象
     */
    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)) {
            // 外部?jī)?chǔ)存可用
            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();
        //設(shè)置音量  測(cè)試期間設(shè)置為0
        simpleExoPlayer.setVolume(10);
        videoView.setPlayer(simpleExoPlayer);
        //賦值給顯示時(shí)間
        simpleExoPlayer.addListener(this);
      //開(kāi)啟播放
        simpleExoPlayer.setPlayWhenReady(true);

播放器有個(gè)監(jiān)聽(tīng) Player.EventListener ,這就講幾個(gè)狀態(tài)

@Override
    public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
        if (isFullScreen) {
            fullScreenDialog.setPlayState(playbackState);
        }
        switch (playbackState) {
        //緩沖狀態(tài)
            case 2:
                break;
            //播放狀態(tài)
            case 3:
             
                break;
            //播放完成
            case 4:
               
            default:
                break;
        }

  • 如何播放raw下文件
    RawResourceDataSource.buildRawResourceUri(R.raw.login_bg_video);
        ExoPlayerManger exoPlayerManger = new ExoPlayerManger();
        exoPlayerManger.setBuilderContext(getContext());
//設(shè)置從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);
  • 列表中點(diǎn)擊切換到全屏
    這里有很多方法,我選擇的方法不是最好的。科學(xué)上網(wǎng)看了很多老外寫(xiě)的例子,大部分都是彈出一個(gè)Dialog,將item中的播放PlayerView,remove出來(lái),放到Dialog里面。然后更改PlayerView的布局大小就可以了。
    但是這里會(huì)有一個(gè)卡頓問(wèn)題,老外同學(xué)們基本沒(méi)講,如果你按照直接removeView然后addView to Dialog, 我測(cè)試基本會(huì)卡1-5s。這時(shí)候可以做一個(gè)騷操作,就是向前或者向后seekto一下,可以基本做到秒開(kāi)。當(dāng)你切換窗口的時(shí)候,可以將PlayerView 還回去就可以了,記得設(shè)置原來(lái)的寬高和大小。
 //從當(dāng)前布局移除播放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);

其實(shí)也可以嘗試使用,simpleExoPlayer.setVideoTextureView();切換畫(huà)布,但是我在測(cè)試的時(shí)候,會(huì)有卡頓,有時(shí)間的同學(xué)可以試試,希望你有別的辦法可以給我留言,謝謝。還有很多細(xì)節(jié)的東西,等我想起來(lái)再補(bǔ)上,連續(xù)加班,今天才有時(shí)間寫(xiě)點(diǎn)東西,就先到這里 。


最近公司大佬提出建議,說(shuō)來(lái)回切換全屏的時(shí)候有卡頓經(jīng)過(guò)一番百度谷姐,終于找到解決辦法。
使用TextureView 作為ExoPlayer播放畫(huà)布。
TextureView 在切換狀態(tài)的時(shí)候會(huì)經(jīng)歷銷(xiāo)毀重建過(guò)程。
那么之前TextureView中播放的SurfaceTexture也會(huì)銷(xiāo)毀。
建議在從列表到全屏之前調(diào)用TextureView.getSurfaceTexture()
保存當(dāng)前狀態(tài),在設(shè)置完新的寬高后,將保存的SurfaceTexture重新設(shè)置進(jìn)去。
這里會(huì)有一個(gè)坑,就是設(shè)置進(jìn)去也會(huì)卡在那里。
在調(diào)用TextureView.getSurfaceTexture()之前,給TextureView設(shè)置setSurfaceTextureListener 并在onSurfaceTextureDestroyed方法中返回false
在onSurfaceTextureAvailable中重新調(diào)用textureView.setSurfaceTexture(mSurfaceTexture);
建議try一下,因?yàn)榭赡軙?huì)報(bào)SurfaceTexture IsReleased 異常。
簡(jiǎn)單代碼如下

 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) {

            }
        });
 //獲取當(dāng)前緩存的幀
        SurfaceTexture surfaceTexture = windowTextureView.getSurfaceTexture();
//跳轉(zhuǎn)全屏
        fullScreenDialog.setFullScreenRes(windowTextureView, surfaceTexture);

//在全屏頁(yè)面中
            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) {

                }
            });

經(jīng)過(guò)測(cè)試基本可以實(shí)現(xiàn)無(wú)卡頓切換。


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,295評(píng)論 0 17
  • 一、簡(jiǎn)歷準(zhǔn)備 1、個(gè)人技能 (1)自定義控件、UI設(shè)計(jì)、常用動(dòng)畫(huà)特效 自定義控件 ①為什么要自定義控件? Andr...
    lucas777閱讀 5,372評(píng)論 2 54
  • 其實(shí)之前我想了很久要不要回復(fù)的,還是跟你說(shuō)清楚吧。 我一直以為當(dāng)自己發(fā)現(xiàn)被刪了之后是不會(huì)來(lái)問(wèn)清楚原因的,畢竟我們已...
    羽琳澪閱讀 192評(píng)論 0 0
  • 一次次拼勁全力的射門(mén),一次次關(guān)鍵時(shí)刻的助攻,一次次折回中場(chǎng)堅(jiān)韌的回?fù)?.. 雖則得球的機(jī)會(huì)不多,雖則每次都有建功,...
    單子老師閱讀 303評(píng)論 0 2
  • MyBatis Generator (MBG) 可以用如下方式運(yùn)行。 參考:http://www.mybatis....
    nerowu閱讀 623評(píng)論 1 1

友情鏈接更多精彩內(nèi)容