Exemplo n.º 1
0
        private async void LookForNextSong()
        {
            string uri;

            if (_playerStatus.IsShuffleEnabled)
            {
                uri = string.Format("playlists/{0}/songs/{1}/nextshuffle", _playerStatus.PlaylistId, _playerStatus.SongId);
            }
            else
            {
                uri = string.Format("playlists/{0}/songs/{1}/next", _playerStatus.PlaylistId, _playerStatus.SongId);
            }

            using (var client = new HttpClient())
            {
                InitializeClient(client);
                var response = await client.GetAsync(uri);

                if (response.IsSuccessStatusCode)
                {
                    var playlistSongDto = await response.Content.ReadAsAsync <PlaylistSongDto>();

                    if (playlistSongDto != null)
                    {
                        var entities = new SoulstoneEntities();
                        var song     = entities.Songs.Find(playlistSongDto.Song.Id);
                        NextSong = song;
                        var message = string.Format("Next song file name found: {0}, Next song id: {1}", NextSong.FileName, NextSong.Id);
                        IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Information.Write(message);
                        ConsoleLog(message);

                        if (!_playerStatus.IsPlaying)
                        {
                            message = "Play Next file since there is no playing in progress";
                            IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Information.Write(message);
                            ConsoleLog(message);
                            PlayNextFile();
                        }

                        return;
                    }
                }

                NextSong = null;
                var msg = "There are no other songs in the playlist";
                ConsoleLog(msg);
                IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Information.Write(msg);
            }
        }
Exemplo n.º 2
0
        private void AddSong(SoulstoneEntities entities, MusicTrack musicTrack, string fileHash, string fileName)
        {
            var song = new Song
            {
                Artist   = musicTrack.Artist,
                Album    = musicTrack.Album,
                Title    = musicTrack.Title,
                Artwork  = musicTrack.Artwork,
                Bitrate  = musicTrack.Bitrate,
                Duration = musicTrack.Duration,
                Genre    = musicTrack.Genre,
                Year     = musicTrack.Year,
                Hash     = fileHash,
                FileName = fileName
            };

            entities.Songs.Add(song);
        }
Exemplo n.º 3
0
        private void btnScan_Click(object sender, EventArgs e)
        {
            var folderPath = txtFolderPath.Text;

            ScanStarted();
            if (Directory.Exists(folderPath))
            {
                ConsoleLog("Computing hash of files...");
                _filesHash = Sha2Calculator.ComputeFolderHash(txtFolderPath.Text);
                ConsoleLog("Processing files...");
                var filePaths = Directory.GetFiles(folderPath, "*.mp3");
                var entities  = new SoulstoneEntities();
                foreach (var filePath in filePaths)
                {
                    var fileName = Path.GetFileName(filePath).ToLower();
                    var fileHash = _filesHash[fileName];
                    if (!SongExists(entities, fileHash))
                    {
                        var musicTrack = Id3Reader.Instance.GetMusicTrackFromId3(filePath);
                        if (musicTrack == null)
                        {
                            continue;
                        }

                        AddSong(entities, musicTrack, fileHash, fileName);
                        entities.SaveChanges();
                        ConsoleLog(string.Format("{0} : added to the database", Path.GetFileName(filePath)));
                    }
                    else
                    {
                        ConsoleLog(string.Format("{0} : already exists on database", fileName));
                    }

                    _totalFiles += 1;
                }
            }

            ScanFinished();
        }
Exemplo n.º 4
0
 private bool SongExists(SoulstoneEntities entities, string fileHash)
 {
     return(entities.Songs.Any(s => s.Hash == fileHash));
 }
Exemplo n.º 5
0
        private async void InitializeSignalR(string url)
        {
            if (_hubConnection != null)
            {
                _hubConnection.Stop();
            }

            _hubConnection = new HubConnection(url);
            _soulstoneHub  = _hubConnection.CreateHubProxy("soulstoneHub");

            _soulstoneHub.On <int, int, int>("PlaySong", (hostId, playlistId, songId) =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        var entities = new SoulstoneEntities();
                        var song     = entities.Songs.Find(songId);
                        SetPlayerStatusInfo(song);
                        _playerStatus.PlaylistId = playlistId;
                        _playerStatus.IsPlaying  = true;
                        if (song != null)
                        {
                            PlayFile(song.FileName);

                            LookForNextSong();
                        }
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("Play", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        ConsoleLog("Play");
                        Player.Dispatcher.BeginInvoke(new Action(() => Player.Play()));
                        _playerStatus.IsPlaying = true;
                        _soulstoneHub.Invoke("PlayerStatus", HostId, _playerStatus);
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("Stop", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() => Player.Stop()));
                        ConsoleLog("Stop");
                        _playerStatus.IsPlaying = false;
                        _soulstoneHub.Invoke("PlayerStatus", HostId, _playerStatus);
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("Pause", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() => Player.Pause()));
                        _playerStatus.IsPlaying = false;
                        ConsoleLog("Pause");
                        _soulstoneHub.Invoke("PlayerStatus", HostId, _playerStatus);
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int, int>("Volume", (hostId, volumeValue) =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            UpdateVolume(volumeValue * 0.1);
                        }));
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("Mute", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            ToogleMute();
                        }));
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("GetPlayerStatus", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        _soulstoneHub.Invoke("PlayerStatus", hostId, _playerStatus);
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });


            _soulstoneHub.On <int>("Shuffle", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            _playerStatus.IsShuffleEnabled = !_playerStatus.IsShuffleEnabled;
                            ConsoleLog(string.Format("Shuffle:{0}", _playerStatus.IsShuffleEnabled));
                            LookForNextSong();
                        }));
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            _soulstoneHub.On <int>("NextSong", hostId =>
            {
                try
                {
                    if (hostId == HostId)
                    {
                        Player.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            PlayNextFile();
                            LookForNextSong();
                        }));
                    }
                }
                catch (Exception ex)
                {
                    IocUnityContainer.Instance.Resolve <ILogManager>().DefaultLogger.Error.Write(ex.Message, ex);
                    ConsoleLog("An error has occurred: " + ex.Message);
                }
            });

            try
            {
                await _hubConnection.Start();
            }
            catch
            {
                ConsoleLog("SignalR connection could not be established");
                throw;
            }

            ConsoleLog("SignalR connection established");
        }