public async Task ExecuteAsync()
        {
            Debug.WriteLine("BackgroundAudioRun.ExecuteAsync()");

            try
            {
                _systemMediaTransportControls = SystemMediaTransportControls.GetForCurrentView();

                var smtc = _systemMediaTransportControls;

                var isOk = false;

                BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground;

                var mediaPlayer = BackgroundMediaPlayer.Current;

                _metadataHandler = new MetadataHandler(_systemMediaTransportControls, _foregroundNotifier,
                                                       () => mediaPlayer.Position,
                                                       position => UpdateMediaPlayerEvents(mediaPlayer, position),
                                                       _cancellationTokenSource.Token);

                try
                {
                    mediaPlayer.CurrentStateChanged        += OnCurrentStateChanged;
                    mediaPlayer.PlaybackMediaMarkerReached += OnPlaybackMediaMarkerReached;

                    BackgroundSettings.SetBackgroundId(_id);

                    isOk = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() initialization failed: " + ex.ExtendedMessage());
                }

                MediaPlayerManager mediaPlayerManager = null;

                if (isOk)
                {
                    try
                    {
                        mediaPlayerManager = new MediaPlayerManager(mediaPlayer, _metadataHandler, _cancellationTokenSource.Token);

                        mediaPlayerManager.TrackChanged += MediaPlayerManagerOnTrackChanged;
                        mediaPlayerManager.Failed       += MediaPlayerManagerOnFailed;
                        mediaPlayerManager.Ended        += MediaPlayerManagerOnEnded;

                        _mediaPlayerManager = mediaPlayerManager;

                        smtc.ButtonPressed   += SystemMediaTransportControlsOnButtonPressed;
                        smtc.PropertyChanged += SystemMediaTransportControlsOnPropertyChanged;

                        smtc.IsEnabled         = true;
                        smtc.IsPauseEnabled    = true;
                        smtc.IsPlayEnabled     = true;
                        smtc.IsNextEnabled     = true;
                        smtc.IsPreviousEnabled = true;

                        SyncNotification();

                        Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() running");

                        await _completionSource.Task.ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() playback failed: " + ex.ExtendedMessage());
                    }
                }

                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() done running");

                BackgroundSettings.RemoveBackgroundId(_id);

                try
                {
                    _timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
                }
                catch (Exception)
                {
                    // Guard against race with cleanup
                }

                _mediaPlayerManager = null;

                smtc.PropertyChanged -= SystemMediaTransportControlsOnPropertyChanged;
                smtc.ButtonPressed   -= SystemMediaTransportControlsOnButtonPressed;

                if (null != mediaPlayerManager)
                {
                    mediaPlayerManager.TrackChanged -= MediaPlayerManagerOnTrackChanged;
                    mediaPlayerManager.Failed       -= MediaPlayerManagerOnFailed;
                    mediaPlayerManager.Ended        -= MediaPlayerManagerOnEnded;

                    await mediaPlayerManager.CloseAsync().ConfigureAwait(false);

                    mediaPlayerManager.Dispose();
                }

                if (_appId.HasValue)
                {
                    _foregroundNotifier.Notify(BackgroundNotificationType.Stop);
                }

                mediaPlayer.CurrentStateChanged        -= OnCurrentStateChanged;
                mediaPlayer.PlaybackMediaMarkerReached -= OnPlaybackMediaMarkerReached;
                BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayerOnMessageReceivedFromForeground;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() failed: " + ex.ExtendedMessage());
            }
            finally
            {
                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() completed");
            }
        }
        public MediaPlayerManager(MediaPlayer mediaPlayer, MetadataHandler metadataHandler, CancellationToken cancellationToken)
        {
            if (null == mediaPlayer)
            {
                throw new ArgumentNullException("mediaPlayer");
            }
            if (null == metadataHandler)
            {
                throw new ArgumentNullException("metadataHandler");
            }

            Debug.WriteLine("MediaPlayerManager.ctor()");

            _mediaPlayer       = mediaPlayer;
            _metadataHandler   = metadataHandler;
            _cancellationToken = cancellationToken;

            var parameters = MediaStreamFacadeSettings.Parameters;

            parameters.UseHttpConnection = true;
            //parameters.UseSingleStreamMediaManager = true;

            _mediaManagerParameters = new MediaManagerParameters
            {
                ProgramStreamsHandler =
                    streams =>
                {
                    var firstAudio = streams.Streams.FirstOrDefault(x => x.StreamType.Contents == TsStreamType.StreamContents.Audio);

                    var others = null == firstAudio ? streams.Streams : streams.Streams.Where(x => x.Pid != firstAudio.Pid);
                    foreach (
                        var programStream in others)
                    {
                        programStream.BlockStream = true;
                    }
                }
            };

            _bufferingPolicy = new DefaultBufferingPolicy
            {
                BytesMinimumStarting = 24 * 1024,
                BytesMinimum         = 64 * 1024
            };

            _mediaPlayer.MediaOpened         += MediaPlayerOnMediaOpened;
            _mediaPlayer.MediaEnded          += MediaPlayerOnMediaEnded;
            _mediaPlayer.CurrentStateChanged += MediaPlayerOnCurrentStateChanged;
            _mediaPlayer.MediaFailed         += MediaPlayerOnMediaFailed;

            _position = BackgroundSettings.Position;

            var currentTrackUrl = BackgroundSettings.Track;

            if (null == currentTrackUrl)
            {
                if (_position.HasValue)
                {
                    _position = null;
                    BackgroundSettings.Track = null;
                }

                return;
            }

            for (var i = 0; i < _tracks.Count; ++i)
            {
                var track = _tracks[i];

                if (null == track)
                {
                    continue;
                }

                if (track.Url == currentTrackUrl)
                {
                    _trackIndex = i;
                    return;
                }
            }

            if (_position.HasValue)
            {
                _position = null;
                BackgroundSettings.Track = null;
            }
        }
