android exoplayer的使用

一.背景

    最近公司让研究一个exoplayer播放器,所以在网上找了些资料,记录一下。英语不错的同学可以直接看文档( 点击打开链接)    另外附上官方的github地址( https://github.com/google/ExoPlayer)

二.步骤

  1. 添加exoplayer的依赖
  2. 创建simpleExoplayer实例
  3. 将播放器和SimpleExoplayerView结合
  4. 用MediaSource准备播放
  5. 结束播放释放资源

三.具体代码

public class MainActivity extends AppCompatActivity {

    private Context mContext;
    private SimpleExoPlayerView simpleExoPlayerView;
    
    private SimpleExoPlayer simpleExoPlayer;
    private DataSource.Factory dataSourceFactory;
    private EventLogger eventLogger;
    private Handler mainHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        simpleExoPlayerView = (SimpleExoPlayerView) findViewById(R.id.player_view);

        init();
    }

    private void init() {
        // 1 create a default TrackSelector
        BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
        TrackSelection.Factory videoTrackSelectionFactory =
                new AdaptiveTrackSelection.Factory(bandwidthMeter);
        DefaultTrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
        // 打印日志
        eventLogger = new EventLogger(trackSelector);
        // 2. Create the player
        simpleExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, trackSelector);

        // bind the player to the view
        simpleExoPlayerView.setPlayer(simpleExoPlayer);

        // 默认带宽测量
        DefaultBandwidthMeter defaultBandwidthMeter =
                new DefaultBandwidthMeter();
        dataSourceFactory =
                new DefaultDataSourceFactory(mContext,
                        Util.getUserAgent(mContext, "ExoPlayerDemo"), defaultBandwidthMeter);

        Uri uri = Uri.parse("http://devimages.apple.com/samplecode/adDemo/ad.m3u8");

        MediaSource mediaSource = new HlsMediaSource(uri, dataSourceFactory, mainHandler, eventLogger);
        // 准备播放
        simpleExoPlayer.prepare(mediaSource);
        // 自动播放
        simpleExoPlayer.setPlayWhenReady(true);
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (simpleExoPlayer != null) {
            simpleExoPlayer.release();
        }
    }
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.wxj.exoplayer.MainActivity" >

    <com.google.android.exoplayer2.ui.SimpleExoPlayerView
        android:id="@+id/player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
依赖文件,我这里依赖的全部的,其实你可以选择一两个就行
    compile 'com.google.android.exoplayer:exoplayer-core:r2.5.1'
    compile 'com.google.android.exoplayer:exoplayer-dash:r2.5.1'
    compile 'com.google.android.exoplayer:exoplayer-ui:r2.5.1'
    compile 'com.google.android.exoplayer:exoplayer-hls:r2.5.1'
    compile 'com.google.android.exoplayer:exoplayer-smoothstreaming:r2.5.1'

最后不要忘记加网络的权限


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还提供了丰富的参数配置和事件监听接口,可以根据具体需求进行配置和使用
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值