视频时长设置不正确(exoplayer2)

时间:2019-06-03 15:21:48

标签: 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);

2 个答案:

答案 0 :(得分:1)

创建MergingMediaSource时MediaSource顺序出现问题

MergingMediaSource mergedSource = new MergingMediaSource(subtitleSource, videoSource);

应该是这样

MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);

答案 1 :(得分:0)

Exoplayer为您提供了很好的设施 只需将这个id-“ exo_duration”设置为您的textview,现在您无需执行任何操作

exoplayer使用此ID,自动将视频时长设置为textview。

我也在发布一些代码,以节省您的Exo播放器时间

<ImageButton android:id="@id/exo_prev"
style="@style/ExoMediaButton.Previous"/>

<ImageButton android:id="@id/exo_rew"
style="@style/ExoMediaButton.Rewind"/>

<ImageButton android:id="@id/exo_play"
style="@style/ExoMediaButton.Play"/>

<ImageButton android:id="@id/exo_pause"
style="@style/ExoMediaButton.Pause"/>

<ImageButton android:id="@id/exo_ffwd"
style="@style/ExoMediaButton.FastForward"/>

<ImageButton android:id="@id/exo_next"
style="@style/ExoMediaButton.Next"/>