android ExoPlayer未从.srt文件加载字幕

egdjgwm8  于 2023-05-27  发布在  Android
关注(0)|答案(3)|浏览(448)

因此,我试图显示字幕从我的.srt文件在exoplayer,但它不工作。我需要使用一个单独的SubtitleView来显示我的字幕吗?PlayerView中的subtitleView是否不够?我使用PlayerView BTW。
我使用的exoplayer版本是2.14.0。
addTextOutput方法。

simpleExoPlayer.addTextOutput(cues -> {
                playerView.getSubtitleView().setCues(cues);
                playerView.getSubtitleView().setVisibility(View.VISIBLE);
                playerView.getSubtitleView().onCues(cues);
                assert cues.get(0).text != null;
                Log.d("subtitles", cues.get(0).text.toString());
            });

我也试着实现到TextOutput,但也没有工作。
我的示例.srt文件的内容:

1
00:00:00,000 --> 00:00:01,500
For www.forom.com

2
00:00:01,500 --> 00:00:02,500
<i>Tonight's the night.</i>

3
00:00:03,000 --> 00:00:15,000
<i>And it's going to happen
again and again --</i>

我用来加载字幕的函数:

public void buildMediaSourceV3(Uri uri){
        String subtitlesUri = sharedPreferencesSubtitles.getString(videoName.getText().toString(), "");

        DataSource.Factory factory = new DefaultDataSourceFactory(VideoPlayer.this, getPackageName(), new DefaultBandwidthMeter());
        MediaItem mediaItem = MediaItem.fromUri(uri);
        MediaSource videoSource = new ProgressiveMediaSource.Factory(factory).createMediaSource(mediaItem);
        if(subtitlesUri.equals(""))
        {
            simpleExoPlayer.addMediaSource(videoSource);
        }
        else
        {
                MediaItem.Subtitle subtitle = new  MediaItem.Subtitle(Uri.parse(subtitlesUri), MimeTypes.APPLICATION_SUBRIP, "en");
                MediaSource textMediaSource = new SingleSampleMediaSource.Factory(factory).createMediaSource(subtitle, C.TIME_UNSET);
                textMediaSource.getMediaItem().mediaMetadata.subtitle.toString();
                MergingMediaSource mergingMediaSource = new MergingMediaSource(videoSource, textMediaSource);
                simpleExoPlayer.addMediaSource(mergingMediaSource);
        }
    }
8ljdwjyq

8ljdwjyq1#

下面是带有SRT字幕的视频/mp4的最小示例。对于XML布局,使用com.google.android.exoplayer2.ui.StyledPlayerView,不需要额外的字幕布局。文件位于项目内的assets目录中。

val playerView = view.findViewById<StyledPlayerView>(R.id.video_pv)
    val exoPlayer = ExoPlayer.Builder(requireActivity()).build()
    playerView.player = exoPlayer

    val assetSrtUri = Uri.parse(("file:///android_asset/subtitle.srt"))
    val subtitle = SubtitleConfiguration.Builder(assetSrtUri)
        .setMimeType(MimeTypes.APPLICATION_SUBRIP)
        .setLanguage("en")
        .setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
        .build()

    val assetVideoUri = Uri.parse(("file:///android_asset/video.mp4"))
    val mediaItem = MediaItem.Builder()
        .setUri(assetVideoUri)
        .setSubtitleConfigurations(ImmutableList.of(subtitle))
        .build()

    exoPlayer.setMediaItem(mediaItem)

    exoPlayer.prepare()
    exoPlayer.play()

Gradle依赖项:

implementation "com.google.android.exoplayer:exoplayer:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-core:2.17.1"
implementation "com.google.android.exoplayer:exoplayer-ui:2.17.1"
elcex8rz

elcex8rz2#

我知道你已经实施了一个变通方案。
我也遇到了同样的问题,并找到了解决方案:

trackSelector.setPreferredTextLanguage

这帮助了我。完整上下文:

val trackSelector = DefaultTrackSelector(context)
trackSelector.setParameters(trackSelector.buildUponParameters().setPreferredTextLanguage("en"))

**更新:**我错了。该解决方案仅显示视频嵌入式字幕,而不是侧载字幕。

这对我来说很有效:

val mediaItemBuilder = MediaItem.Builder()
                .setUri(uri))

videoCaption?.vtt?.let {
    val uriSubtitle = Uri.parse(it)
    val mediaItemSubtitle = MediaItem.Subtitle(uriSubtitle,
                            MimeTypes.TEXT_VTT,
                            "en",
                            C.SELECTION_FLAG_DEFAULT)
            
        
    mediaItemBuilder.setSubtitles(mutableListOf(mediaItemSubtitle))
}
        
val mediaItem = mediaItemBuilder.build()
val mediaSource = DefaultMediaSourceFactory(dataSourceFactory, extractoryFactory).createMediaSource(mediaItem)
ecbunoof

ecbunoof3#

使用生成器添加字幕...

val assetSrtUri = Uri.parse("https:.........")
val subtitle = MediaItem.SubtitleConfiguration.Builder(assetSrtUri)
               .setMimeType(MimeTypes.APPLICATION_SUBRIP)
               .setLanguage("en")
               .setSelectionFlags(C.SELECTION_FLAG_DEFAULT)
               .build()

val firstVideoUri = Uri.parse(intent.getStringExtra(ARG_VIDEO_URL))
val firstItem = MediaItem.Builder().setUri(firstVideoUri)
firstItem.setSubtitleConfigurations(ImmutableList.of(subtitle))
player.addMediaItem(firstItem.build())
player.prepare()

相关问题