//<SnippetDataCueEntered> private void MetadataTrack_DataCueEntered(TimedMetadataTrack sender, MediaCueEventArgs args) { DataCue cue = (DataCue)args.Cue; string data = System.Text.Encoding.Unicode.GetString(cue.Data.ToArray()); System.Diagnostics.Debug.WriteLine("Cue entered: " + data); }
private void metadata_id3_CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); object customValue = null; timedMetadataTrack.PlaybackItem.Source.CustomProperties.TryGetValue("contentId", out customValue); string contentId = (string)customValue; logMsg.AppendLine($"{contentId} ID3 DataCue CueEntered raised."); var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null && dataCue.Data.Length >= 10) { // The payload is the raw ID3 bytes found in a TS stream // Ref: http://id3.org/id3v2.4.0-structure var dr = DataReader.FromBuffer(dataCue.Data); var header_ID3 = dr.ReadString(3); var header_version_major = dr.ReadByte(); var header_version_minor = dr.ReadByte(); var header_flags = dr.ReadByte(); var header_tagSize = dr.ReadUInt32(); // TODO: Use the ID3 bytes for something useful. logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {dataCue.Id}, StartTime: {dataCue.StartTime}, Duration: {dataCue.Duration}"); logMsg.Append($"{header_ID3}, {header_version_major}.{header_version_minor}, {header_flags}, {header_tagSize}"); } Log(logMsg.ToString()); }
private void AddTimedMetaDataTrack_Data() { //<SnippetAddDataTrack> TimedMetadataTrack metadataTrack = new TimedMetadataTrack("ID_0", "en-us", TimedMetadataKind.Data); metadataTrack.Label = "Custom data track"; metadataTrack.CueEntered += MetadataTrack_DataCueEntered; metadataTrack.CueExited += MetadataTrack_CueExited; // Example cue data string data = "Cue data"; byte[] bytes = new byte[data.Length * sizeof(char)]; System.Buffer.BlockCopy(data.ToCharArray(), 0, bytes, 0, bytes.Length); Windows.Storage.Streams.IBuffer buffer = bytes.AsBuffer(); for (int i = 0; i < 10; i++) { DataCue cue = new DataCue() { Id = "ID_" + i, Data = buffer, StartTime = TimeSpan.FromSeconds(3 + i * 3), Duration = TimeSpan.FromSeconds(2) }; metadataTrack.AddCue(cue); } mediaSource.ExternalTimedMetadataTracks.Add(metadataTrack); //</SnippetAddDataTrack> }
private async void AddSubtitle(StorageFile file) { IRandomAccessStream strSource = await file.OpenReadAsync(); var ttsUri = new Uri(file.Path); var ttsPicked = TimedTextSource.CreateFromStream(strSource); ttsMap[ttsPicked] = ttsUri; ttsPicked.Resolved += TtsEn_Resolved; playbackItem.Source.ExternalTimedTextSources.Add(ttsPicked); // Present the first track playbackItem.TimedMetadataTracksChanged += (item, args) => { Debug.WriteLine($"TimedMetadataTracksChanged, Number of tracks: {item.TimedMetadataTracks.Count}"); uint changedTrackIndex = args.Index; TimedMetadataTrack changedTrack = playbackItem.TimedMetadataTracks[(int)changedTrackIndex]; // keeping 0 as index in below line will make the first subtitle that was found to be the activated on playback playbackItem.TimedMetadataTracks.SetPresentationMode(0, TimedMetadataTrackPresentationMode.PlatformPresented); var activeCues = playbackItem.TimedMetadataTracks[0].ActiveCues; var itr = activeCues.GetEnumerator(); while (itr.MoveNext()) { Debug.WriteLine("current : " + itr.Current); } playbackItem.TimedMetadataTracks[0].CueEntered += TimedText_CueEntered; playbackItem.TimedMetadataTracks[0].CueExited += TimedText_CueExited; }; }
private void metadata_extm3u_CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); object customValue = null; timedMetadataTrack.PlaybackItem.Source.CustomProperties.TryGetValue("contentId", out customValue); string contentId = (string)customValue; logMsg.AppendLine($"{contentId} M3U8 DataCue CueEntered raised."); var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null) { // The payload is a UTF-16 Little Endian null-terminated string. // It is any comment line in a manifest that is not part of the HLS spec. var dr = DataReader.FromBuffer(dataCue.Data); dr.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE; var m3u8Comment = dr.ReadString(dataCue.Data.Length / 2 - 1); logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {dataCue.Id}, StartTime: {dataCue.StartTime}, Duration: {dataCue.Duration}"); logMsg.Append(m3u8Comment); // TODO: Use the m3u8Comment string for something useful. } Log(logMsg.ToString()); }
// <SnippetGoToNextChapter> private void GoToNextChapter(MediaPlayer player, MediaPlaybackItem item) { // Find the chapters track if one exists TimedMetadataTrack chapterTrack = item.TimedMetadataTracks.FirstOrDefault(track => track.TimedMetadataKind == TimedMetadataKind.Chapter); if (chapterTrack == null) { return; } // Find the first chapter that starts after current playback position TimeSpan currentPosition = player.PlaybackSession.Position; foreach (ChapterCue cue in chapterTrack.Cues) { if (cue.StartTime > currentPosition) { // Change player position to chapter start time player.PlaybackSession.Position = cue.StartTime; // Display chapter name ChapterTitleTextBlock.Text = cue.Title; break; } } }
protected override void OnNavigatedTo(NavigationEventArgs e) { // Create the playback item var source = MediaSource.CreateFromUri(rootPage.CaptionedMediaUri); msLogger = new MediaSourceLogger(LoggerControl, source); var playbackItem = new MediaPlaybackItem(source); mpiLogger = new MediaPlaybackItemLogger(LoggerControl, playbackItem); // Turn on English captions by default playbackItem.TimedMetadataTracksChanged += (item, args) => { if (args.CollectionChange == CollectionChange.ItemInserted) { LoggerControl.Log($"TimedMetadataTracksChanged, Number of tracks: {item.TimedMetadataTracks.Count}"); uint changedTrackIndex = args.Index; TimedMetadataTrack changedTrack = playbackItem.TimedMetadataTracks[(int)changedTrackIndex]; if (changedTrack.Language == "en") { playbackItem.TimedMetadataTracks.SetPresentationMode(changedTrackIndex, TimedMetadataTrackPresentationMode.PlatformPresented); } } }; // Set the source to begin playback of the item this.mediaPlayerElement.Source = playbackItem; LoggerControl.Log($"Loaded: {rootPage.CaptionedMediaUri}"); }
//</SnippetDataCueEntered> private void AddTimedMetaDataTrack_Text() { //<SnippetAddTextTrack> TimedMetadataTrack metadataTrack = new TimedMetadataTrack("TrackID_0", "en-us", TimedMetadataKind.Caption); metadataTrack.Label = "Custom text track"; metadataTrack.CueEntered += MetadataTrack_TextCueEntered; for (int i = 0; i < 10; i++) { TimedTextCue cue = new TimedTextCue() { Id = "TextCueID_" + i, StartTime = TimeSpan.FromSeconds(i * 3), Duration = TimeSpan.FromSeconds(2) }; cue.Lines.Add(new TimedTextLine() { Text = "This is a custom timed text cue." }); metadataTrack.AddCue(cue); } mediaSource.ExternalTimedMetadataTracks.Add(metadataTrack); //</SnippetAddTextTrack> }
private void UnregisterMetadataHandlers(TimedMetadataTrack timedTrack) { if (timedTrack.Label == "Tracking Events") { timedTrack.CueEntered -= metadata_TrackingEvents_CueEntered; } }
private void TimedText_CueEntered(TimedMetadataTrack sender, MediaCueEventArgs args) { Debug.WriteLine("Cue Entered"); Debug.WriteLine("Cue start time was " + args.Cue.StartTime + " " + args.Cue.Duration); start += args.Cue.StartTime; args.Cue.StartTime = start; Debug.WriteLine("Cue start time is " + args.Cue.StartTime + " " + args.Cue.Duration); }
private void MetadataTrack_DataCueEntered(TimedMetadataTrack sender, MediaCueEventArgs args) { var playbackList = _mediaPlayer.Source as MediaPlaybackList; DataCue cue = (DataCue)args.Cue; OnPlaybackCueTriggered(new PlaybackCueTriggeredEventArgs() { Segment = (int)playbackList.CurrentItemIndex, EventId = Convert.ToInt32(cue.Id), Time = cue.StartTime }); }
/// <summary> /// This function executes when a SpeechCue is hit and calls the functions to update the UI /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="args">the arguments associated with the event.</param> private async void metadata_SpeechCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { SpeakProgress?.Invoke((int)cue.StartPositionInInput, (int)cue.EndPositionInInput - (int)cue.StartPositionInInput + 1); } } }
/// <summary> /// This function executes when a SpeechCue is hit and calls the functions to update the UI /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="cue">the SpeechCue object.</param> private void FillTextBoxes(SpeechCue cue, TimedMetadataTrack timedMetadataTrack) { //if it is a sentence cue, populate the sentence text box. if (timedMetadataTrack.Id == "SpeechSentence") { textBoxLastSpeechSentence.Text = cue.Text; } //if it is a word cue, populate the word text box if (timedMetadataTrack.Id == "SpeechWord") { textBoxLastSpeechWord.Text = cue.Text; } }
private void metadata_SpeechWordCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { // Do something with the cue System.Diagnostics.Debug.WriteLine($"{cue.StartPositionInInput} - {cue.EndPositionInInput}: {inputText.Substring((int)cue.StartPositionInInput, ((int)cue.EndPositionInInput - (int)cue.StartPositionInInput) + 1)}"); } } }
// </SnippetRegisterMetadataHandlerForEXTM3UCues> // <SnippetEXTM3UCueEntered> private void metadata_EXTM3UCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null) { // The payload is a UTF-16 Little Endian null-terminated string. // It is any comment line in a manifest that is not part of the HLS spec. var dr = Windows.Storage.Streams.DataReader.FromBuffer(dataCue.Data); dr.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf16LE; var m3uComment = dr.ReadString(dataCue.Data.Length / 2 - 1); System.Diagnostics.Debug.WriteLine(m3uComment); } }
private void metadata_TrackingEvents_CueExited(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); var contentId = (Guid)timedMetadataTrack.PlaybackItem.Source.CustomProperties["contentId"]; logMsg.AppendLine($"{contentId} TrackingEventCue CueExited raised."); var trackingEventCue = args.Cue as TrackingEventCue; if (trackingEventCue != null) { logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {trackingEventCue.Id}, StartTime: {trackingEventCue.StartTime}, Duration: {trackingEventCue.Duration}"); logMsg.Append($"{trackingEventCue.TrackingEventUri}"); } Log(logMsg.ToString()); }
// </SnippetRegisterMetadataHandlerForChapterCues> // <SnippetChapterCueEntered> private async void metadata_ChapterCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Chapter) { var cue = args.Cue as ChapterCue; if (cue != null) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { ChapterTitleTextBlock.Text = cue.Title; }); } } }
public async Task <bool> LoadAudio() { // If there is no audio, collapse the MediaPlayer. if (CurrentSession.Segments <= 0) { MediaPlayerVisibility = Visibility.Collapsed; return(false); } try { var localFolder = Windows.Storage.ApplicationData.Current.LocalFolder; var sessionFolder = await localFolder.GetFolderAsync(CurrentSession.Id.ToString()); _playbackList = new MediaPlaybackList(); for (var segment = 0; segment < CurrentSession.Segments; segment++) { var file = await sessionFolder.GetFileAsync($"{CurrentSession.Id}_{segment}.mp4"); if (file == null) { this.LogMessage(RECORD_LOG_NAME, $"LoadAudio() failed because Exception at GetFileFromPathAsync. {file.Path}"); } var source = MediaSource.CreateFromStorageFile(file); TimedMetadataTrack metadataTrack = new TimedMetadataTrack(file.Path.ToString(), "en-us", TimedMetadataKind.Data); metadataTrack.Label = "Custom data track"; metadataTrack.CueEntered += MetadataTrack_DataCueEntered; source.ExternalTimedMetadataTracks.Add(metadataTrack); var playbackItem = new MediaPlaybackItem(source); playbackItem.TimedMetadataTracks.SetPresentationMode(0, TimedMetadataTrackPresentationMode.ApplicationPresented); _playbackList.Items.Add(playbackItem); } _mediaPlayer.Source = _playbackList; } catch (Exception e) { this.LogMessage(RECORD_LOG_NAME, "LoadAudio() failed because Exception loading files: " + e.Message); return(false); } return(true); }
/// <summary> /// This function executes when a SpeechCue is hit and calls then <see cref="EhSpeechProgress(int)"/> in UI context. /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="args">the arguments associated with the event.</param> private async void EhCueReceived(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { // System.Diagnostics.Debug.WriteLine($"Cue text:[{cue.Text}], Access={Dispatcher.CheckAccess()}"); Dispatcher.BeginInvoke( new Action <int>(EhSpeechProgress), DispatcherPriority.Background, cue.StartPositionInInput ); } } }
private void AddSubtitle(StorageFile file, IRandomAccessStream strSource) { var ttsUri = new Uri(file.Path); var ttsPicked = TimedTextSource.CreateFromStream(strSource); ttsMap[ttsPicked] = ttsUri; ttsPicked.Resolved += Tts_Resolved; playbackItem.Source.ExternalTimedTextSources.Add(ttsPicked); // Present the first track playbackItem.TimedMetadataTracksChanged += (item, args) => { Debug.WriteLine($"TimedMetadataTracksChanged, Number of tracks: {item.TimedMetadataTracks.Count}"); uint changedTrackIndex = args.Index; TimedMetadataTrack changedTrack = playbackItem.TimedMetadataTracks[(int)changedTrackIndex]; playbackItem.TimedMetadataTracks.SetPresentationMode(changedTrackIndex, TimedMetadataTrackPresentationMode.PlatformPresented); }; }
/// <summary> /// Creates a TimedMetadataTrack on the source /// </summary> /// <param name="source">the source on which to create a TimedMetadataTrack</param> /// <param name="contentId">the content id for this source</param> /// <param name="reportingUriFormat">the format string used to create reporting Uri for each event</param> public static void CreateTrackingEventsTrack(MediaSource source, string contentId, string reportingUriFormat) { if (source.Duration.HasValue && !string.IsNullOrEmpty(contentId)) { long sourceDurationTicks = source.Duration.Value.Ticks; sourceDurationTicks = AdjustDurationIfLive(sourceDurationTicks); var trackingEvents = new TimedMetadataTrack("TrackingEvents", "", TimedMetadataKind.Custom); trackingEvents.AddCue(new TrackingEventCue() { // This method is called once the media has loaded. If it is set to AutoPlay, then // we may miss Position=0.0. Avoid the race by "starting" at the 2-second mark. StartTime = TimeSpan.FromSeconds(2), Id = "2s", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "started", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.25)), Id = "0.25", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "firstQuartile", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.50)), Id = "0.50", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "midpoint", contentId)), // We can cause CueExited to fire at 55% by setting a Duration: Duration = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.05)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.75)), Id = "0.75", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "thirdQuartile", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks(sourceDurationTicks), Id = "1.00", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "complete", contentId)), }); source.ExternalTimedMetadataTracks.Add(trackingEvents); } }
/// <summary> /// Creates a TimedMetadataTrack on the source /// </summary> /// <param name="source">the source on which to create a TimedMetadataTrack</param> /// <param name="contentId">the content id for this source</param> /// <param name="reportingUriFormat">the format string used to create reporting Uri for each event</param> public static void CreateTrackingEventsTrack(MediaSource source, string contentId, string reportingUriFormat) { if (source.Duration.HasValue && !string.IsNullOrEmpty(contentId)) { long sourceDurationTicks = source.Duration.Value.Ticks; sourceDurationTicks = AdjustDurationIfLive(sourceDurationTicks); var trackingEvents = new TimedMetadataTrack("TrackingEvents", "", TimedMetadataKind.Custom); trackingEvents.AddCue(new TrackingEventCue() { // This method is called once the media has loaded. If it is set to AutoPlay, then // we may miss Position=0.0. Avoid the race by "starting" at the 2-second mark. StartTime = TimeSpan.FromSeconds(2), Id = "2s", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "started", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.25)), Id = "0.25", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "firstQuartile", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.50)), Id = "0.50", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "midpoint", contentId)), // We can cause CueExited to fire at 55% by setting a Duration: Duration = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.05)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks((long)Math.Round(sourceDurationTicks * 0.75)), Id = "0.75", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "thirdQuartile", contentId)), }); trackingEvents.AddCue(new TrackingEventCue() { StartTime = TimeSpan.FromTicks(sourceDurationTicks), Id = "1.00", TrackingEventUri = new Uri(string.Format(reportingUriFormat, "complete", contentId)), }); source.ExternalTimedMetadataTracks.Add(trackingEvents); } }
// </SnippetRegisterMetadataHandlerForID3Cues> // <SnippetID3CueEntered> private void metadata_ID3CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null) { // The payload is the raw ID3 bytes found in a TS stream // Ref: http://id3.org/id3v2.4.0-structure var dr = Windows.Storage.Streams.DataReader.FromBuffer(dataCue.Data); var header_ID3 = dr.ReadString(3); var header_version_major = dr.ReadByte(); var header_version_minor = dr.ReadByte(); var header_flags = dr.ReadByte(); var header_tagSize = dr.ReadUInt32(); System.Diagnostics.Debug.WriteLine($"ID3 tag data: major {header_version_major}, minor: {header_version_minor}"); } }
private void metadata_TrackingEvents_CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); object customValue = null; timedMetadataTrack.PlaybackItem.Source.CustomProperties.TryGetValue("contentId", out customValue); string contentId = (string)customValue; logMsg.AppendLine($"{contentId} TrackingEventCue CueEntered raised."); var trackingEventCue = args.Cue as TrackingEventCue; if (trackingEventCue != null) { logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {trackingEventCue.Id}, StartTime: {trackingEventCue.StartTime}, Duration: {trackingEventCue.Duration}"); logMsg.Append($"{trackingEventCue.TrackingEventUri}"); // TODO: Use the reporing Uri for something useful. } Log(logMsg.ToString()); }
/// <summary> /// This function executes when a SpeechCue is hit and calls the functions to update the UI /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="args">the arguments associated with the event.</param> private async void metadata_MarkCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { System.Diagnostics.Debug.WriteLine("Cue text:[" + cue.Text + "]"); // Do something with the cue await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! FillTextBox(cue); }); } } }
private void UnregisterMetadataHandlers(TimedMetadataTrack timedTrack) { if (timedTrack.Label == "HLS Manifest comments") { timedTrack.CueEntered -= metadata_extm3u_CueEntered; } if (timedTrack.Label == "ID3 Tags") { timedTrack.CueEntered -= metadata_id3_CueEntered; } if (timedTrack.Label == "emsg payload") { timedTrack.CueEntered -= metadata_emsg_mp4_CueEntered; } if (timedTrack.Label == "Tracking Events") { timedTrack.CueEntered -= metadata_TrackingEvents_CueEntered; timedTrack.CueExited -= metadata_TrackingEvents_CueExited; } }
/// <summary> /// This function executes when a SpeechCue is hit and calls the functions to update the UI /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="args">the arguments associated with the event.</param> private async void metadata_SpeechCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (timedMetadataTrack.Id == "SpeechSentence" || timedMetadataTrack.Id == "SpeechWord") { SpeechCueType type = timedMetadataTrack.Id == "SpeechSentence" ? SpeechCueType.Sentence : SpeechCueType.Word; var arg = new SpeechCueEventArgs(cue, type); SpeechCueChanged?.Invoke(this, arg); } }); } } }
private void SetDefaultSubtitle(String lang) { // Present the first track Debug.WriteLine($"Tryin to set default subtitle to : {lang}"); playbackItem.TimedMetadataTracksChanged += (item, args) => { if (args.CollectionChange == CollectionChange.ItemInserted) { Debug.WriteLine($"TimedMetadataTracksChanged, Number of tracks: {item.TimedMetadataTracks.Count}"); uint changedTrackIndex = args.Index; TimedMetadataTrack changedTrack = playbackItem.TimedMetadataTracks[(int)changedTrackIndex]; Debug.WriteLine($"Log : {changedTrack.Language}"); if (changedTrack.Language == lang) { Debug.WriteLine($"Trying : {lang}"); playbackItem.TimedMetadataTracks.SetPresentationMode(changedTrackIndex, TimedMetadataTrackPresentationMode.PlatformPresented); Debug.WriteLine($"Set default subtitle to : {lang}"); } } }; }
// </SnippetRegisterMetadataHandlerForImageSubtitles> // <SnippetImageSubtitleCueEntered> private async void metadata_ImageSubtitleCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.ImageSubtitle) { var cue = args.Cue as ImageCue; if (cue != null) { await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { var source = new SoftwareBitmapSource(); await source.SetBitmapAsync(cue.SoftwareBitmap); SubtitleImage.Source = source; SubtitleImage.Width = cue.Extent.Width; SubtitleImage.Height = cue.Extent.Height; SubtitleImage.SetValue(Canvas.LeftProperty, cue.Position.X); SubtitleImage.SetValue(Canvas.TopProperty, cue.Position.Y); }); } } }
/// <summary> /// This function executes when a SpeechCue is hit and calls the functions to update the UI /// </summary> /// <param name="timedMetadataTrack">The timedMetadataTrack associated with the event.</param> /// <param name="args">the arguments associated with the event.</param> private async void metadata_SpeechCueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { // Check in case there are different tracks and the handler was used for more tracks if (timedMetadataTrack.TimedMetadataKind == TimedMetadataKind.Speech) { var cue = args.Cue as SpeechCue; if (cue != null) { System.Diagnostics.Debug.WriteLine("Hit Cue with start:" + cue.StartPositionInInput + " and end:" + cue.EndPositionInInput); System.Diagnostics.Debug.WriteLine("Cue text:[" + cue.Text + "]"); // Do something with the cue await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { // Your UI update code goes here! HighlightTextOnScreen(cue.StartPositionInInput, cue.EndPositionInInput); FillTextBoxes(cue, timedMetadataTrack); }); } } }
protected override void OnNavigatedTo(NavigationEventArgs e) { // Create the playback item var source = MediaSource.CreateFromUri(rootPage.CaptionedMediaUri); var playbackItem = new MediaPlaybackItem(source); // Turn on English captions by default playbackItem.TimedMetadataTracksChanged += (sender, args) => { if (args.CollectionChange == CollectionChange.ItemInserted) { uint changedTrackIndex = args.Index; TimedMetadataTrack changedTrack = playbackItem.TimedMetadataTracks[(int)changedTrackIndex]; if (changedTrack.Language == "en") { playbackItem.TimedMetadataTracks.SetPresentationMode(changedTrackIndex, TimedMetadataTrackPresentationMode.PlatformPresented); } } }; // Set the source to begin playback of the item this.mediaElement.SetPlaybackSource(playbackItem); }
public TimedMetadataTrackEvents(TimedMetadataTrack This) { this.This = This; }
private void metadata_extm3u_CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); var contentId = (Guid)timedMetadataTrack.PlaybackItem.Source.CustomProperties["contentId"]; logMsg.AppendLine($"{contentId} M3U8 DataCue CueEntered raised."); var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null) { // The payload is a UTF-16 Little Endian null-terminated string. // It is any comment line in a manifest that is not part of the HLS spec. var dr = DataReader.FromBuffer(dataCue.Data); dr.UnicodeEncoding = UnicodeEncoding.Utf16LE; var m3u8Comment = dr.ReadString(dataCue.Data.Length / 2 - 1); logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {dataCue.Id}, StartTime: {dataCue.StartTime}, Duration: {dataCue.Duration}"); logMsg.Append(m3u8Comment); // TODO: Use the m3u8Comment string. } Log(logMsg.ToString()); }
private void metadata_id3_CueEntered(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); var contentId = (Guid)timedMetadataTrack.PlaybackItem.Source.CustomProperties["contentId"]; logMsg.AppendLine($"{contentId} ID3 DataCue CueEntered raised."); var dataCue = args.Cue as DataCue; if (dataCue != null && dataCue.Data != null && dataCue.Data.Length >= 10) { // The payload is the raw ID3 bytes found in a TS stream // Ref: http://id3.org/id3v2.4.0-structure var dr = DataReader.FromBuffer(dataCue.Data); var header_ID3 = dr.ReadString(3); var header_version_major = dr.ReadByte(); var header_version_minor = dr.ReadByte(); var header_flags = dr.ReadByte(); var header_tagSize = dr.ReadUInt32(); // TODO: Use the ID3 bytes. logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {dataCue.Id}, StartTime: {dataCue.StartTime}, Duration: {dataCue.Duration}"); logMsg.Append($"{header_ID3}, {header_version_major}.{header_version_minor}, {header_flags}, {header_tagSize}"); } Log(logMsg.ToString()); }
private void metadata_TrackingEvents_CueExited(TimedMetadataTrack timedMetadataTrack, MediaCueEventArgs args) { StringBuilder logMsg = new StringBuilder(); var contentId = (Guid)timedMetadataTrack.PlaybackItem.Source.CustomProperties["contentId"]; logMsg.AppendLine($"{contentId} TrackingEventCue CueExited raised."); var trackingEventCue = args.Cue as TrackingEventCue; if (trackingEventCue != null) { logMsg.AppendLine($"Label: {timedMetadataTrack.Label}, Id: {trackingEventCue.Id}, StartTime: {trackingEventCue.StartTime}, Duration: {trackingEventCue.Duration}"); logMsg.Append($"{trackingEventCue.TrackingEventUri}"); } Log(logMsg.ToString()); }