private void SpotifyOnTrackChange(object sender, SpotifyAPI.Local.TrackChangeEventArgs trackChangeEventArgs)
        {
            var track = new Track()
            {
                Length         = trackChangeEventArgs.NewTrack.Length,
                AlbumArtUri    = new Uri(trackChangeEventArgs.NewTrack.GetAlbumArtUrl(AlbumArtSize.Size160)),
                ArtistResource = new Resource()
                {
                    Name = trackChangeEventArgs.NewTrack.ArtistResource.Name
                },
                AlbumResource = new Resource()
                {
                    Name = trackChangeEventArgs.NewTrack.AlbumResource.Name
                },
                TrackResource = new Resource()
                {
                    Name = trackChangeEventArgs.NewTrack.TrackResource.Name
                }
            };

            var eventArgs = new TrackChangeEventArgs()
            {
                NewTrack = track
            };

            OnTrackChange?.Invoke(this, eventArgs);
        }
 private void OnLocalTrackChange(object sender, SpotifyAPI.Local.TrackChangeEventArgs e)
 {
     OnTrackChange?.Invoke(
         sender,
         new TrackChangeEventArgs(
             new TrackDetails(e.NewTrack),
             new TrackDetails(e.OldTrack)));
 }
Exemple #3
0
        private void ElapsedTick(object sender, ElapsedEventArgs e)
        {
            if (_eventStatusResponse == null)
            {
                _eventStatusResponse = GetStatus();
                _eventTimer.Start();
                return;
            }
            StatusResponse newStatusResponse = GetStatus();

            if (newStatusResponse == null)
            {
                _eventTimer.Start();
                return;
            }
            if (!newStatusResponse.Running && newStatusResponse.Track == null)
            {
                _eventTimer.Start();
                return;
            }
            if (newStatusResponse.Track != null && _eventStatusResponse.Track != null)
            {
                if (newStatusResponse.Track.TrackResource?.Uri != _eventStatusResponse.Track.TrackResource?.Uri ||
                    newStatusResponse.Track.IsOtherTrackType() && newStatusResponse.Track.Length != this._eventStatusResponse.Track.Length)
                {
                    OnTrackChange?.Invoke(this, new TrackChangeEventArgs()
                    {
                        OldTrack = _eventStatusResponse.Track,
                        NewTrack = newStatusResponse.Track
                    });
                }
            }
            if (newStatusResponse.Playing != _eventStatusResponse.Playing)
            {
                OnPlayStateChange?.Invoke(this, new PlayStateEventArgs()
                {
                    Playing = newStatusResponse.Playing
                });
            }
            if (newStatusResponse.Volume != _eventStatusResponse.Volume)
            {
                OnVolumeChange?.Invoke(this, new VolumeChangeEventArgs()
                {
                    OldVolume = _eventStatusResponse.Volume,
                    NewVolume = newStatusResponse.Volume
                });
            }
            if (newStatusResponse.PlayingPosition != _eventStatusResponse.PlayingPosition)
            {
                OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs()
                {
                    TrackTime = newStatusResponse.PlayingPosition
                });
            }
            _eventStatusResponse = newStatusResponse;
            _eventTimer.Start();
        }
Exemple #4
0
        public async void ElapsedEventTick(object sender, ElapsedEventArgs e)
        {
            SpotifyLatestStatus = await SpotifyProcess.GetSpotifyStatus();

            if (SpotifyLatestStatus?.CurrentTrack == null)
            {
                EventTimer.Start();
                return;
            }

            var newestTrack = SpotifyLatestStatus.CurrentTrack;

            if (Track != null)
            {
                if (newestTrack.Playing != Track.Playing)
                {
                    if (newestTrack.Playing)
                    {
                        SongTimer.Start();
                    }
                    else
                    {
                        SongTimer.Stop();
                    }

                    await Task.Run(() => OnPlayStateChange?.Invoke(this, new PlayStateEventArgs()
                    {
                        Playing = newestTrack.Playing
                    }));
                }
                if (!newestTrack.Equals(Track))
                {
                    SongTimer.Start();
                    await Task.Run(async() => OnTrackChange?.Invoke(this, new TrackChangeEventArgs()
                    {
                        OldTrack = Track,
                        NewTrack = await SpotifyLatestStatus.GetTrack()
                    }));
                }
                if (Track.CurrentPosition != null || newestTrack != null)
                {
                    await Task.Run(() => OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs()
                    {
                        TrackTime = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : 0
                    }));
                }
            }
            if (newestTrack != null)
            {
                newestTrack.CurrentPosition = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : (int?)null;
                Track = newestTrack;
            }
            EventTimer.Start();
        }
