Skip to content

Commit

Permalink
link #4057
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Nov 20, 2024
1 parent 26df80f commit 9444155
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 28 deletions.
20 changes: 20 additions & 0 deletions app/lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright 2021 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<lint>
<issue id="UnsafeOptInUsageError">
<option name="opt-in" value="androidx.media3.common.util.UnstableApi" />
</issue>
</lint>
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void onClick(View v) {
}
});

binding.detailPlayer.setSubTitle("http://img.cdn.guoshuyu.cn/subtitle.srt");
binding.detailPlayer.setSubTitle("http://img.cdn.guoshuyu.cn/subtitle2.srt");
}

@Override
Expand Down Expand Up @@ -239,6 +239,6 @@ private GSYVideoPlayer getCurPlay() {


private String getUrl() {
return "http://devimages.apple.com.edgekey.net/streaming/examples/bipbop_4x3/gear3/prog_index.m3u8";
return "https://pointshow.oss-cn-hangzhou.aliyuncs.com/McTk51586843620689.mp4";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.MergingMediaSource;
import androidx.media3.exoplayer.source.SingleSampleMediaSource;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import androidx.media3.exoplayer.trackselection.DefaultTrackSelector;
import androidx.media3.exoplayer.upstream.DefaultBandwidthMeter;
import androidx.media3.extractor.Extractor;
import androidx.media3.extractor.ExtractorsFactory;
import androidx.media3.extractor.text.DefaultSubtitleParserFactory;
import androidx.media3.extractor.text.SubtitleExtractor;
import androidx.media3.extractor.text.SubtitleParser;

public class GSYExoSubTitlePlayer extends IjkExo2MediaPlayer {

Expand Down Expand Up @@ -110,31 +115,40 @@ public void run() {

public MediaSource getTextSource(Uri subTitle) {
//todo C.SELECTION_FLAG_AUTOSELECT language MimeTypes
Format textFormat = new Format.Builder()
/// 其他的比如 text/x-ssa ,text/vtt,application/ttml+xml 等等
.setSampleMimeType(MimeTypes.APPLICATION_MEDIA3_CUES)
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
/// 如果出现字幕不显示,可以通过修改这个语音去对应,
// 这个问题在内部的 selectTextTrack 时,TextTrackScore 通过 getFormatLanguageScore 方法判断语言获取匹配不上
// 就会不出现字幕
.setLanguage("zh-cn")
.build();

MediaItem.SubtitleConfiguration subtitle = new MediaItem.SubtitleConfiguration.Builder(subTitle)
.setMimeType(checkNotNull(textFormat.sampleMimeType))
.setLanguage( textFormat.language)
.setSelectionFlags(textFormat.selectionFlags).build();

DefaultHttpDataSource.Factory factory = new DefaultHttpDataSource.Factory()
.setAllowCrossProtocolRedirects(true)
.setConnectTimeoutMs(50000)
.setReadTimeoutMs(50000)
.setTransferListener( new DefaultBandwidthMeter.Builder(mAppContext).build());

MediaSource textMediaSource = new SingleSampleMediaSource.Factory(new DefaultDataSource.Factory(mAppContext,
factory))
.createMediaSource(subtitle, C.TIME_UNSET);
return textMediaSource;
Format format = new Format.Builder()
/// 其他的比如 text/x-ssa ,text/vtt,application/ttml+xml 等等
.setSampleMimeType(MimeTypes.APPLICATION_SUBRIP)
.setSelectionFlags(C.SELECTION_FLAG_FORCED)
/// 如果出现字幕不显示,可以通过修改这个语音去对应,
// 这个问题在内部的 selectTextTrack 时,TextTrackScore 通过 getFormatLanguageScore 方法判断语言获取匹配不上
// 就会不出现字幕
.setLanguage(null)
.build();

MediaItem.SubtitleConfiguration subtitle = new MediaItem.SubtitleConfiguration.Builder(subTitle)
.setMimeType(checkNotNull(format.sampleMimeType))
.setLanguage(format.language)
.setSelectionFlags(format.selectionFlags).build();

DefaultHttpDataSource.Factory factory = new DefaultHttpDataSource.Factory()
.setAllowCrossProtocolRedirects(true)
.setConnectTimeoutMs(50000)
.setReadTimeoutMs(50000)
.setTransferListener(new DefaultBandwidthMeter.Builder(mAppContext).build());

SubtitleParser.Factory subtitleParserFactory = new DefaultSubtitleParserFactory();

ExtractorsFactory extractorsFactory =
() ->
new Extractor[]{
new SubtitleExtractor(subtitleParserFactory.create(format), format)
};
ProgressiveMediaSource.Factory progressiveMediaSourceFactory =
new ProgressiveMediaSource.Factory(new DefaultDataSource.Factory(mAppContext,
factory), extractorsFactory);

return progressiveMediaSourceFactory.createMediaSource(
MediaItem.fromUri(subtitle.uri.toString()));

}

Expand Down

0 comments on commit 9444155

Please sign in to comment.