如何 运行 ExternalTimedMetadataTracks 集合中的项目?

How to run items from an ExternalTimedMetadataTracks collection?

我正在开发用于播放视频的 UWP 桌面应用程序。我需要在运行时包含字幕,并且我正在尝试使用 Subtitle 类型的 TimedMetadataTrack 对象。尽管我在 TimedMetadataTrack 对象中包含了几个 TimedTextCue 并将此对象添加到 MediaSource 的 ExternalTimedMetadataTracks 集合中,但在 MediaPlayerElement 上播放视频时没有显示字幕。 什么不见​​了?欢迎任何帮助。谢谢

XAML

<MediaPlayerElement x:Name="mediaPlayerElement" 
                                AutoPlay="False" 
                                Margin="5"
                                Width="640" Height="480"
                                HorizontalAlignment="Center" 
                                AreTransportControlsEnabled="True" />

代码隐藏

        var source = MediaSource.CreateFromUri(new Uri("ms-appx:///Assets/teste.mp4"));

        TimedMetadataTrack metadataTrack = new TimedMetadataTrack("ID_0", "en-us", TimedMetadataKind.Subtitle);

        for (int i = 0; i < 10; i++)
        {
            TimedTextCue ttc = new TimedTextCue();
            TimedTextLine ttl = new TimedTextLine();

            ttl.Text = "This is subtitles line: " + i.ToString() + ".";

            ttc.Id = "ID_" + i;
            ttc.Lines.Add(ttl);
            ttc.StartTime = TimeSpan.FromSeconds((i * 10) + 1);
            ttc.Duration = TimeSpan.FromSeconds(10);

            metadataTrack.AddCue(ttc);
        }

        source.ExternalTimedMetadataTracks.Add(metadataTrack);

        this.mediaPlayerElement.Source = source;

is there a way to select the subtitle via programming, so that the user doesn't have to do this?

您可以创建一个 MediaPlaybackItem Class 作为媒体源,然后您可以调用 MediaPlaybackItem 对象的 SetPresentationMode 方法,传入要切换的轨道的索引,然后提供 TimedMetadataTrackPresentationMode 枚举中的值.

像这样:

            source.ExternalTimedMetadataTracks.Add(metadataTrack);

        MediaPlaybackItem mediaPlaybackItem = new MediaPlaybackItem(source);

        mediaPlayerElement.Source = mediaPlaybackItem;

        mediaPlayerElement.AutoPlay = true;

        mediaPlaybackItem.TimedMetadataTracks.SetPresentationMode(0, TimedMetadataTrackPresentationMode.PlatformPresented);

您可以参考此文档以获取更多信息:Media items, playlists, and tracks