From 5076e560c9dc3f8f47d250b8d2810a8380a6d37e Mon Sep 17 00:00:00 2001 From: guoshuyu <359369982@qq.com> Date: Wed, 20 Nov 2024 16:52:08 +0800 Subject: [PATCH] exo subtitle update link #4057 --- .../example/gsyvideoplayer/DetailPlayer.java | 1 - .../GSYExoSubTitleDetailPlayer.java | 85 +++++++++++++++++-- .../exosubtitle/GSYExoSubTitlePlayer.java | 2 + .../exosubtitle/GSYExoSubTitleVideoView.java | 2 +- .../activity_detail_exo_subtitle_player.xml | 8 ++ 5 files changed, 90 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/example/gsyvideoplayer/DetailPlayer.java b/app/src/main/java/com/example/gsyvideoplayer/DetailPlayer.java index 1acc691a7..519dc0aa1 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/DetailPlayer.java +++ b/app/src/main/java/com/example/gsyvideoplayer/DetailPlayer.java @@ -179,7 +179,6 @@ public void onPrepared(String url, Object... objects) { } - //设置 seek 的临近帧。 if (binding.detailPlayer.getGSYVideoManager().getPlayer() instanceof Exo2PlayerManager) { IjkExo2MediaPlayer player = ((IjkExo2MediaPlayer) binding.detailPlayer.getGSYVideoManager().getPlayer().getMediaPlayer()); MappingTrackSelector.MappedTrackInfo mappedTrackInfo = player.getTrackSelector().getCurrentMappedTrackInfo(); diff --git a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleDetailPlayer.java b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleDetailPlayer.java index f1f019a37..cb2af7738 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleDetailPlayer.java +++ b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleDetailPlayer.java @@ -4,12 +4,17 @@ import android.os.Bundle; import android.view.View; import android.widget.ImageView; -import android.widget.RelativeLayout; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; -import androidx.core.widget.NestedScrollView; +import androidx.media3.common.C; +import androidx.media3.common.TrackGroup; +import androidx.media3.common.TrackSelectionOverride; +import androidx.media3.common.TrackSelectionParameters; import androidx.media3.exoplayer.SeekParameters; +import androidx.media3.exoplayer.source.TrackGroupArray; +import androidx.media3.exoplayer.trackselection.MappingTrackSelector; +import androidx.media3.exoplayer.trackselection.TrackSelector; import com.example.gsyvideoplayer.R; import com.example.gsyvideoplayer.databinding.ActivityDetailExoSubtitlePlayerBinding; @@ -24,7 +29,7 @@ import java.util.HashMap; import java.util.Map; -import tv.danmaku.ijk.media.exo2.Exo2PlayerManager; +import tv.danmaku.ijk.media.exo2.IjkExo2MediaPlayer; public class GSYExoSubTitleDetailPlayer extends AppCompatActivity { @@ -101,10 +106,35 @@ public void onPrepared(String url, Object... objects) { isPlay = true; //设置 seek 的临近帧。 - if (binding.detailPlayer.getGSYVideoManager().getPlayer() instanceof Exo2PlayerManager) { - ((Exo2PlayerManager) binding.detailPlayer.getGSYVideoManager().getPlayer()).setSeekParameter(SeekParameters.NEXT_SYNC); + if (binding.detailPlayer.getGSYVideoManager().getPlayer() instanceof GSYExoSubTitlePlayerManager) { + ((GSYExoSubTitlePlayerManager) binding.detailPlayer.getGSYVideoManager().getPlayer()).setSeekParameter(SeekParameters.NEXT_SYNC); Debuger.printfError("***** setSeekParameter **** "); } + + + ///TODO 注意,用这个 M3U8 的话,内部会有内嵌字幕 embedded caption + ///TODO 所以就算你加了外挂字幕,也需要再切换一次才能看到外部字幕 + ///TODO 这里输出所有字幕信息 + if (binding.detailPlayer.getGSYVideoManager().getPlayer() instanceof GSYExoSubTitlePlayerManager) { + IjkExo2MediaPlayer player = ((IjkExo2MediaPlayer) binding.detailPlayer.getGSYVideoManager().getPlayer().getMediaPlayer()); + MappingTrackSelector.MappedTrackInfo mappedTrackInfo = player.getTrackSelector().getCurrentMappedTrackInfo(); + if (mappedTrackInfo != null) { + for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) { + TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(i); + if (C.TRACK_TYPE_TEXT == mappedTrackInfo.getRendererType(i)) { //判断是否是音轨 + for (int j = 0; j < rendererTrackGroups.length; j++) { + TrackGroup trackGroup = rendererTrackGroups.get(j); + Debuger.printfError("####### Text " + trackGroup.getFormat(0).toString() + " #######"); + } + } + } + } + } + + + + + } @Override @@ -166,6 +196,43 @@ public void onClick(View v) { } }); + + ///exo 切换音轨 + binding.change.setOnClickListener(new View.OnClickListener() { + int index = 0; + + @Override + public void onClick(View view) { + + ///TODO 注意,DEMO 的 getUrl 如果 M3U8 的话,内部可能会有内嵌字幕 embedded caption + ///TODO 所以就算你加了外挂字幕,也需要再切换一次才能看到外部字幕 + if (binding.detailPlayer.getGSYVideoManager().getPlayer() instanceof GSYExoSubTitlePlayerManager) { + IjkExo2MediaPlayer player = ((IjkExo2MediaPlayer) binding.detailPlayer.getGSYVideoManager().getPlayer().getMediaPlayer()); + TrackSelector trackSelector = player.getTrackSelector(); + MappingTrackSelector.MappedTrackInfo mappedTrackInfo = player.getTrackSelector().getCurrentMappedTrackInfo(); + + if (mappedTrackInfo != null) { + for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) { + TrackGroupArray rendererTrackGroups = mappedTrackInfo.getTrackGroups(i); + if (C.TRACK_TYPE_TEXT == mappedTrackInfo.getRendererType(i)) { //判断是否是音轨 + if (index == 0) { + index = 1; + } else { + index = 0; + } + if (rendererTrackGroups.length <= 1) { + return; + } + TrackGroup trackGroup = rendererTrackGroups.get(index); + TrackSelectionParameters parameters = trackSelector.getParameters().buildUpon().setForceHighestSupportedBitrate(true).setOverrideForType(new TrackSelectionOverride(trackGroup, 0)).build(); + trackSelector.setParameters(parameters); + } + } + } + } + } + }); + binding.detailPlayer.setSubTitle("http://img.cdn.guoshuyu.cn/subtitle2.srt"); } @@ -239,6 +306,12 @@ private GSYVideoPlayer getCurPlay() { private String getUrl() { - return "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4"; + + ///该 MP4 没有内嵌字幕 + //return "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4"; + + ///TODO 注意,用这个 M3U8 的话,内部会有内嵌字幕 embedded caption + ///TODO 所以就算你加了外挂字幕,也需要再切换一次才能看到外部字幕 + return "http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear3/prog_index.m3u8"; } } diff --git a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitlePlayer.java b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitlePlayer.java index 6617ae0bf..3a68f65fb 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitlePlayer.java +++ b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitlePlayer.java @@ -137,6 +137,8 @@ public void run() { public MediaSource getTextSource(Uri subTitle) { //todo C.SELECTION_FLAG_AUTOSELECT language MimeTypes + ///TODO 注意,如果原来视频有内嵌字幕,例如 M3U8 embedded caption + ///TODO 所以就算你加了外挂字幕,也需要再切换一次 track 才能看到外部字幕,具体看 GSYExoSubTitleDetailPlayer Format format = new Format.Builder() /// 其他的比如 text/x-ssa ,text/vtt,application/ttml+xml 等等 .setSampleMimeType(MimeTypes.APPLICATION_SUBRIP) diff --git a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleVideoView.java b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleVideoView.java index b8409a253..da3269856 100644 --- a/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleVideoView.java +++ b/app/src/main/java/com/example/gsyvideoplayer/exosubtitle/GSYExoSubTitleVideoView.java @@ -46,7 +46,7 @@ protected void init(Context context) { mSubtitleView = findViewById(R.id.sub_title_view); - mSubtitleView.setStyle(new CaptionStyleCompat(Color.RED, Color.TRANSPARENT, Color.TRANSPARENT, CaptionStyleCompat.EDGE_TYPE_NONE, CaptionStyleCompat.EDGE_TYPE_NONE, null)); + mSubtitleView.setStyle(new CaptionStyleCompat(Color.GREEN, Color.TRANSPARENT, Color.TRANSPARENT, CaptionStyleCompat.EDGE_TYPE_NONE, CaptionStyleCompat.EDGE_TYPE_NONE, null)); mSubtitleView.setFixedTextSize(TypedValue.COMPLEX_UNIT_DIP, 14); } diff --git a/app/src/main/res/layout/activity_detail_exo_subtitle_player.xml b/app/src/main/res/layout/activity_detail_exo_subtitle_player.xml index dac459474..ff97434af 100644 --- a/app/src/main/res/layout/activity_detail_exo_subtitle_player.xml +++ b/app/src/main/res/layout/activity_detail_exo_subtitle_player.xml @@ -34,4 +34,12 @@ android:layout_width="match_parent" android:layout_height="@dimen/post_media_height" /> +