Example #1
0
 public async Task UpdateNowPlaying(QueueSong queue)
 {
     try
     {
         await _service.ScrobbleNowPlayingAsync(queue.Song.Name, queue.Song.Artist.Name,
             DateTime.UtcNow, queue.Song.Duration);
     }
     catch
     {
     }
 }
        public static async void PlaySong(QueueSong queueSong)
        {
            if (queueSong == null)
                return;

//            Insights.Track("Play Song", new Dictionary<string, string>
//            {
//                {"Name",queueSong.Song.Name},
//                {"ArtistName",queueSong.Song.ArtistName},
//                {"ProviderId",queueSong.Song.ProviderId}
//            });

            if (!App.Current.AudioServiceConnection.IsPlayerBound)
            {
                if (!await App.Current.StartPlaybackServiceAsync())
                    return;
            }

            App.Current.AudioServiceConnection.GetPlaybackService().PlaySong(queueSong);
        }
        private async Task AddToQueueAsync(Song song)
        {
            if (QueueSongs.Any(p => p.SongId == song.Id)) return;

            var prev = QueueSongs.LastOrDefault();

            // Create the new queue entry
            var newQueue = new QueueSong
            {
                SongId = song.Id,
                PrevId = prev == null ? 0 : prev.Id,
                Song = song
            };

            using (var bg = _bgFunc())
            {
                await bg.InsertAsync(newQueue);
                if (prev != null)
                {
                    prev.NextId = newQueue.Id;
                    await bg.UpdateItemAsync(prev);
                }

                QueueSongsLookup.Add(newQueue.Id, newQueue);
                QueueSongs.Add(newQueue);
            }
        }
        public async Task DeleteFromQueueAsync(QueueSong songToRemove)
        {
            QueueSong previousModel;

            if (songToRemove == null)
            {
                return;
            }

            if (_lookupMap.TryGetValue(songToRemove.PrevId, out previousModel))
            {
                previousModel.NextId = songToRemove.NextId;
                await _bgSqlService.UpdateItemAsync(previousModel);
            }

            if (_lookupMap.TryGetValue(songToRemove.ShufflePrevId, out previousModel))
            {
                previousModel.ShuffleNextId = songToRemove.ShuffleNextId;
                await _bgSqlService.UpdateItemAsync(previousModel);
            }

            QueueSong nextModel;

            if (_lookupMap.TryGetValue(songToRemove.NextId, out nextModel))
            {
                nextModel.PrevId = songToRemove.PrevId;
                await _bgSqlService.UpdateItemAsync(nextModel);
            }

            if (_lookupMap.TryGetValue(songToRemove.ShuffleNextId, out nextModel))
            {
                nextModel.ShufflePrevId = songToRemove.ShufflePrevId;
                await _bgSqlService.UpdateItemAsync(nextModel);
            }

            await _dispatcher.RunAsync(
                () =>
                {
                    PlaybackQueue.Remove(songToRemove);
                    ShufflePlaybackQueue.Remove(songToRemove);
                });
            _lookupMap.TryRemove(songToRemove.Id, out songToRemove);

            // Delete from database
            await _bgSqlService.DeleteItemAsync(songToRemove);
        }
        public async Task<QueueSong> AddToQueueAsync(Song song, QueueSong position = null, bool shuffleInsert = true)
        {
            if (song == null)
            {
                return null;
            }

            var rnd = new Random(DateTime.Now.Millisecond);
            QueueSong prev = null;
            QueueSong shufflePrev = null;
            QueueSong next = null;
            QueueSong shuffleNext = null;
            var shuffleIndex = -1;
            var normalIndex = -1;

            if (position != null)
            {
                shuffleIndex = ShufflePlaybackQueue.IndexOf(position) + 1;
                normalIndex = PlaybackQueue.IndexOf(position) + 1;
            }

            var insert = normalIndex > -1 && normalIndex < PlaybackQueue.Count;
            var insertShuffle = shuffleIndex > -1 && shuffleInsert;
            var shuffleLastAdd = shuffleIndex == ShufflePlaybackQueue.Count;

            if (insert)
            {
                next = PlaybackQueue.ElementAtOrDefault(normalIndex);
                if (next != null)
                {
                    _lookupMap.TryGetValue(next.PrevId, out prev);
                }
            }
            else
            {
                prev = PlaybackQueue.LastOrDefault();
            }

            if (insertShuffle)
            {
                if (shuffleLastAdd)
                {
                    shufflePrev = ShufflePlaybackQueue.ElementAtOrDefault(ShufflePlaybackQueue.Count - 1);
                }
                else
                {
                    shuffleNext = ShufflePlaybackQueue.ElementAtOrDefault(shuffleIndex);
                    if (shuffleNext != null)
                    {
                        _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev);
                    }
                }
            }
            else
            {
                if (ShufflePlaybackQueue.Count > 1)
                {
                    shuffleIndex = rnd.Next(1, ShufflePlaybackQueue.Count - 1);
                    shuffleNext = ShufflePlaybackQueue.ElementAt(shuffleIndex);

                    _lookupMap.TryGetValue(shuffleNext.ShufflePrevId, out shufflePrev);
                }
                else
                {
                    shuffleLastAdd = true;
                    shufflePrev = prev;
                }
            }

            // Create the new queue entry
            var newQueue = new QueueSong
            {
                SongId = song.Id,
                NextId = next == null ? 0 : next.Id,
                PrevId = prev == null ? 0 : prev.Id,
                ShuffleNextId = shuffleNext == null ? 0 : shuffleNext.Id,
                ShufflePrevId = shufflePrev == null ? 0 : shufflePrev.Id,
                Song = song
            };

            // Add it to the database
            await _bgSqlService.InsertAsync(newQueue).ConfigureAwait(false);

            if (next != null)
            {
                // Update the prev id of the queue that was replaced
                next.PrevId = newQueue.Id;
                await _bgSqlService.UpdateItemAsync(next).ConfigureAwait(false);
            }

            if (prev != null)
            {
                // Update the next id of the previous tail
                prev.NextId = newQueue.Id;
                await _bgSqlService.UpdateItemAsync(prev).ConfigureAwait(false);
            }

            if (shuffleNext != null)
            {
                shuffleNext.ShufflePrevId = newQueue.Id;
                await _bgSqlService.UpdateItemAsync(shuffleNext).ConfigureAwait(false);
            }

            if (shufflePrev != null)
            {
                shufflePrev.ShuffleNextId = newQueue.Id;
                await _bgSqlService.UpdateItemAsync(shufflePrev).ConfigureAwait(false);
            }

            // Add the new queue entry to the collection and map
            await _dispatcher.RunAsync(
                () =>
                {
                    if (insert)
                    {
                        try
                        {
                            PlaybackQueue.Insert(normalIndex, newQueue);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            PlaybackQueue.Add(newQueue);
                        }
                    }
                    else
                    {
                        PlaybackQueue.Add(newQueue);
                    }

                    if (shuffleLastAdd || !shuffleInsert)
                    {
                        ShufflePlaybackQueue.Add(newQueue);
                    }
                    else
                    {
                        try
                        {
                            ShufflePlaybackQueue.Insert(shuffleIndex, newQueue);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            ShufflePlaybackQueue.Add(newQueue);
                        }
                    }
                }).ConfigureAwait(false);

            _lookupMap.TryAdd(newQueue.Id, newQueue);

            return newQueue;
        }
        public async void PlaySong(QueueSong song)
        {
            if (song == null || song.Song == null)
            {
                CurtainPrompt.ShowError("Song seems to be empty...");
                return;
            }

            Insights.Track(
                "Play Song",
                new Dictionary<string, string>
                {
                    {"Name", song.Song.Name},
                    {"ArtistName", song.Song.ArtistName},
                    {"ProviderId", song.Song.ProviderId}
                });

            if (_isShutdown)
            {
                await AddMediaPlayerEventHandlers();
            }

            _appSettings.Write("RadioId", null);
            _appSettings.Write("RadioMode", false);
            _appSettings.Write(PlayerConstants.CurrentTrack, song.Id);

            var message = new ValueSet {{PlayerConstants.StartPlayback, null}};
            BackgroundMediaPlayer.SendMessageToBackground(message);

            RaiseEvent(TrackChanged);
        }
        public async void PlaySong(QueueSong queue)
        {
            _appSettingsHelper.Write(PlayerConstants.CurrentTrack, queue.Id);
            if (_alreadyStarted)
                _player.Reset();
            else
            {
                StartForeground(NotificationId, BuildNotification());
            }
            _alreadyStarted = true;

            try
            {
                await _player.SetDataSourceAsync(ApplicationContext, Uri.Parse(queue.Song.AudioUrl));
                MediaPlayerState = MediaPlayerState.Buffering;
                _player.PrepareAsync();
            }
            catch (Exception e)
            {
                MediaPlayerState = MediaPlayerState.None;
                Log.Error("MUSIC SERVICE", "Player error", e);
            }
        }
