//</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 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> }
/// <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); } }