我正在使用Exoplayer播放不同类型的视频,我已经下载了此演示并使用它
https://github.com/google/ExoPlayer
现在我需要为视频添加字幕,我使用的是ExoPlayer 2.9.5版
这是构建媒体源的方法
private MediaSource buildMediaSource(Uri uri, @Nullable String overrideExtension) {
@ContentType int type = Util.inferContentType(uri, overrideExtension);
switch (type) {
case C.TYPE_DASH:
return new DashMediaSource.Factory(dataSourceFactory)
.setManifestParser(
new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri);
case C.TYPE_SS:
return new SsMediaSource.Factory(dataSourceFactory)
.setManifestParser(
new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(dataSourceFactory)
.setPlaylistParserFactory(
new DefaultHlsPlaylistParserFactory(getOfflineStreamKeys(uri)))
.createMediaSource(uri);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
我想这是应该更改为在媒体文件中添加字幕的地方,但是我不知道如何?
字幕也将来自服务器,且格式为.str
任何人都可以帮忙吗?
这是我更新的代码
private MediaSource buildMediaSourceWithSubtitle (Uri uri, @Nullable String overrideExtension, Uri subtitle) {
Format subtitleFormat = Format.createTextSampleFormat(
null, // An identifier for the track. May be null.
MimeTypes.APPLICATION_SUBRIP, // The mime type. Must be set correctly.
0, // Selection flags for the track.
null); // The subtitle language. May be null.
MediaSource subtitleSource =
new SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(subtitle, subtitleFormat, C.TIME_UNSET);
MediaSource mediaSource = null;
@ContentType int type = Util.inferContentType(uri, overrideExtension);
switch (type) {
case C.TYPE_DASH:
mediaSource = new DashMediaSource.Factory(dataSourceFactory)
.setManifestParser(
new FilteringManifestParser<>(new DashManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri);
return new MergingMediaSource(mediaSource, subtitleSource);
case C.TYPE_SS:
mediaSource = new SsMediaSource.Factory(dataSourceFactory)
.setManifestParser(
new FilteringManifestParser<>(new SsManifestParser(), getOfflineStreamKeys(uri)))
.createMediaSource(uri);
return new MergingMediaSource(mediaSource, subtitleSource);
case C.TYPE_HLS:
mediaSource = new HlsMediaSource.Factory(dataSourceFactory)
.setPlaylistParserFactory(
new DefaultHlsPlaylistParserFactory(getOfflineStreamKeys(uri)))
.createMediaSource(uri);
return new MergingMediaSource(mediaSource, subtitleSource);
case C.TYPE_OTHER:
mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri);
return new MergingMediaSource(mediaSource, subtitleSource);
default: {
throw new IllegalStateException("Unsupported type: " + type);
}
}
}
答案 0 :(得分:0)
这实际上取决于您是在侧面加载字幕还是将其嵌入到流中。如果它们是嵌入式的,则HLS,Dash或其他媒体源将检测到轨道,并且您需要选择文本轨道。参见SimpleExoPlayer example
如果您是侧面装载,则需要使用SingleSampleMediaSource和MergingMediaSource。首先获取视频/音频源,然后从srt创建一个SingleSampleMediaSource
,然后使用MergingMediaSource
Example合并两者。完成后,选择上面的文本轨道。