Exemple #5
0
        static public void Select(SignalTrack t)
        {
            Unselect();
            Selected = t;

            if (Selected.Border != null)
            {
                Selected.Border.BorderBrush = Defaults.Brushes.Highlight;
            }

            OnTrackChange?.Invoke(Selected, null);
        }
Exemple #6
0
 protected void OnTrackChangeHandler()
 {
     OnTrackChange?.Invoke();
 }
Exemple #7
0
        private async Task MediaPlaybackListOnCurrentItemChangedAsync(MediaPlaybackList sender, CurrentMediaPlaybackItemChangedEventArgs args)
        {
            var track = args.NewItem?.Source.AsBaseTrack();

            // If there is no new item, don't do anything
            if (track == null)
            {
                return;
            }

            // Invoke the track change method
            OnTrackChange?.Invoke(track);

            // Update the live tile
            UpdateTile(track);

            await Task.Run(async() =>
            {
                string currentUsageLimit;
                var memoryUsage = MemoryManager.AppMemoryUsage / 1024 / 1024;

                if (memoryUsage > 512)
                {
                    currentUsageLimit = "More than 512MB";
                }
                else if (memoryUsage > 256)
                {
                    currentUsageLimit = "More than 256MB";
                }
                else if (memoryUsage > 128)
                {
                    currentUsageLimit = "More than 128MB";
                }
                else
                {
                    currentUsageLimit = "Less than 128MB";
                }

                _telemetryService.TrackEvent("Current Song Change", new Dictionary <string, string>
                {
                    { "Current Usage", currentUsageLimit },
                    { "Free", SystemInformation.AvailableMemory.ToString(CultureInfo.InvariantCulture) },
                    { "Track Type", track.ServiceType.ToString() ?? "Null" },
                    { "Device", SystemInformation.DeviceFamily },
                    { "Current Version / First Version", SystemInformation.FirstVersionInstalled.ToFormattedString() + "/" + SystemInformation.ApplicationVersion.ToFormattedString() },
                });

                try
                {
                    // Only perform logic if soundbyte account is connected
                    // and the track type is not a local track
                    if (SoundByteService.Current.IsSoundByteAccountConnected &&
                        track.ServiceType != ServiceTypes.Local)
                    {
                        await SoundByteService.Current.PostItemAsync(ServiceTypes.SoundByte, "history", track);
                    }
                }
                catch (Exception ex)
                {
                    _telemetryService.TrackException(ex);
                }
            });

            try
            {
                // The correct playlist
                var tempPlaylist = new List <BaseSoundByteItem>();
                tempPlaylist.AddRange(_mediaPlaybackList.ShuffleEnabled
                    ? _mediaPlaybackList.ShuffledItems.Select(x => new BaseSoundByteItem(x.Source.AsBaseTrack())).ToList()
                    : _mediaPlaybackList.Items.Select(x => new BaseSoundByteItem(x.Source.AsBaseTrack())).ToList());

                // Update roaming activity
                await App.RoamingService.UpdateActivityAsync(_playlistSource, track, tempPlaylist, _playlistToken, null, IsPlaylistShuffled());

                // Update the resume files
                var roamingFolder = SettingsService.Instance.SyncLastPlayed
                    ? ApplicationData.Current.RoamingFolder
                    : ApplicationData.Current.LocalFolder;

                // Save in file
                var playbackFile = await roamingFolder.CreateFileAsync("currentPlayback.txt", CreationCollisionOption.OpenIfExists);

                await FileIO.WriteTextAsync(playbackFile, ProtocolHelper.EncodeTrackProtocolItem(new ProtocolHelper.TrackProtocolItem(_playlistSource, new BaseSoundByteItem(track), tempPlaylist, _playlistToken, null, _mediaPlaybackList.ShuffleEnabled), false));
            }
            catch
            {
                // Ignore
            }

            // Find the index of this item and see if we are near the end
            var currentIndex = _mediaPlaybackList.ShuffleEnabled
                ? _mediaPlaybackList.ShuffledItems.ToList().IndexOf(args.NewItem)
                : _mediaPlaybackList.Items.IndexOf(args.NewItem);

            var maxIndex = _mediaPlaybackList.ShuffleEnabled
                ? _mediaPlaybackList.ShuffledItems.Count - 1
                : _mediaPlaybackList.Items.Count - 1;

            // When we are three items from the end, load more items
            if (currentIndex >= maxIndex - 3)
            {
                try
                {
                    var newItems = await _playlistSource.GetItemsAsync(50, _playlistToken);

                    _playlistToken = newItems.Token;

                    if (newItems.IsSuccess)
                    {
                        // Loop through all the tracks and add them to the playlist
                        foreach (var newTrack in newItems.Items)
                        {
                            if (newTrack.Type != ItemType.Track)
                            {
                                continue;
                            }

                            try
                            {
                                BuildMediaItem(newTrack.Track);
                            }
                            catch (Exception e)
                            {
                                _telemetryService.TrackEvent("Playback Item Addition Failed", new Dictionary <string, string>
                                {
                                    { "TrackID", newTrack.Track.TrackId },
                                    { "TrackService", newTrack.Track.ServiceType.ToString() },
                                    { "ErrorMessage", e.Message }
                                });
                            }
                        }
                    }
                }
                catch
                {
                    _playlistToken = "eol";
                }
            }
        }
