Exemplo n.º 1
0
        /// <remarks>
        /// AudioPlayer instances can share the same process.
        /// Static fields can be used to share state between AudioPlayer instances
        /// or to communicate with the Audio Streaming agent.
        /// </remarks>
        public AudioPlayer()
        {
            // since we are in a background agent : the types registered in the IoC container are not shared.
            IVersionDependentResourcesProvider versionDependentResourcesProvider = new VersionDependentResourcesProvider();

            _audioTrackFactory = new AudioTrackFactory(versionDependentResourcesProvider);

            // TODO : Handle exceptions
            using (var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
            {
                LoadAsciiUriFixes(userStoreForApplication);
                LoadPlayqueue(userStoreForApplication);
            }

            if (!_classInitialized)
            {
                _classInitialized = true;
                // Subscribe to the managed exception handler
                Deployment.Current.Dispatcher.BeginInvoke(delegate
                {
                    Application.Current.UnhandledException += AudioPlayer_UnhandledException;
                });
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PlaybackService"/> class.
        /// </summary>
        /// <param name="audioStationSession"></param>
        /// <param name="audioTrackFactory"></param>
        public PlaybackService(IAudioStationSession audioStationSession, IAudioTrackFactory audioTrackFactory, IVersionDependentResourcesProvider versionDependentResourceProvider)
        {
            _logService = IoC.Container.Get <ILogService>();

            _audioStationSession = audioStationSession;
            _audioTrackFactory   = audioTrackFactory;
            _versionDependentResourceProvider = versionDependentResourceProvider;

            // We need an observable collection so we can serialize the items to IsolatedStorage in order to get the background rendering service to read it from disk, since the background Agent is not running in the same process.
            //PlayqueueItems = new ObservableCollection<ISynoTrack>();

            this._tracksToGuidMapping = new List <GuidToTrackMapping>();

            using (var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (
                    IsolatedStorageFileStream asciiUriFixes = userStoreForApplication.OpenFile(
                        "AsciiUriFixes.xml", FileMode.OpenOrCreate))
                {
                    DataContractSerializer dcs = new DataContractSerializer(typeof(List <AsciiUriFix>));
                    //var xs = new XmlSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));

                    try
                    {
                        _asciiUriFixes = (List <AsciiUriFix>)dcs.ReadObject(asciiUriFixes);
                    }
                    catch (Exception e)
                    {
                        // could not deserialize XML for playlist : let's build an empty list.
                        _asciiUriFixes = new List <AsciiUriFix>();
                    }
                }

                PlayqueueInterProcessCommunicationTransporter deserialization = null;
                using (
                    IsolatedStorageFileStream playQueueFile = userStoreForApplication.OpenFile(
                        "playqueue.xml", FileMode.OpenOrCreate))
                {
                    DataContractSerializer dcs =
                        new DataContractSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));
                    //var xs = new XmlSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));

                    try
                    {
                        deserialization = (PlayqueueInterProcessCommunicationTransporter)dcs.ReadObject(playQueueFile);

                        foreach (GuidToTrackMapping pair in deserialization.Mappings)
                        {
                            this._tracksToGuidMapping.Add(pair);
                        }
                    }
                    catch (Exception e)
                    {
                        // could not deserialize XML for playlist : let's keep it empty.
                    }
                }
            }


            //this.PlayqueueItems.CollectionChanged += this.OnPlayqueueItemsChanged;

            BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(this.BackgroundPlayerPlayStateChanged);

            this._progressUpdater = new Timer(
                e =>
            {
                var backgroundAudioPlayer = BackgroundAudioPlayer.Instance;
                if (this.TrackCurrentPositionChanged != null && backgroundAudioPlayer.Track != null)
                {
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {
                        TrackCurrentPositionChangedEventArgs trackCurrentPositionChangedEventArgs = new TrackCurrentPositionChangedEventArgs();

                        trackCurrentPositionChangedEventArgs.LoadPercentComplete = backgroundAudioPlayer.BufferingProgress;
                        double totalSeconds = 0;
                        TimeSpan position   = new TimeSpan();

                        try
                        {
                            position = backgroundAudioPlayer.Position;
                        }
                        catch (SystemException)
                        {
                            _progressUpdater.Change(-1, 0);
                            // swallow exception : we get an HRESULT error, when no valid position could be retrieved. Maybe a beta behavior that will change in the future. since we can ignore the error and set the duration to 0 ( maybe the track hasn't been loaded yet ) we'll just swallow the exception.
                        }

                        try
                        {
                            totalSeconds = backgroundAudioPlayer.Position.TotalSeconds;
                        }
                        catch (SystemException)
                        {
                            // swallow exception : we get an HRESULT error, when no valid duration could be retrieved. Maybe a beta behavior that will change in the future. since we can ignore the error and set the duration to 0 ( maybe the track hasn't been loaded yet ) we'll just swallow the exception.
                        }

                        if (position.TotalSeconds == 0)
                        {
                            // avoid zero-division
                            trackCurrentPositionChangedEventArgs.PlaybackPercentComplete = 0;
                        }
                        else
                        {
                            trackCurrentPositionChangedEventArgs.PlaybackPercentComplete = totalSeconds / backgroundAudioPlayer.Track.Duration.TotalSeconds;
                            trackCurrentPositionChangedEventArgs.Position = position;
                        }

                        this.TrackCurrentPositionChanged(this, trackCurrentPositionChangedEventArgs);
                    });
                }
            },
                null,
                0,
                200);
            //(o, e) =>
            //{
            //    _backgroundAudioRenderingService.OnPlayqueueItemsChanged(e.NewItems, e.OldItems);
            //};
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PlaybackService"/> class.
        /// </summary>
        /// <param name="audioStationSession"></param>
        /// <param name="audioTrackFactory"></param>
        public PlaybackService(IAudioStationSession audioStationSession, IAudioTrackFactory audioTrackFactory, IVersionDependentResourcesProvider versionDependentResourceProvider)
        {
            _logService = IoC.Container.Get<ILogService>();

            _audioStationSession = audioStationSession;
            _audioTrackFactory = audioTrackFactory;
            _versionDependentResourceProvider = versionDependentResourceProvider;

            // We need an observable collection so we can serialize the items to IsolatedStorage in order to get the background rendering service to read it from disk, since the background Agent is not running in the same process.
            //PlayqueueItems = new ObservableCollection<ISynoTrack>();

            this._tracksToGuidMapping = new List<GuidToTrackMapping>();

            using (var userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
            {

                using (
                    IsolatedStorageFileStream asciiUriFixes = userStoreForApplication.OpenFile(
                        "AsciiUriFixes.xml", FileMode.OpenOrCreate))
                {

                    DataContractSerializer dcs = new DataContractSerializer(typeof(List<AsciiUriFix>));
                    //var xs = new XmlSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));

                    try
                    {
                        _asciiUriFixes = (List<AsciiUriFix>)dcs.ReadObject(asciiUriFixes);
                    }
                    catch (Exception e)
                    {
                        // could not deserialize XML for playlist : let's build an empty list.
                        _asciiUriFixes = new List<AsciiUriFix>();
                    }
                }

                PlayqueueInterProcessCommunicationTransporter deserialization = null;
                using (
                    IsolatedStorageFileStream playQueueFile = userStoreForApplication.OpenFile(
                        "playqueue.xml", FileMode.OpenOrCreate))
                {

                    DataContractSerializer dcs =
                        new DataContractSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));
                    //var xs = new XmlSerializer(typeof(PlayqueueInterProcessCommunicationTransporter));

                    try
                    {
                        deserialization = (PlayqueueInterProcessCommunicationTransporter)dcs.ReadObject(playQueueFile);

                        foreach (GuidToTrackMapping pair in deserialization.Mappings)
                        {
                            this._tracksToGuidMapping.Add(pair);
                        }
                    }
                    catch (Exception e)
                    {
                        // could not deserialize XML for playlist : let's keep it empty.

                    }
                }
            }

            //this.PlayqueueItems.CollectionChanged += this.OnPlayqueueItemsChanged;

            BackgroundAudioPlayer.Instance.PlayStateChanged += new EventHandler(this.BackgroundPlayerPlayStateChanged);

            this._progressUpdater = new Timer(
                e =>
                {
                    var backgroundAudioPlayer = BackgroundAudioPlayer.Instance;
                    if (this.TrackCurrentPositionChanged != null && backgroundAudioPlayer.Track != null)
                    {

                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                TrackCurrentPositionChangedEventArgs trackCurrentPositionChangedEventArgs = new TrackCurrentPositionChangedEventArgs();

                                trackCurrentPositionChangedEventArgs.LoadPercentComplete = backgroundAudioPlayer.BufferingProgress;
                                double totalSeconds = 0;
                                TimeSpan position = new TimeSpan();

                                try
                                {
                                    position = backgroundAudioPlayer.Position;
                                }
                                catch (SystemException)
                                {
                                    _progressUpdater.Change(-1, 0);
                                    // swallow exception : we get an HRESULT error, when no valid position could be retrieved. Maybe a beta behavior that will change in the future. since we can ignore the error and set the duration to 0 ( maybe the track hasn't been loaded yet ) we'll just swallow the exception.
                                }

                                try
                                {
                                    totalSeconds = backgroundAudioPlayer.Position.TotalSeconds;
                                }
                                catch (SystemException)
                                {
                                    // swallow exception : we get an HRESULT error, when no valid duration could be retrieved. Maybe a beta behavior that will change in the future. since we can ignore the error and set the duration to 0 ( maybe the track hasn't been loaded yet ) we'll just swallow the exception.
                                }

                                if (position.TotalSeconds == 0)
                                {
                                    // avoid zero-division
                                    trackCurrentPositionChangedEventArgs.PlaybackPercentComplete = 0;
                                }
                                else
                                {
                                    trackCurrentPositionChangedEventArgs.PlaybackPercentComplete = totalSeconds / backgroundAudioPlayer.Track.Duration.TotalSeconds;
                                    trackCurrentPositionChangedEventArgs.Position = position;
                                }

                                this.TrackCurrentPositionChanged(this, trackCurrentPositionChangedEventArgs);
                            });
                    }
                },
                null,
                0,
                200);
            //(o, e) =>
            //{
            //    _backgroundAudioRenderingService.OnPlayqueueItemsChanged(e.NewItems, e.OldItems);
            //};
        }