【问题标题】:Video Duration not set correctly (exoplayer2)视频时长设置不正确(exoplayer2)
【发布时间】:2019-06-03 15:21:48
【问题描述】:

使用 exopler2 播放带字幕的视频没有问题,但是搜索栏没有显示任何内容,并且视频时长没有出现 如果我删除字幕代码并只设置视频源,一切正常,但我需要添加字幕 这是我的代码

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

    //  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);

【问题讨论】:

    标签: java android exoplayer2.x


    【解决方案1】:

    创建 MergingMediaSource 时的 MediaSource 顺序问题

    MergingMediaSource mergedSource = new MergingMediaSource(subtitleSource, videoSource);
    

    应该是这样的

    MergingMediaSource mergedSource = new MergingMediaSource(videoSource, subtitleSource);
    

    【讨论】:

      【解决方案2】:

      Exoplayer 为您提供了非常好的工具来执行此操作 只需将default id - "exo_duration" 设置为您的文本视图,现在您无需执行任何操作

      使用这个 id,exoplayer 自动将视频时长设置为 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"/>
      

      更新: 我忘了提一些关于此的更多信息:

      当您将id's 分配给rewindforward 和其他如上例所示的按钮时,您将不再需要findViewById,甚至不需要设置clickListeners,您甚至不需要t需要申请style(根据我)..

      另一件事:

      使用下面给出的两个按钮很难处理可见性和点击事件:

      <ImageButton android:id="@id/exo_play" />
      
      <ImageButton android:id="@id/exo_pause" />
      

      但有一个解决方案:

      <ImageButton android:id="@id/exo_play_pause" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-14
        • 1970-01-01
        • 1970-01-01
        • 2013-08-18
        • 1970-01-01
        • 1970-01-01
        • 2011-10-17
        • 2020-08-14
        相关资源
        最近更新 更多