Exemple #8
0
        /// <summary>
        ///     Occurs when a current media playback item changes.
        /// </summary>
        private async void MediaPlaybackListOnCurrentItemChanged(MediaPlaybackList sender, CurrentMediaPlaybackItemChangedEventArgs args)
        {
            var track = args.NewItem?.Source.AsBaseTrack();

            // If there is no new item, don't do anything
            if (track == null)
            {
                return;
            }

            // Invoke the track change method
            OnTrackChange?.Invoke(track);

            Debug.WriteLine(JsonConvert.SerializeObject(track));

            await Task.Run(async() =>
            {
                string currentUsageLimit;
                var memoryUsage = MemoryManager.AppMemoryUsage / 1024 / 1024;

                if (memoryUsage > 512)
                {
                    currentUsageLimit = "More than 512MB";
                }
                else if (memoryUsage > 256)
                {
                    currentUsageLimit = "More than 256MB";
                }
                else if (memoryUsage > 128)
                {
                    currentUsageLimit = "More than 128MB";
                }
                else
                {
                    currentUsageLimit = "Less than 128MB";
                }

                App.Telemetry.TrackEvent("Current Song Change", new Dictionary <string, string>
                {
                    { "Current Usage", currentUsageLimit },
                    { "Free", SystemInformation.AvailableMemory.ToString(CultureInfo.InvariantCulture) },
                    { "Track Type", track.ServiceType.ToString() ?? "Null" },
                    { "Device", SystemInformation.DeviceFamily },
                    { "Current Version / First Version", SystemInformation.FirstVersionInstalled.ToFormattedString() + "/" + SystemInformation.ApplicationVersion.ToFormattedString() },
                });

                try
                {
                    // Only perform logic if soundbyte account is connected
                    // and the track type is not a local track
                    if (SoundByteService.Current.IsSoundByteAccountConnected &&
                        track.ServiceType != ServiceType.Local)
                    {
                        await SoundByteService.Current.PostItemAsync(ServiceType.SoundByte, "history", track);
                    }
                }
                catch (Exception ex)
                {
                    var i = 0;
                }
            });

            // Update roaming activity
            await App.RoamingService.UpdateActivityAsync(_playlistSource, track,
                                                         MediaPlaybackList.Items.Select(x => x.Source.AsBaseTrack()), _playlistToken);

            // Find the index of this item and see if we are near the end
            var currentIndex = MediaPlaybackList.ShuffleEnabled
                ? MediaPlaybackList.ShuffledItems.ToList().IndexOf(args.NewItem)
                : MediaPlaybackList.Items.IndexOf(args.NewItem);

            var maxIndex = MediaPlaybackList.ShuffleEnabled
                ? MediaPlaybackList.ShuffledItems.Count - 1
                : MediaPlaybackList.Items.Count - 1;

            // When we are three items from the end, load more items
            if (currentIndex >= maxIndex - 3)
            {
                var newItems = await _playlistSource.GetItemsAsync(50, _playlistToken);

                _playlistToken = newItems.Token;

                if (newItems.IsSuccess)
                {
                    // Loop through all the tracks and add them to the playlist
                    foreach (var newTrack in newItems.Items)
                    {
                        try
                        {
                            BuildMediaItem(newTrack);
                        }
                        catch (Exception e)
                        {
                            App.Telemetry.TrackEvent("Playback Item Addition Failed", new Dictionary <string, string>
                            {
                                { "TrackID", newTrack.TrackId },
                                { "TrackService", newTrack.ServiceType.ToString() },
                                { "ErrorMessage", e.Message }
                            });
                        }
                    }
                }
            }
        }
