ExoPlayer簡單使用幫助類

最簡單使用exoplayer工具類(帶視頻緩存)

/**
 * Created  on  2019/05/29.
 * interface by
 *
 * @author fengzimin
 */
public class SimpleExoPlayerHelper {

    private final Context mContext;
    private SimpleExoPlayer mSimpleExoPlayer;
    private ConcatenatingMediaSource mConcatenatingMediaSource;
    private static SimpleExoPlayerHelper simpleExoPlayerHelper;

    public SimpleExoPlayerHelper(Context context, PlayerView playerView) {
        mContext = context;
        // 創(chuàng)建帶寬
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        // 創(chuàng)建軌道選擇工廠
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

        // 創(chuàng)建軌道選擇器實例
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

        //step2\. 創(chuàng)建播放器
        mSimpleExoPlayer = ExoPlayerFactory.newSimpleInstance(context, trackSelector);

        playerView.setPlayer(mSimpleExoPlayer);

        mConcatenatingMediaSource = new ConcatenatingMediaSource();

    }

    public static SimpleExoPlayerHelper createMySimpleExoPlayer(Context context, PlayerView playerView) {
        simpleExoPlayerHelper = new SimpleExoPlayerHelper(context, playerView);
        return simpleExoPlayerHelper;
    }

    /**
     * @return 提供simpleExoPlayer對象,實現(xiàn)其他功能
     */
    public SimpleExoPlayer getSimpleExoPlayer() {
        return mSimpleExoPlayer;
    }

    public SimpleExoPlayerHelper prepare(List<String> urls) {
        for (String url : urls) {
            MediaSource mediaSource = buildMediaSource(url);
            mConcatenatingMediaSource.addMediaSource(mediaSource);
        }
        mSimpleExoPlayer.prepare(mConcatenatingMediaSource);
        return simpleExoPlayerHelper;
    }

    public SimpleExoPlayerHelper prepare(String url) {
        List<String> strings = new ArrayList<>();
        strings.add(url);
        prepare(strings);
        return simpleExoPlayerHelper;
    }

    public enum RepeatMode {
        REPEAT_MODE_ALL(Player.REPEAT_MODE_ALL),
        REPEAT_MODE_ONE(Player.REPEAT_MODE_ONE),
        REPEAT_MODE_OFF(Player.REPEAT_MODE_OFF);

        private int mMode;

        RepeatMode(int repeatMode) {
            mMode = repeatMode;
        }

        public int getMode() {
            return mMode;
        }

        public void setMode(int mode) {
            mMode = mode;
        }
    }

    /**
     * 設置循環(huán)樣式
     *
     * @param repeatMode
     * @return
     */
    public SimpleExoPlayerHelper setRepeatMode(RepeatMode repeatMode) {
        mSimpleExoPlayer.setRepeatMode(repeatMode.getMode());
        return simpleExoPlayerHelper;
    }

    public SimpleExoPlayerHelper setPlayWhenReady(boolean playWhenReady) {
        mSimpleExoPlayer.setPlayWhenReady(playWhenReady);
        return simpleExoPlayerHelper;
    }

    public void stop() {
        mSimpleExoPlayer.stop();
    }

    public void pause() {
        boolean playWhenReady = mSimpleExoPlayer.getPlayWhenReady();
        if(playWhenReady) {
            mSimpleExoPlayer.setPlayWhenReady(false);
        }
    }

    public void start() {
        boolean playWhenReady = mSimpleExoPlayer.getPlayWhenReady();
        if(!playWhenReady) {
            mSimpleExoPlayer.setPlayWhenReady(true);
        }
    }

    public void release() {
        mSimpleExoPlayer.release();
    }

    private MediaSource buildMediaSource(String url) {
        String proxyUrl = getProxyUrl(url);
        return newVideoSource(proxyUrl);
    }

    /**
     * 獲取帶緩存的視頻url
     *
     * @param originalUrl
     * @return
     */
    private String getProxyUrl(String originalUrl) {
        HttpProxyCacheServer proxy = VideoCacheProxy.getProxy(mContext);
        return proxy.getProxyUrl(originalUrl);
    }

    /**
     * 構(gòu)造播放數(shù)據(jù)
     *
     * @param url
     * @return
     */
    private MediaSource newVideoSource(String url) {
        //測量播放過程中的帶寬。 如果不需要,可以為null。
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // 生成加載媒體數(shù)據(jù)的DataSource實例。
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, "useExoplayer"), bandwidthMeter);
        // 生成用于解析媒體數(shù)據(jù)的Extractor實例。
        ExtractorMediaSource.Factory factory = new ExtractorMediaSource.Factory(dataSourceFactory);
        ExtractorMediaSource mediaSource = factory.createMediaSource(Uri.parse(url));
        //        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        //        return new ExtractorMediaSource(Uri.parse(url), dataSourceFactory, extractorsFactory, null,
        //                null);
        return mediaSource;
    }
}

image.gif

設置緩存:

public class App extends Application implements VideoCacheProxy.AppWrapper {
    private HttpProxyCacheServer mProxy;
    private HttpProxyCacheServer newProxy() {
        // 這里的配置可以修改,參考上面的“這里”
        return new HttpProxyCacheServer.Builder(this)
        // 視頻緩存文件所在的文件夾,可以進行修改
        .cacheDirectory(VideoCacheProxy.getVideoCacheDir(this))
        .build();
    }
    @Override
    public HttpProxyCacheServer getVideoCacheProxy() {
        return mProxy == null ? (mProxy = newProxy()) : mProxy;
    }
}
image.gif

使用方法:

1.導入依賴包

    //exoplayer播放器
    implementation 'com.google.android.exoplayer:exoplayer:2.8.4'
    // 視頻緩存
    implementation 'com.danikula:videocache:2.7.0'
image.gif

2.xml布局中添加PlayerView

   <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/exo_play_2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        app:layout_constraintTop_toTopOf="parent"/>
image.gif

3.使用工具類

    private void initExo2() {
        //播放的數(shù)據(jù)源
        List<String> strings = new ArrayList<>();
        strings.add("https://lixiang.https.xiaozhuschool.com/statics/images/shiping/1556521397576.mp4");
        strings.add("https://lixiang.https.xiaozhuschool.com/statics/images/shiping/net1.mp3");
        strings.add("https://lixiang.https.xiaozhuschool.com/statics/images/shiping/1556521318783.mp4");
        //設置
        mSimpleExoPlayerHelper1 = SimpleExoPlayerHelper.createMySimpleExoPlayer(this, mExoPlay2)
                .prepare(strings)
                .setRepeatMode(SimpleExoPlayerHelper.RepeatMode.REPEAT_MODE_ONE)
                .setPlayWhenReady(true);
        //是否顯示默認進度條
        mExoPlay.setUseController(true);
    }
image.gif
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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