Example #8
0
        private async void InternalStartTrack(QueueSong track)
        {
            _transportControls.IsPreviousEnabled = !IsRadioMode;

            // If the flac media source adapter is not null, disposed of it
            // since we won't be using it

            if (_currentMediaSourceAdapter != null)
            {
                _currentMediaSourceAdapter.Dispose();
                _currentMediaSourceAdapter = null;
            }

            if (track.Song.IsStreaming)
            {
                _mediaPlayer.SetUriSource(new Uri(track.Song.AudioUrl));
            }
            else
            {
                var file = await StorageFile.GetFileFromPathAsync(track.Song.AudioUrl);

                if (file != null)
                {
                    try
                    {
                        if (file.FileType != ".flac")
                        {
                            _mediaPlayer.SetFileSource(file);
                        }
                        else
                        {
                            // Use custom media source for FLAC support
                            _currentMediaSourceAdapter =
                                await FlacMediaSourceAdapter.CreateAsync(file);
                            BackgroundMediaPlayer.Current.SetMediaSource(_currentMediaSourceAdapter.MediaSource);
                        }
                    }
                    catch
                    {
                        SkipToNext();
                    }
                }
                else if (CurrentTrack.NextId != 0 && CurrentTrack.PrevId != 0)
                    SkipToNext();
            }
            _mediaPlayer.Play();
        }