Exemple #9
0
        //***********************************************************************************************************************************************************************************************************

        /// <summary>
        /// Check if an event has to be raised.
        /// </summary>
        private void ElapsedTick(object sender, ElapsedEventArgs e)
        {
            if (_tmpPlaybackStatus == null)
            {
                UpdateCurrentPlaybackStatus();
                _tmpPlaybackStatus = CurrentPlaybackStatus;
                _eventTimer.Start();
                return;
            }
            UpdateCurrentPlaybackStatus();

            if (CurrentPlaybackStatus == null)
            {
                _eventTimer.Start();
                return;
            }
            if (!CurrentPlaybackStatus.IsPlaying && CurrentPlaybackStatus.Track == null)
            {
                _eventTimer.Start();
                return;
            }
            if (CurrentPlaybackStatus.IsPlaying && _tmpPlaybackStatus.Track != null && CurrentPlaybackStatus.Track == null)
            {
                OnTrackChange?.Invoke(this, new PlayerTrackChangeEventArgs()
                {
                    OldTrack = _tmpPlaybackStatus.Track,
                    NewTrack = null
                });
            }
            if (CurrentPlaybackStatus.Track != null && _tmpPlaybackStatus.Track != null)
            {
                if (CurrentPlaybackStatus.Track?.TrackID != _tmpPlaybackStatus.Track?.TrackID || CurrentPlaybackStatus.Track.Duration != _tmpPlaybackStatus.Track.Duration)
                {
                    OnTrackChange?.Invoke(this, new PlayerTrackChangeEventArgs()
                    {
                        OldTrack = _tmpPlaybackStatus.Track,
                        NewTrack = CurrentPlaybackStatus.Track
                    });
                }
            }
            if (CurrentPlaybackStatus.IsPlaying != _tmpPlaybackStatus.IsPlaying)
            {
                OnPlayStateChange?.Invoke(this, new PlayerPlayStateEventArgs()
                {
                    Playing = CurrentPlaybackStatus.IsPlaying
                });
            }
            if (CurrentPlaybackStatus.DeviceVolumePercent != _tmpPlaybackStatus.DeviceVolumePercent)
            {
                OnVolumeChange?.Invoke(this, new PlayerVolumeChangeEventArgs()
                {
                    OldVolume = _tmpPlaybackStatus.DeviceVolumePercent,
                    NewVolume = CurrentPlaybackStatus.DeviceVolumePercent
                });
            }
            if (CurrentPlaybackStatus.Progress != _tmpPlaybackStatus.Progress)
            {
                OnTrackTimeChange?.Invoke(this, new PlayerTrackTimeChangeEventArgs()
                {
                    TrackTime = CurrentPlaybackStatus.Progress
                });
            }

            _tmpPlaybackStatus = CurrentPlaybackStatus;
            _eventTimer.Start();
            OnPropertyChanged("CurrentPlaybackStatus");
            OnPropertyChanged("CurrentTrack");
        }
Exemple #10
0
        public async Task TriggerEvents()
        {
            // avoid concurrences
            if (_processingEvents == true)
            {
                return;
            }

            _processingEvents = true;

            SpotifyLatestStatus = await SpotifyProcess.GetSpotifyStatus();

            if (SpotifyLatestStatus?.CurrentTrack != null)
            {
                var newestTrack = SpotifyLatestStatus.CurrentTrack;
                if (Track != null)
                {
                    if (newestTrack.Playing != Track.Playing)
                    {
                        if (newestTrack.Playing)
                        {
                            SongTimer?.Start();
                        }
                        else
                        {
                            SongTimer?.Stop();
                        }

                        _ = Task.Run(() => OnPlayStateChange?.Invoke(this, new PlayStateEventArgs()
                        {
                            Playing = newestTrack.Playing
                        }));
                    }
                    if (!newestTrack.Equals(Track))
                    {
                        SongTimer?.Start();
                        _ = Task.Run(async() => OnTrackChange?.Invoke(this, new TrackChangeEventArgs()
                        {
                            OldTrack = Track,
                            NewTrack = await SpotifyLatestStatus.GetTrack()
                        }));
                    }
                    if (Track.CurrentPosition != null || newestTrack != null)
                    {
                        _ = Task.Run(() => OnTrackTimeChange?.Invoke(this, new TrackTimeChangeEventArgs()
                        {
                            TrackTime = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : 0
                        }));
                    }
                }

                if (newestTrack != null)
                {
                    newestTrack.CurrentPosition = newestTrack.Equals(Track) ? Track?.CurrentPosition ?? 0 : (int?)null;
                    Track = newestTrack;
                }
            }

            EventTimerStart();

            _processingEvents = false;
        }