【Android -- 开源库】ExoPlayer 的基本使用

在这里插入图片描述

GitHub 地址
最新版本:2.13.2

一、简介

ExoPlayer 是运行在 YouTube app Android 版本上的视频播放器。
优点:

  • 支持HTTP上的动态自适应流DASH和SmoothStreaming。
  • 支持高级的HLS特点,例如正确的处理#EXT-X-DISCONTINUITY标签。
  • 能够无缝的合并,串联,循环播放媒体文件。
  • 能够被高度扩展和定制,以适用不同的场景。

二、使用

1. 添加依赖

implementation 'com.google.android.exoplayer:exoplayer:2.11.0'

2. 清单文件中申请权限及 application 设置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.hk.test">

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/app_icon"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:usesCleartextTraffic="true"     //需加上,避免报错
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. 布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/video_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

4. MainActivity.java

public class MainActivity extends AppCompatActivity {
    private PlayerView mPlayerView;

    private SimpleExoPlayer player;

    private boolean playWhenReady;

    private int currentWindow;

    private long playbackPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();
    }

    private void init() {
       mPlayerView = findViewById(R.id.video_view);
       initializePlayer();
    }

    private void initializePlayer() {
        player = new SimpleExoPlayer.Builder(this).build();

        player.setPlayWhenReady(playWhenReady);
        player.seekTo(currentWindow, playbackPosition);
        Uri playUri = Uri.parse("https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4");
        MediaSource mediaSource = buildMediaSource(playUri);
        player.prepare(mediaSource, true, false);

        mPlayerView.setPlayer(player);
    }

    private MediaSource buildMediaSource(Uri uri) {
        DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, "com.example.exoplayerdemo");
        MediaSource videoSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
        return videoSource;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        player.release();
    }
}

三、小视频 url 地址:

新冠肺炎疫情相关视频:武汉加油中国加油

  1. https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4
  2. https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218093206z8V1JuPlpe.mp4
  3. https://stream7.iqilu.com/10339/article/202002/18/2fca1c77730e54c7b500573c2437003f.mp4
  4. https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218025702PSiVKDB5ap.mp4
  5. https://stream7.iqilu.com/10339/upload_transcode/202002/18/202002181038474liyNnnSzz.mp4
  6. https://stream7.iqilu.com/10339/article/202002/18/02319a81c80afed90d9a2b9dc47f85b9.mp4
  7. http://stream4.iqilu.com/ksd/video/2020/02/17/c5e02420426d58521a8783e754e9f4e6.mp4
  8. http://stream4.iqilu.com/ksd/video/2020/02/17/87d03387a05a0e8aa87370fb4c903133.mp4
  9. https://stream7.iqilu.com/10339/article/202002/17/c292033ef110de9f42d7d539fe0423cf.mp4
  10. http://stream4.iqilu.com/ksd/video/2020/02/17/97e3c56e283a10546f22204963086f65.mp4
  11. https://stream7.iqilu.com/10339/article/202002/17/778c5884fa97f460dac8d90493c451de.mp4
  12. https://stream7.iqilu.com/10339/upload_transcode/202002/17/20200217021133Eggh6zdlAO.mp4
  13. https://stream7.iqilu.com/10339/article/202002/17/4417a27b1a656f4779eaa005ecd1a1a0.mp4
  14. https://stream7.iqilu.com/10339/upload_transcode/202002/17/20200217104524H4D6lmByOe.mp4
  15. https://stream7.iqilu.com/10339/upload_transcode/202002/17/20200217101826WjyFCbUXQ2.mp4
  16. http://stream.iqilu.com/vod_bag_2016//2020/02/16/903BE158056C44fcA9524B118A5BF230/903BE158056C44fcA9524B118A5BF230_H264_mp4_500K.mp4
  17. https://stream7.iqilu.com/10339/upload_transcode/202002/16/20200216050645YIMfjPq5Nw.mp4
  18. https://stream7.iqilu.com/10339/article/202002/16/3be2e4ef4aa21bfe7493064a7415c34d.mp4
  19. https://stream7.iqilu.com/10339/upload_transcode/202002/09/20200209105011F0zPoYzHry.mp4
  20. https://stream7.iqilu.com/10339/upload_transcode/202002/09/20200209104902N3v5Vpxuvb.mp4

20 条速度比较快的地址:

  1. https://v-cdn.zjol.com.cn/280443.mp4
  2. https://v-cdn.zjol.com.cn/276982.mp4
  3. https://v-cdn.zjol.com.cn/276984.mp4
  4. https://v-cdn.zjol.com.cn/276985.mp4
  5. https://v-cdn.zjol.com.cn/276986.mp4
  6. https://v-cdn.zjol.com.cn/276987.mp4
  7. https://v-cdn.zjol.com.cn/276988.mp4
  8. https://v-cdn.zjol.com.cn/276989.mp4
  9. https://v-cdn.zjol.com.cn/276990.mp4
  10. https://v-cdn.zjol.com.cn/276991.mp4
  11. https://v-cdn.zjol.com.cn/276992.mp4
  12. https://v-cdn.zjol.com.cn/276993.mp4
  13. https://v-cdn.zjol.com.cn/276994.mp4
  14. https://v-cdn.zjol.com.cn/276996.mp4
  15. https://v-cdn.zjol.com.cn/276998.mp4
  16. https://v-cdn.zjol.com.cn/277000.mp4
  17. https://v-cdn.zjol.com.cn/277001.mp4
  18. https://v-cdn.zjol.com.cn/277002.mp4
  19. https://v-cdn.zjol.com.cn/277003.mp4
  20. https://v-cdn.zjol.com.cn/277004.mp4
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
ExoPlayer是一个开源Android音视频播放器,由Google开发和维护。它支持多种音视频格式、网络协议和多种数据源,可以满足各种应用场景的需求。下面是使用ExoPlayer进行音视频播放的基本步骤: 1. 添加ExoPlayer依赖 在项目的build.gradle中添加以下依赖: ``` implementation 'com.google.android.exoplayer:exoplayer-core:2.14.0' implementation 'com.google.android.exoplayer:exoplayer-dash:2.14.0' // 适用于DASH格式视频 implementation 'com.google.android.exoplayer:exoplayer-ui:2.14.0' // 可选依赖,提供了默认的播放器UI组件 ``` 2. 创建ExoPlayer实例 在需要播放视频的Activity或Fragment中创建ExoPlayer实例: ``` SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build(); ``` 3. 准备媒体资源 准备需要播放的媒体资源,可以是本地文件或网络地址。如果是网络资源,需要使用ExtractorMediaSource或DashMediaSource进行数据源的构建: ``` MediaItem mediaItem = MediaItem.fromUri(uri); // uri为需要播放的资源地址 player.setMediaItem(mediaItem); player.prepare(); ``` 4. 开始播放 调用ExoPlayer的start方法即可开始播放: ``` player.play(); ``` 5. 暂停和停止播放 可以通过pause和stop方法实现暂停和停止播放: ``` player.pause(); player.stop(); ``` 6. 释放资源 在Activity或Fragment的生命周期中,需要在onDestroy或onDestroyView方法中释放ExoPlayer资源: ``` player.release(); ``` 以上就是使用ExoPlayer进行音视频播放的基本步骤。需要注意的是,ExoPlayer在播放过程中可能会抛出各种异常,需要在代码中进行处理。同时,ExoPlayer还提供了丰富的参数配置和事件监听接口,可以根据具体需求进行配置和使用

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Kevin-Dev

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值