Example #9
0
        public async void StartTrack(QueueSong track, bool ended  = false)
        {
            if (track == null) return;

            try
            {
                ScrobbleOnMediaEnded();
            }
            catch { }

            if (IsRadioMode && _station != null && _currentTrack != null && _currentTrack.Song.ProviderId.Contains("gn."))
            {
                // Create the station manager
                var radioStationManager = new RadioStationManager(_station.GracenoteId, _station.Id,
                    CreateCollectionSqlService,
                    CreatePlayerSqlService);

                var trackId = _currentTrack.Song.ProviderId.Replace("gn.", "");

                if (!ended)
                    await radioStationManager.SkippedAsync(trackId);
                else
                    await radioStationManager.PlayedAsync(trackId);

                await radioStationManager.UpdateQueueAsync();
                track = radioStationManager.QueueSongs[0];
                _appSettingsHelper.Write(PlayerConstants.CurrentTrack, track.Id);
                OnEvent(QueueUpdated);
            }

            _currentTrack = track;
            UpdateTile();

            if (TrackChanged != null)
                OnTrackChanged(_currentTrack.SongId);

            _mediaPlayer.Pause();
            if (_mediaPlayer.Position > TimeSpan.Zero)
                _mediaPlayer.Position = TimeSpan.Zero;
            _transportControls.PlaybackStatus = MediaPlaybackStatus.Changing;

            if (IsRadioMode)
                StartRadioTrack(track);
            else
                InternalStartTrack(track);
        }
Example #10
0
        public async void StartRadioTrack(QueueSong track)
        {
            if (track == null) return;

            switch (track.Song.SongState)
            {
                case SongState.BackgroundMatching:
                    _transportControls.IsPlayEnabled = false;
                    _transportControls.IsNextEnabled = false;
                    _transportControls.IsPreviousEnabled = false;
                    OnEvent(MatchingTrack);

                    // Create the station manager
                    var radioStationManager = new RadioStationManager(_station.GracenoteId, _station.Id,
                        CreateCollectionSqlService,
                        CreatePlayerSqlService);

                    var matched = await radioStationManager.MatchSongAsync(track.Song);
                    _transportControls.IsNextEnabled = true;
                    
                    if (!matched)
                    {
                        SkipToNext();
                        return;
                    }
                    break;
                case SongState.NoMatch:
                    SkipToNext();
                    break;
            }

            InternalStartTrack(track);
        }
 public Task DeleteFromQueueAsync(QueueSong songToRemove)
 {
     throw new NotImplementedException();
 }
 public Task<QueueSong> AddToQueueAsync(Song song, QueueSong position = null, bool shuffleInsert = true)
 {
     throw new NotImplementedException();
 }