我知道可以用MediaElement
查看外部字幕,但我不知道该怎么做。谁能解释一下如何用MediaElement
连接字幕。
到目前为止,这只是我所知道的线索。但是如何告诉MediaElement
使用它呢?
TimedTextSource.CreateFromUri(new Uri("ms-appx:///TheVideoTitle.srt"), "en");
我从Microsoft网站上找到了许多有关此的信息: https://docs.microsoft.com/en-us/uwp/api/windows.media.core.timedtextsource(编辑的www网址。我偶然粘贴了错误的网址) 但他们没有明确的例子。
更新
我创建了以下代码。它开始播放视频,但仍然缺少字幕。有什么问题。 MediaElement已在XAML中这样创建:
<MediaPlayerElement x:Name="MyVideoPlayer" Stretch="Fill"/>
此代码仍不起作用:
//Get MediaPlayerElement
MediaPlayerElement VideoPlayer = 'THE MEDIAPLAYER ELEMENT IN XAML';
//Get video file StorageFile
StorageFile VideoStorageFile = await StorageFile.GetFileFromPathAsync(GetMultimediaPlayer(ID).LocalVideoInformation.TitleFilePathOnStorage);
//Create MediaSource using VideoStorageFile as source
MediaSource VideoMediaSource = MediaSource.CreateFromStorageFile(VideoStorageFile);
//Create subtitle Uri
Uri SubtitleUri = new Uri("ms-appx:///TheTestSubtitle.srt");
//Set SubtitleUri as source of TimedTextSource
TimedTextSource SubtitleTimedTextSource = TimedTextSource.CreateFromUri(SubtitleUri);
//Add TimedTextSource (SubtitleTimedTextSource) to MediaSource (VideoMediaSource)
VideoMediaSource.ExternalTimedTextSources.Add(SubtitleTimedTextSource);
//Set MediaPlayerElement (VideoPlayer) source
VideoPlayer.Source = VideoMediaSource;
//Play
VideoPlayer.MediaPlayer.Play();
确定...试图从GibHub中的示例代码复制,但是以下代码仍然无法正常工作。 Tts-Resolved
args.Error
出现在空白中。 (系统找不到文件指定字段。)想知道是什么问题:所有srt文件等都与示例应用程序完全位于同一文件夹中。
public sealed partial class MainPage : Page
{
private Dictionary<TimedTextSource, Uri> ttsMap = new Dictionary<TimedTextSource, Uri>();
public MainPage()
{
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
var source = MediaSource.CreateFromUri(new Uri("https://mediaplatstorage1.blob.core.windows.net/windows-universal-samples-media/elephantsdream-clip-h264_sd-aac_eng-aac_spa-aac_eng_commentary.mp4"));
var ttsEnUri = new Uri("ms-appx:///Assets/Media/ElephantsDream-Clip-SRT_en.srt");
var ttsEn = TimedTextSource.CreateFromUri(ttsEnUri);
ttsMap[ttsEn] = ttsEnUri;
var ttsPtUri = new Uri("ms-appx:///Assets/Media/ElephantsDream-Clip-SRT_pt.srt");
var ttsPt = TimedTextSource.CreateFromUri(ttsPtUri);
ttsMap[ttsPt] = ttsPtUri;
var ttsSvUri = new Uri("ms-appx:///Assets/Media/ElephantsDream-Clip-SRT_sv.srt");
var ttsSv = TimedTextSource.CreateFromUri(ttsSvUri);
ttsMap[ttsSv] = ttsSvUri;
ttsEn.Resolved += Tts_Resolved;
ttsPt.Resolved += Tts_Resolved;
ttsSv.Resolved += Tts_Resolved;
source.ExternalTimedTextSources.Add(ttsEn);
source.ExternalTimedTextSources.Add(ttsPt);
source.ExternalTimedTextSources.Add(ttsSv);
var playbackItem = new MediaPlaybackItem(source);
playbackItem.TimedMetadataTracksChanged += (item, args) =>
{
playbackItem.TimedMetadataTracks.SetPresentationMode(0, TimedMetadataTrackPresentationMode.PlatformPresented);
};
mediaPlayerElement.Source = playbackItem;
}
private void Tts_Resolved(TimedTextSource sender, TimedTextSourceResolveResultEventArgs args)
{
var ttsUri = ttsMap[sender];
if (args.Error != null)
{
return;
}
var ttsUriString = ttsUri.AbsoluteUri;
if (ttsUriString.Contains("_en"))
args.Tracks[0].Label = "English";
else if (ttsUriString.Contains("_pt"))
args.Tracks[0].Label = "Portuguese";
else if (ttsUriString.Contains("_sv"))
args.Tracks[0].Label = "Swedish";
}
}
}
答案 0 :(得分:0)
您必须将定时文本源添加到ExternalTimedTextSources
的MediaSource
集合中:
var source = MediaSource.CreateFromUri(mediaUri);
var timedTextSource = TimedTextSource.CreateFromUri(timedTextUri);
source.ExternalTimedTextSources.Add(timedTextSource);
有关完整的工作示例,请参见this Windows Universal sample on GitHub。