android - Exoplayer 无法仅播放 HLS 音频

标签 android exoplayer

我只有音频链接 https://storage.googleapis.com/as-piepme/1465/livestream/1465.fef238cdc9d9eb0b958d4eb23080e28b/index.m3u8但是Exoplayer无法播放上面的链接, 它返回“源错误”。但IOS和Chrome都运行良好。我的代码:

SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();
player.setMediaItem(MediaItem.fromUri(Uri.parse(linkPlay)));
player.prepare();
player.setPlayWhenReady(true);

更新我的解决方案:

MediaItem mediaItem = new MediaItem.Builder()
                        .setUri(Uri.parse("https://storage.googleapis.com/as-piepme/1465/livestream/1465.fef238cdc9d9eb0b958d4eb23080e28b/index.m3u8"))
                        .build();
                DefaultHlsExtractorFactory defaultHlsExtractorFactory = new DefaultHlsExtractorFactory(DefaultTsPayloadReaderFactory.FLAG_IGNORE_H264_STREAM, false);
                MediaSource mediaSource = new HlsMediaSource.Factory(new DefaultHlsDataSourceFactory(new DefaultDataSourceFactory(context)))
                        .setExtractorFactory(defaultHlsExtractorFactory)
                        .setAllowChunklessPreparation(true)
                        .createMediaSource(mediaItem);
                player.setMediaSource(mediaSource);

最佳答案

您需要使用媒体源。

尝试这个示例代码。

DataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory();
// Create a HLS media source pointing to a playlist uri.
HlsMediaSource hlsMediaSource =
    new HlsMediaSource.Factory(dataSourceFactory)
        .createMediaSource(MediaItem.fromUri(hlsUri));
// Create a player instance.
SimpleExoPlayer player = new SimpleExoPlayer.Builder(context).build();
// Set the media source to be played.
player.setMediaSource(hlsMediaSource);
// Prepare the player.
player.prepare();

有关更多详细信息,请查看documentation .

更新 使用这个。

        DefaultTrackSelector trackSelector = new DefaultTrackSelector(this, new AdaptiveTrackSelection.Factory());
        DefaultTrackSelector.Parameters trackSelectorParameters = trackSelector.buildUponParameters().setMaxAudioChannelCount(0).build();
        trackSelector.setParameters(trackSelectorParameters);
        RenderersFactory renderersFactory = new DefaultRenderersFactory(this);
        SimpleExoPlayer audioPlayer = = new SimpleExoPlayer.Builder(this, renderersFactory).setTrackSelector(trackSelector).build();
        HttpDataSource.Factory factory = new DefaultHttpDataSourceFactory("Online Audio Player", DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
        audioPlayer.setMediaSource(new ProgressiveMediaSource.Factory(factory).createMediaSource(MediaItem.fromUri("https://storage.googleapis.com/as-piepme/1465/livestream/1465.fef238cdc9d9eb0b958d4eb23080e28b/index.m3u8")));
        audioPlayer.prepare();
        audioPlayer.setPlayWhenReady(true);

关于android - Exoplayer 无法仅播放 HLS 音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66687353/

相关文章:

java - Android TV Leanback Exoplayer 视频缩放问题

android - Exoplayer 正在使用 PageTransformer 移除预览

android - 对话框关闭后隐藏软键盘

android - 可以缩小的 2D 可 ScrollView

java - Android Exoplayer - 如何在离线播放时显示可用品质(轨道)

Android Exoplayer ConcatenatingMediaSource 检测第一个源的结尾

Android Exoplayer : What is 'playWhenReady' in "onPlayerStateChanged(boolean playWhenReady, int playbackState)" method

android - 如何以编程方式打开或模拟对使用 XML 创建的 android 首选项的单击?

android - java.lang.unsatisfiedlinkerror :couldn't load testjni: findLibrary returned null

android - Android N 弃用安全 "Crypto"提供程序