/// <summary> /// SetAudioVideoUrl /// Prepare the MediaElement to play audio or video content /// </summary> /// <param name="content">Url string of the content to play </param> /// <returns>true if success</returns> private async System.Threading.Tasks.Task<bool> SetAudioVideoUrl(string Content) { try { if (IsLocalFile(Content)) { Windows.Storage.StorageFile file = await GetFileFromLocalPathUrl(Content); if (file != null) { var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); if (fileStream != null) { mediaElement.SetSource(fileStream, file.FileType); return true; } } else LogMessage("Failed to load media file: " + Content ); } else if (!IsAdaptiveStreaming(Content)) { mediaElement.Source = new Uri(Content); return true; } else { // If SMOOTH stream if (IsSmoothStreaming(Content)) { // if the stream is ready to play in the cache // initialize the Source with the url of the asset // using the MediaCache method GetSourceUri if ((mediaCache.IsAssetReadyToPlay(new Uri(Content)) == true)) { // // Warning: // In order to prevent the MediaElement from sending http request when offline // we need to replace http:// with ms-sstr::// prefix // // LogMessage("Playing from the cache asset: " + Content); LogMessage("Url used to avoid network request: " + mediaCache.GetSourceUri(new Uri(Content)).ToString()); mediaElement.Source = mediaCache.GetSourceUri(new Uri(Content)); return true; } else { mediaElement.Source = new Uri(Content); return true; } } else { // If DASH or HLS content // Create the AdaptiveMediaSource Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationResult result = await Windows.Media.Streaming.Adaptive.AdaptiveMediaSource.CreateFromUriAsync(new Uri(Content)); if (result.Status == Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationStatus.Success) { if (adaptiveMediaSource != null) { adaptiveMediaSource.DownloadBitrateChanged -= AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted -= AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed -= AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested -= AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged -= AdaptiveMediaSource_PlaybackBitrateChanged; } adaptiveMediaSource = result.MediaSource; adaptiveMediaSource.DownloadBitrateChanged += AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted += AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed += AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested += AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged += AdaptiveMediaSource_PlaybackBitrateChanged; LogMessage("Available bitrates: "); uint startupBitrate = 0; foreach (var b in adaptiveMediaSource.AvailableBitrates) { LogMessage("bitrate: " + b.ToString() + " b/s "); if ((startupBitrate == 0) && (b >= MinBitRate) && (b <= MaxBitRate)) startupBitrate = b; } // Set bitrate range for HLS and DASH if(startupBitrate>0) adaptiveMediaSource.InitialBitrate = startupBitrate; adaptiveMediaSource.DesiredMaxBitrate = MaxBitRate; adaptiveMediaSource.DesiredMinBitrate = MinBitRate; mediaElement.SetMediaStreamSource(adaptiveMediaSource); return true; } else LogMessage("Failed to create AdaptiveMediaSource: " + result.Status.ToString()); } } } catch (Exception ex) { LogMessage("Exception Playing: " + ex.Message.ToString()); CurrentMediaUrl = string.Empty; CurrentPosterUrl = string.Empty; } return false; }
/// <summary> /// SetAudioVideoUrl /// Prepare the MediaElement to play audio or video content /// </summary> /// <param name="content">Url string of the content to play </param> /// <returns>true if success</returns> private async System.Threading.Tasks.Task<bool> SetAudioVideoUrl(string Content) { try { if (IsLocalFile(Content)) { Windows.Storage.StorageFile file = await GetFileFromLocalPathUrl(Content); if (file != null) { var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); if (fileStream != null) { mediaElement.SetSource(fileStream, file.FileType); return true; } } else LogMessage("Failed to load media file: " + Content); } else if (!IsAdaptiveStreaming(Content)) { mediaElement.Source = new Uri(Content); return true; } else { // If SMOOTH stream if (IsSmoothStreaming(Content)) { mediaElement.Source = new Uri(Content); return true; } else { // If HLS content uses legacy HLS Libray if (IsHLSUrl(Content)) { Content = UpdateHLSUrl(Content); mediaElement.Source = new Uri(Content); return true; } else { // If DASH content // Create the AdaptiveMediaSource Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationResult result = await Windows.Media.Streaming.Adaptive.AdaptiveMediaSource.CreateFromUriAsync(new Uri(Content)); if (result.Status == Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationStatus.Success) { if (adaptiveMediaSource != null) { adaptiveMediaSource.DownloadBitrateChanged -= AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted -= AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed -= AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested -= AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged -= AdaptiveMediaSource_PlaybackBitrateChanged; } adaptiveMediaSource = result.MediaSource; adaptiveMediaSource.DownloadBitrateChanged += AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted += AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed += AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested += AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged += AdaptiveMediaSource_PlaybackBitrateChanged; LogMessage("Available bitrates: "); uint startupBitrate = 0; foreach (var b in adaptiveMediaSource.AvailableBitrates) { LogMessage("bitrate: " + b.ToString() + " b/s "); if ((startupBitrate == 0) && (b >= MinBitRate) && (b <= MaxBitRate)) startupBitrate = b; } // Set bitrate range for HLS and DASH if (startupBitrate > 0) adaptiveMediaSource.InitialBitrate = startupBitrate; adaptiveMediaSource.DesiredMaxBitrate = MaxBitRate; adaptiveMediaSource.DesiredMinBitrate = MinBitRate; mediaElement.SetMediaStreamSource(adaptiveMediaSource); return true; } else LogMessage("Failed to create AdaptiveMediaSource: " + result.Status.ToString()); } } } } catch (Exception ex) { LogMessage("Exception Playing: " + ex.Message.ToString()); CurrentMediaUrl = string.Empty; CurrentPosterUrl = string.Empty; } return false; }
/// <summary> /// SetAudioVideoUrl /// Prepare the MediaElement to play audio or video content /// </summary> /// <param name="content">Url string of the content to play </param> /// <returns>true if success</returns> private async System.Threading.Tasks.Task<bool> SetAudioVideoUrl(string Content) { try { if (IsLocalFile(Content)) { Windows.Storage.StorageFile file = await GetFileFromLocalPathUrl(Content); if (file != null) { mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromStorageFile(file); /* var fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read); if (fileStream != null) { mediaPlayer.SetStreamSource(fileStream); mediaPlayer.SetFileSource(file); return true; }*/ } else LogMessage("Failed to load media file: " + Content); } else if (!IsAdaptiveStreaming(Content)) { if (httpHeaders != null) { try { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); Windows.Web.Http.HttpRequestMessage request = new Windows.Web.Http.HttpRequestMessage(Windows.Web.Http.HttpMethod.Get, new Uri(Content)); SetHttpHeaders(httpHeaders, request.Headers); Windows.Web.Http.HttpResponseMessage response = await httpClient.SendRequestAsync(request, Windows.Web.Http.HttpCompletionOption.ResponseHeadersRead); if (response.IsSuccessStatusCode) { string contentType = "video/mp4"; if (response.Content.Headers.ContainsKey("Content-Type")) { contentType = response.Content.Headers["Content-Type"]; } var inputStream = await response.Content.ReadAsInputStreamAsync(); var memStream = new MemoryStream(); await inputStream.AsStreamForRead().CopyToAsync(memStream); mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromStream(memStream.AsRandomAccessStream(), contentType); } else LogMessage("DownloadRequested for uri: " + Content.ToString() + " error: " + response.StatusCode.ToString()); } catch (Exception e) { LogMessage("DownloadRequested for uri: " + Content.ToString() + " exception: " + e.Message); } } else { mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromUri(new Uri(Content)); } return true; } else { // If SMOOTH stream if (IsSmoothStreaming(Content)) { mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromUri(new Uri(Content)); return true; } else { // If DASH or HLS content // Create the AdaptiveMediaSource Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationResult result = null; if (httpHeaders != null) { try { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); SetHttpHeaders(httpHeaders, httpClient.DefaultRequestHeaders); result = await Windows.Media.Streaming.Adaptive.AdaptiveMediaSource.CreateFromUriAsync(new Uri(Content), httpClient); } catch (Exception e) { LogMessage("Exception while downloading DASH or HLS manifest: " + e.Message); } } else result = await Windows.Media.Streaming.Adaptive.AdaptiveMediaSource.CreateFromUriAsync(new Uri(Content)); if (result.Status == Windows.Media.Streaming.Adaptive.AdaptiveMediaSourceCreationStatus.Success) { if (adaptiveMediaSource != null) { adaptiveMediaSource.DownloadBitrateChanged -= AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted -= AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed -= AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested -= AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged -= AdaptiveMediaSource_PlaybackBitrateChanged; } adaptiveMediaSource = result.MediaSource; adaptiveMediaSource.DownloadBitrateChanged += AdaptiveMediaSource_DownloadBitrateChanged; adaptiveMediaSource.DownloadCompleted += AdaptiveMediaSource_DownloadCompleted; adaptiveMediaSource.DownloadFailed += AdaptiveMediaSource_DownloadFailed; adaptiveMediaSource.DownloadRequested += AdaptiveMediaSource_DownloadRequested; adaptiveMediaSource.PlaybackBitrateChanged += AdaptiveMediaSource_PlaybackBitrateChanged; LogMessage("Available bitrates: "); uint startupBitrate = 0; foreach (var b in adaptiveMediaSource.AvailableBitrates) { LogMessage("bitrate: " + b.ToString() + " b/s "); if ((startupBitrate == 0) && (b >= MinBitRate) && (b <= MaxBitRate)) startupBitrate = b; } // Set bitrate range for HLS and DASH if(startupBitrate>0) adaptiveMediaSource.InitialBitrate = startupBitrate; adaptiveMediaSource.DesiredMaxBitrate = MaxBitRate; adaptiveMediaSource.DesiredMinBitrate = MinBitRate; mediaPlayer.Source = Windows.Media.Core.MediaSource.CreateFromAdaptiveMediaSource(adaptiveMediaSource); return true; } else LogMessage("Failed to create AdaptiveMediaSource: " + result.Status.ToString()); } } } catch (Exception ex) { LogMessage("Exception Playing: " + ex.Message.ToString()); CurrentMediaUrl = string.Empty; CurrentPosterUrl = string.Empty; } return false; }