exoplayer 播放rtmp流

使用exoplayer 播放rtmp 流,需要添加exoplayer扩展的rtmp库

在项目build.gradle中添加

            exoplayer:'com.google.android.exoplayer:exoplayer:2.9.6',
            exoplayer_core:'com.google.android.exoplayer:exoplayer-core:2.9.6',
            exoplayer_rtmp:'com.google.android.exoplayer:extension-rtmp:2.9.6',

其中exoplayer  版本可以在ExoPlayer版本中查找,exoplayer本体是不支持rtmp流的,在首页点开extensions可以看到exoplayer的扩展,可以发现exoplayer已经支持很多东西了,点开rtmp找到引入方法;直接引入比较方便,建议直接引入,如上述代码;引用编译好之后可以根据Uri 在初始化exoplayer的时候创建对应的DataSource.Factory,如下

ExoPlayer.prepare(buildMediaSource(context, mUri), false, true);
private MediaSource buildMediaSource(Context context, Uri uri) {
        String path = uri.getPath();
        MyLog.v("player- path = " + path);
 if(uri.toString().startsWith("rtmp")){
            MyLog.v("player- rtmp:");
            ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
            return new ExtractorMediaSource.Factory(new RtmpDataSourceFactory())
                    .setExtractorsFactory(extractorsFactory)
                    .createMediaSource(uri);
        }else {
            MyLog.v("player- ExtractorMediaSource");
            return new ExtractorMediaSource.Factory(buildDataSourceFactory(context, true)).createMediaSource(uri);

        }
    }
    private DataSource.Factory buildDataSourceFactory(Context context, boolean useBandwidthMeter) {
        return new DefaultDataSourceFactory(context, !useBandwidthMeter ? null : BANDWIDTH_METER,
                new DefaultHttpDataSourceFactory(Util.getUserAgent(context, "defaultPlayer"),
                        !useBandwidthMeter ? null : BANDWIDTH_METER, 30000, 30000, true));
    }

这样就可以播放RTMP直播流了,

这一篇可结合利用ffmpeg实现rtmp推流直播_ffmpeg推流rtmp-CSDN博客来实现客户端的拉流观看;

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用ExoPlayer播放FLV格式的直播,你需要进行一些额外的配置。以下是一个示例代码: 首先,在你的Android项目的 build.gradle 文件中添加以下依赖项: ```groovy implementation 'com.google.android.exoplayer:exoplayer-core:2.15.1' implementation 'com.google.android.exoplayer:exoplayer-ui:2.15.1' implementation 'com.google.android.exoplayer:extension-rtmp:2.15.1' implementation 'com.google.android.exoplayer:extension-ffmpeg:2.15.1' ``` 接下来,在你的Activity或Fragment中使用以下代码来播放FLV格式的直播: ```java import android.net.Uri; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory; import com.google.android.exoplayer2.upstream.DefaultHttpDataSource; public class MainActivity extends AppCompatActivity { private static final String RTMP_URL = "your_rtmp_url_here"; private SimpleExoPlayer player; private PlayerView playerView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); playerView = findViewById(R.id.player_view); // 创建ExoPlayer实例 player = new SimpleExoPlayer.Builder(this).build(); // 设置播放器视图 playerView.setPlayer(player); // 创建媒体资源 MediaSource mediaSource = buildMediaSource(Uri.parse(RTMP_URL)); // 准备播放 player.prepare(mediaSource); // 开始播放 player.setPlayWhenReady(true); } private MediaSource buildMediaSource(Uri uri) { DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory(this, "exoplayer"); // 使用DefaultHttpDataSourceFactory来处理RTMP DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory() .setAllowCrossProtocolRedirects(true) .setUserAgent("exoplayer"); dataSourceFactory.setHttpDataSourceFactory(httpDataSourceFactory); // 创建ProgressiveMediaSource.Builder来构建媒体资源 ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory); // 设置扩展选项以支持FLV格式 mediaSourceFactory.setCustomCacheKey(RTMP_URL); mediaSourceFactory.setLoadErrorHandlingPolicy(new ExoPlayerLoadErrorHandlingPolicy()); // 创建媒体资源 return mediaSourceFactory.createMediaSource(uri); } @Override protected void onDestroy() { super.onDestroy(); // 释放资源 player.release(); } } ``` 请将 `your_rtmp_url_here` 替换为你要播放RTMP地址。 在这个示例中,我们创建了一个ExoPlayer实例,并将其与一个PlayerView关联起来。然后,我们构建了一个媒体资源,并通过使用 `ProgressiveMediaSource.Builder` 来设置扩展选项以支持FLV格式。最后,我们准备播放并开始播放。 为了支持处理RTMP,我们使用了 `DefaultHttpDataSource.Factory` 来处理HTTP请求。这里设置了一些选项,如允许跨域重定向和自定义用户代理。 为了处理加载错误,我们创建了一个自定义的 `ExoPlayerLoadErrorHandlingPolicy` 类,你需要根据你的需求进行相应的实现。 请注意,这只是一个简单的示例代码,你可以根据你的具体需求进行相应的修改和扩展。 希望这个示例能帮助到你!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值