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;
        // 创建带宽
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();

        // 创建轨道选择工厂
        TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(bandwidthMeter);

        // 创建轨道选择器实例
        TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

        //step2\. 创建播放器
        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对象,实现其他功能
     */
    public SimpleExoPlayer getSimpleExoPlayer() {
        return mSimpleExoPlayer;
    }

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

    public SimpleExoPlayerHelper prepare(String url) {
        List 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;
        }
    }

    /**
     * 设置循环样式
     *
     * @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);
    }

    /**
     * 构造播放数据
     *
     * @param url
     * @return
     */
    private MediaSource newVideoSource(String url) {
        //测量播放过程中的带宽。 如果不需要,可以为null。
        DefaultBandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        // 生成加载媒体数据的DataSource实例。
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext,
                Util.getUserAgent(mContext, "useExoplayer"), bandwidthMeter);
        // 生成用于解析媒体数据的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

   
image.gif

3.使用工具类

    private void initExo2() {
        //播放的数据源
        List 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

你可能感兴趣的:(ExoPlayer简单使用帮助类)