Exemplo n.º 1
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();
        }
Exemplo n.º 2
0
        // <SnippetBinderBinding>
        private void Binder_Binding(MediaBinder sender, MediaBindingEventArgs args)
        {
            // Get a deferral if you need to perform async operations
            // var deferral = args.GetDeferral();

            var contentUri = new Uri("http://contoso.com/media/" + args.MediaBinder.Token);

            args.SetUri(contentUri);

            // Call complete after your async operations are complete
            // deferral.Complete();
        }
Exemplo n.º 3
0
        // <SnippetBinderBindingAMS>
        private async void Binder_Binding_AdaptiveMediaSource(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferral = args.GetDeferral();

            var contentUri = new Uri("http://contoso.com/media/" + args.MediaBinder.Token);
            AdaptiveMediaSourceCreationResult result = await AdaptiveMediaSource.CreateFromUriAsync(contentUri);

            if (result.MediaSource != null)
            {
                args.SetAdaptiveMediaSource(result.MediaSource);
            }
            args.SetUri(contentUri);

            deferral.Complete();
        }
Exemplo n.º 4
0
        private void Binder_Binding(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferral = args.GetDeferral();

            if (_mapping.TryGetValue(args.MediaBinder.Token, out Message message))
            {
                var file = GetFile(message);
                if (file.Local.IsDownloadingCompleted)
                {
                    args.SetUri(new Uri("file:///" + file.Local.Path));
                    deferral.Complete();
                }
                else if (file.Local.CanBeDownloaded && !file.Local.IsDownloadingCompleted)
                {
                    _inverse[args.MediaBinder.Token] = deferral;
                    _binders[args.MediaBinder.Token] = args;
                    _protoService.Send(new DownloadFile(file.Id, 10));
                }
            }
        }
Exemplo n.º 5
0
        private async void Binder_Binding(MediaBinder sender, MediaBindingEventArgs args)
        {
            var deferral = args.GetDeferral();

            loading = true;
            var songid = Convert.ToInt32(args.MediaBinder.Token);
            var item   = playList.FirstOrDefault(x => x.songid == songid);
            var url    = await GetUrl(songid);

            if (url == null)
            {
                await DispatcherHelper.ExecuteOnUIThreadAsync(() =>
                {
                    Utils.ShowMessageToast("无法播放" + item.title + ",已从播放列表移除");
                    playList.Remove(item);
                });

                mediaPlaybackList.Items.Remove(mediaPlaybackList.Items.FirstOrDefault(x => x.Source == sender.Source));
                loading = false;
                return;
            }
            if (url.type == -1)
            {
                item.is_preview = true;
            }
            else
            {
                item.is_preview = false;
            }

            item.play_url   = new Uri(url.cdns[0]);
            item.qualities  = url.qualities;
            item.backup_url = url.cdns;
            var contentUri = item.play_url;

            args.SetUri(contentUri);
            loading = false;
            deferral.Complete();
        }
Exemplo n.º 6
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();
        }
Exemplo n.º 7
0
 private void Binder_Binding(MediaBinder sender, MediaBindingEventArgs args)
 {
     args.SetUri(new Uri(sender.Token));
     sender.Binding -= Binder_Binding;
 }
Exemplo n.º 8
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.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();
        }