Skip to content

Commit

Permalink
exo subtitle update link #4057
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Nov 20, 2024
1 parent afdb955 commit 5076e56
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
android:layout_width="match_parent"
android:layout_height="@dimen/post_media_height" />

<Button
android:id="@+id/change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/detail_player"
android:layout_margin="10dp"
android:text="exo track txt" />

</RelativeLayout>

0 comments on commit 5076e56

Please sign in to comment.