java - 视频持续时间设置不正确 (exoplayer2)

标签 java android exoplayer2.x

使用exopler2,我可以毫无问题地播放带字幕的视频,但搜索栏不显示任何内容,并且不显示视频时长 如果我删除字幕代码并仅设置视频源,一切正常,但我需要添加副标题 这是我的代码

注意:我使用 m3u8 视频和 srt 作为字幕

enter image description here

    //  Create the player

    player = ExoPlayerFactory.
            newSimpleInstance(this, trackSelector, loadControl);


    // Bind the player to the view.
    //simpleExoPlayerView.setPlayer(player);

    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new
            DefaultDataSourceFactory(this,
            Util.getUserAgent(this,
                    "exoplayer2example"), bandwidthMeter);


    //FOR LIVESTREAM LINK:
    // MediaSource videoSource =new HlsMediaSource(videoUri,dataSourceFactory,1,null,null);
    MediaSource videoSource = new
            HlsMediaSource.Factory(dataSourceFactory).
            createMediaSource(videoUri);

    player.prepare(videoSource);

    //time = player.getDuration();
    // Log.d("TTTTTTT", String.valueOf(player.getDuration()));
    //Build the subtitle MediaSource.
    Format textFormat = Format.createTextSampleFormat(null,
            MimeTypes.APPLICATION_SUBRIP,
            null, Format.NO_VALUE, Format.NO_VALUE,
            "ar", null, Format.NO_VALUE);


    MediaSource subtitleSource = new SingleSampleMediaSource.Factory(
            dataSourceFactory).createMediaSource(subtitleUri,
            textFormat, C.TIME_UNSET);
    //merging the video with subTitle
    MergingMediaSource mergedSource = new MergingMediaSource(subtitleSource, videoSource);
    //prepare video with sub title
    player.prepare(mergedSource);
    //set the player to view
    simpleExoPlayerView.setPlayer(player);
    //auto play
    player.setPlayWhenReady(true);

最佳答案

创建 MergingMediaSource 时 MediaSource 顺序出现问题

MergingMediaSource mergedSource = new MergingMediaSource(subtitleSource, videoSource);

应该是这样的

MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);

关于java - 视频持续时间设置不正确 (exoplayer2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56430306/

相关文章:

java - 在接收器外部的应用程序链接中打开应用程序

android-recyclerview - 在 RecyclerView 中释放并重新初始化 exoplayer 的最佳方式

live-streaming - 跨设备同步 HLS 直播

Java:在Eclipse中运行java文件时执行cmd命令?

java - 如何在 Java 中的 Telegram bot 的 textview 上设置超链接?

java - 将 Java 代码转换为 Kotlin 代码时出错

java - 构建一个简单的Android应用> Android基本编程> Red Rs和缺少资源标签

java - 用 Java 解密 Blowfish/CBC

android - 设置频率属性 RRULE 以运行事件一次

java - 如何在不使用android :configChanges的情况下让ExoPlayer在设备旋转时恢复视频播放