Beispiel #3
0
        public async Task ExecuteAsync()
        {
            Debug.WriteLine("BackgroundAudioRun.ExecuteAsync()");

            try
            {
                _systemMediaTransportControls = SystemMediaTransportControls.GetForCurrentView();

                var smtc = _systemMediaTransportControls;

                var isOk = false;

                BackgroundMediaPlayer.MessageReceivedFromForeground += BackgroundMediaPlayerOnMessageReceivedFromForeground;

                var mediaPlayer = BackgroundMediaPlayer.Current;

                _metadataHandler = new MetadataHandler(_systemMediaTransportControls, _foregroundNotifier,
                    () => mediaPlayer.Position,
                    position => UpdateMediaPlayerEvents(mediaPlayer, position),
                    _cancellationTokenSource.Token);

                try
                {
                    mediaPlayer.CurrentStateChanged += OnCurrentStateChanged;
                    mediaPlayer.PlaybackMediaMarkerReached += OnPlaybackMediaMarkerReached;

                    BackgroundSettings.SetBackgroundId(_id);

                    isOk = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() initialization failed: " + ex.ExtendedMessage());
                }

                MediaPlayerManager mediaPlayerManager = null;

                if (isOk)
                {
                    try
                    {
                        mediaPlayerManager = new MediaPlayerManager(mediaPlayer, _metadataHandler, _cancellationTokenSource.Token);

                        mediaPlayerManager.TrackChanged += MediaPlayerManagerOnTrackChanged;
                        mediaPlayerManager.Failed += MediaPlayerManagerOnFailed;
                        mediaPlayerManager.Ended += MediaPlayerManagerOnEnded;

                        _mediaPlayerManager = mediaPlayerManager;

                        smtc.ButtonPressed += SystemMediaTransportControlsOnButtonPressed;
                        smtc.PropertyChanged += SystemMediaTransportControlsOnPropertyChanged;

                        smtc.IsEnabled = true;
                        smtc.IsPauseEnabled = true;
                        smtc.IsPlayEnabled = true;
                        smtc.IsNextEnabled = true;
                        smtc.IsPreviousEnabled = true;

                        SyncNotification();

                        Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() running");

                        await _completionSource.Task.ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() playback failed: " + ex.ExtendedMessage());
                    }
                }

                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() done running");

                BackgroundSettings.RemoveBackgroundId(_id);

                try
                {
                    _timer.Change(Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
                }
                catch (Exception)
                {
                    // Guard against race with cleanup
                }

                _mediaPlayerManager = null;

                smtc.PropertyChanged -= SystemMediaTransportControlsOnPropertyChanged;
                smtc.ButtonPressed -= SystemMediaTransportControlsOnButtonPressed;

                if (null != mediaPlayerManager)
                {
                    mediaPlayerManager.TrackChanged -= MediaPlayerManagerOnTrackChanged;
                    mediaPlayerManager.Failed -= MediaPlayerManagerOnFailed;
                    mediaPlayerManager.Ended -= MediaPlayerManagerOnEnded;

                    await mediaPlayerManager.CloseAsync().ConfigureAwait(false);

                    mediaPlayerManager.Dispose();
                }

                if (_appId.HasValue)
                    _foregroundNotifier.Notify(BackgroundNotificationType.Stop);

                mediaPlayer.CurrentStateChanged -= OnCurrentStateChanged;
                mediaPlayer.PlaybackMediaMarkerReached -= OnPlaybackMediaMarkerReached;
                BackgroundMediaPlayer.MessageReceivedFromForeground -= BackgroundMediaPlayerOnMessageReceivedFromForeground;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() failed: " + ex.ExtendedMessage());
            }
            finally
            {
                Debug.WriteLine("BackgroundAudioRun.ExecuteAsync() completed");
            }
        }
Beispiel #4
0
        public MediaPlayerManager(MediaPlayer mediaPlayer, MetadataHandler metadataHandler, CancellationToken cancellationToken)
        {
            if (null == mediaPlayer)
                throw new ArgumentNullException(nameof(mediaPlayer));
            if (null == metadataHandler)
                throw new ArgumentNullException(nameof(metadataHandler));

            Debug.WriteLine("MediaPlayerManager.ctor()");

            _mediaPlayer = mediaPlayer;
            _metadataHandler = metadataHandler;
            _cancellationToken = cancellationToken;

            var parameters = MediaStreamFacadeSettings.Parameters;

            parameters.UseHttpConnection = true;
            //parameters.UseSingleStreamMediaManager = true;

            _mediaManagerParameters = new MediaManagerParameters
            {
                ProgramStreamsHandler =
                    streams =>
                    {
                        var firstAudio = streams.Streams.FirstOrDefault(x => x.StreamType.Contents == TsStreamType.StreamContents.Audio);

                        var others = null == firstAudio ? streams.Streams : streams.Streams.Where(x => x.Pid != firstAudio.Pid);
                        foreach (
                            var programStream in others)
                            programStream.BlockStream = true;
                    }
            };

            _bufferingPolicy = new DefaultBufferingPolicy
            {
                BytesMinimumStarting = 24 * 1024,
                BytesMinimum = 64 * 1024
            };

            _mediaPlayer.MediaOpened += MediaPlayerOnMediaOpened;
            _mediaPlayer.MediaEnded += MediaPlayerOnMediaEnded;
            _mediaPlayer.CurrentStateChanged += MediaPlayerOnCurrentStateChanged;
            _mediaPlayer.MediaFailed += MediaPlayerOnMediaFailed;

            _position = BackgroundSettings.Position;

            var currentTrackUrl = BackgroundSettings.Track;

            if (null == currentTrackUrl)
            {
                if (_position.HasValue)
                {
                    _position = null;
                    BackgroundSettings.Track = null;
                }

                return;
            }

            for (var i = 0; i < _tracks.Count; ++i)
            {
                var track = _tracks[i];

                if (null == track)
                    continue;

                if (track.Url == currentTrackUrl)
                {
                    _trackIndex = i;
                    return;
                }
            }

            if (_position.HasValue)
            {
                _position = null;
                BackgroundSettings.Track = null;
            }
        }