Example #1
0
        private void Binder_Binding_StorageFile(MediaBinder sender, MediaBindingEventArgs args)
        {
            switch (args.MediaBinder.Token)
            {
            case "MyBindingToken1":
                args.SetStorageFile(binderFiles[0]);
                break;

            case "MyBindingToken2":
                args.SetStorageFile(binderFiles[1]);
                break;
            }
        }
Example #2
0
        private async void BindMediaSource(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferal = args.GetDeferral();

            // Get the track data
            var track = JsonConvert.DeserializeObject <BaseTrack>(args.MediaBinder.Token);

            // Only run if the track exists
            if (track != null)
            {
                // Get the audio stream url for this track
                var audioStreamUri = await track.GetAudioStreamAsync(_youTubeClient);

                // If we are live and youtube, we get an adaptive stream url
                if (track.ServiceType == ServiceType.YouTube && track.IsLive)
                {
                    var source = await AdaptiveMediaSource.CreateFromUriAsync(audioStreamUri);

                    if (source.Status == AdaptiveMediaSourceCreationStatus.Success)
                    {
                        args.SetAdaptiveMediaSource(source.MediaSource);
                    }
                }
                else if (track.ServiceType == ServiceType.Local)
                {
                    var file = track.CustomProperties["File"] as StorageFile;

                    args.SetStorageFile(file);
                }
                else if (track.ServiceType == ServiceType.ITunesPodcast)
                {
                    args.SetUri(new Uri(track.AudioStreamUrl));
                }
                else
                {
                    // Set generic stream url.
                    args.SetUri(audioStreamUri);
                }
            }

            deferal.Complete();
        }
Example #3
0
        private async Task BindMediaSourceAsync(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferal = args.GetDeferral();

            // Get the track data
            var track = JsonConvert.DeserializeObject <BaseTrack>(args.MediaBinder.Token);

            try
            {
                // Only run if the track exists
                if (track != null)
                {
                    switch (track.ServiceType)
                    {
                    case ServiceTypes.YouTube:
                        var youTubeAudioUrl = await track.GetAudioStreamAsync(_youTubeClient);

                        if (!string.IsNullOrEmpty(youTubeAudioUrl))
                        {
                            if (track.IsLive)
                            {
                                var source = await AdaptiveMediaSource.CreateFromUriAsync(new Uri(youTubeAudioUrl));

                                if (source.Status == AdaptiveMediaSourceCreationStatus.Success)
                                {
                                    args.SetAdaptiveMediaSource(source.MediaSource);
                                }
                            }
                            else
                            {
                                args.SetUri(new Uri(youTubeAudioUrl));
                            }
                        }
                        break;

                    case ServiceTypes.Local:
                        var fileToken = track.CustomProperties["file_token"].ToString();
                        var file      = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);

                        args.SetStorageFile(file);
                        break;

                    case ServiceTypes.ITunesPodcast:
                        args.SetUri(new Uri(track.AudioStreamUrl));
                        break;

                    default:
                        // Get the audio stream url for this track
                        var audioStreamUrl = await track.GetAudioStreamAsync(_youTubeClient);

                        if (!string.IsNullOrEmpty(audioStreamUrl))
                        {
                            // Set generic stream url.
                            args.SetUri(new Uri(audioStreamUrl));
                        }
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                // So we know an error has occured
                _telemetryService.TrackEvent("Media Item Load Fail", new Dictionary <string, string> {
                    { "Message", e.Message },
                    { "Service Type", track.ServiceType.ToString() },
                    { "Item ID", track.TrackId }
                });

                if (!(DeviceHelper.IsBackground || DeviceHelper.IsXbox))
                {
                    await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                    {
                        App.NotificationManager?.Show("An error occurred while trying to play or preload the track '" + track?.Title + "' (" + track?.ServiceType + ").\n\nError message: " + e.Message, 6500);
                    });
                }
            }

            deferal.Complete();
        }
        private async Task BindMediaSourceAsync(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferal = args.GetDeferral();

            // Get the track data
            var track = JsonConvert.DeserializeObject <BaseTrack>(args.MediaBinder.Token);

            try
            {
                // Only run if the track exists
                if (track != null)
                {
                    switch (track.ServiceType)
                    {
                    case ServiceTypes.YouTube:
                        var youTubeAudioUrl = await track.GetAudioStreamAsync(_youTubeClient);

                        if (!string.IsNullOrEmpty(youTubeAudioUrl))
                        {
                            if (track.IsLive)
                            {
                                var source = await AdaptiveMediaSource.CreateFromUriAsync(new Uri(youTubeAudioUrl));

                                if (source.Status == AdaptiveMediaSourceCreationStatus.Success)
                                {
                                    args.SetAdaptiveMediaSource(source.MediaSource);
                                }
                            }
                            else
                            {
                                args.SetUri(new Uri(youTubeAudioUrl));
                            }
                        }
                        break;

                    case ServiceTypes.SoundCloud:
                    case ServiceTypes.SoundCloudV2:
                        // Build the url for soundcloud
                        var soundcloudStreamUrl = await track.GetAudioStreamAsync(_youTubeClient);

                        // SoundCloud now requires auth feeds

                        var scConnected = SoundByteService.Current.IsServiceConnected(ServiceTypes.SoundCloud);
                        if (!scConnected)
                        {
                            throw new Exception("You must be logged into your SoundCloud account to play music from SoundCloud!");
                        }

                        // Get scheme and token
                        var scheme = SoundByteService.Current.Services.FirstOrDefault(x => x.Service == ServiceTypes.SoundCloud)?.AuthenticationScheme;
                        var token  = SoundByteService.Current.Services.FirstOrDefault(x => x.Service == ServiceTypes.SoundCloud)?.UserToken?.AccessToken;

                        // Client with Auth
                        var client = new Windows.Web.Http.HttpClient();
                        client.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue(scheme, token);

                        var stream = await HttpRandomAccessStream.CreateAsync(client, new Uri(soundcloudStreamUrl));

                        args.SetStream(stream, stream.ContentType);

                        break;

                    case ServiceTypes.Local:
                        var fileToken = track.CustomProperties["file_token"].ToString();
                        var file      = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);

                        args.SetStorageFile(file);
                        break;

                    case ServiceTypes.ITunesPodcast:
                        args.SetUri(new Uri(track.AudioStreamUrl));
                        break;

                    default:
                        // Get the audio stream url for this track
                        var audioStreamUrl = await track.GetAudioStreamAsync(_youTubeClient);

                        if (!string.IsNullOrEmpty(audioStreamUrl))
                        {
                            // Set generic stream url.
                            args.SetUri(new Uri(audioStreamUrl));
                        }
                        break;
                    }
                }
            }
            catch (Exception e)
            {
                // So we know an error has occured
                _telemetryService.TrackEvent("Media Item Load Fail", new Dictionary <string, string> {
                    { "Message", e.Message },
                    { "Service Type", track.ServiceType.ToString() },
                    { "Item ID", track.TrackId }
                });

                if (!(DeviceHelper.IsBackground || DeviceHelper.IsXbox))
                {
                    await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                    {
                        App.NotificationManager?.Show("An error occurred while trying to play or preload the track '" + track?.Title + "' (" + track?.ServiceType + ").\n\nError message: " + e.Message, 6500);
                    });
                }
            }

            deferal.Complete();
        }