gpt4 book ai didi

java - 如何在 Exoplayer android 中向视频添加字幕(.SRT 文件)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:49:17 25 4
gpt4 key购买 nike

我正在做一个项目,我应该在 android 中播放 .srt 文件和视频。我正在研究 Exoplayer 的示例,但无法播放带视频的 .srt 文件。

我使用的代码是,

MediaSource mediaSource = new HlsMediaSource(Uri.parse("https://bitdash-a.akamaihd.net/content/sintel/hls/playlist.m3u8"),
mediaDataSourceFactory, mainHandler, null);

Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
null, Format.NO_VALUE, Format.NO_VALUE, "en", null);

Uri uri = Uri.parse("http://www.storiesinflight.com/js_videosub/jellies.srt");


MediaSource subtitleSource = new SingleSampleMediaSource(uri, mediaDataSourceFactory, textFormat, C.TIME_UNSET);
// Plays the video with the sideloaded subtitle.
MergingMediaSource mergedSource =
new MergingMediaSource(mediaSource, subtitleSource);

player.prepare(mergedSource);

任何人都可以建议我解决这个问题或任何教程链接。非常感谢您的帮助!

最佳答案

我知道现在回答这个问题为时已晚,但如果有人爱上它,试试这个:

public class MainActivity extends AppCompatActivity {
SimpleExoPlayerView exoPlayerView;
SimpleExoPlayer exoPlayer;
String videoURL = "http://blueappsoftware.in/layout_design_android_blog.mp4";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

exoPlayerView = (SimpleExoPlayerView) findViewById(R.id.exo_player_view);
try {


BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelector trackSelector = new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(bandwidthMeter));
exoPlayer = ExoPlayerFactory.newSimpleInstance(this, trackSelector);

Uri videoURI = Uri.parse(videoURL);
Uri subtitleUri=Uri.parse("https://firebasestorage.googleapis.com/v0/b/findandfix-2f4a9.appspot.com/o/Despacito%20Remix%20Luis%20Fonsi%20ft.Daddy%20Yankee%20Justin%20Bieber%20Lyrics%20%5BSpanish%5D.srt?alt=media&token=63344d04-af1c-4e2c-9d15-381bf7159308");
DefaultHttpDataSourceFactory dataSourceFactory = new DefaultHttpDataSourceFactory("exoplayer_video");

ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
MediaSource mediaSource = new ExtractorMediaSource(videoURI, dataSourceFactory, extractorsFactory, null, null);

// Build the subtitle MediaSource.
Format subtitleFormat = Format.createTextSampleFormat(
null, // An identifier for the track. May be null.
MimeTypes.APPLICATION_SUBRIP, // The mime type. Must be set correctly.
null,
Format.NO_VALUE,
Format.NO_VALUE,
"en",
null); // The subtitle language. May be null.

MediaSource subtitleSource =new SingleSampleMediaSource(subtitleUri, dataSourceFactory, subtitleFormat, C.TIME_UNSET);

MergingMediaSource mergedSource =
new MergingMediaSource(mediaSource, subtitleSource);

exoPlayerView.setPlayer(exoPlayer);
exoPlayer.prepare(mergedSource);
exoPlayer.setPlayWhenReady(true);
}catch (Exception e){
Log.e("MainAcvtivity"," exoplayer error "+ e.toString());
}

}

关于java - 如何在 Exoplayer android 中向视频添加字幕(.SRT 文件)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42344779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com