Example #1
0
        public Player()
        {
            Instance = this;
            Engine   = new ISoundEngine(SoundOutputDriver.AutoDetect, SoundEngineOptionFlag.DefaultOptions, Config.GetInstance().DeviceId);
            Playlist = new Playlist();

            //Playlist.Tracks.CollectionChanged += OnPlaylistChanged;
            new Thread(() => {
                while (true)
                {
                    PlayerThread(); Thread.Sleep(250);
                }
            }).Start();
        }
Example #2
0
        private void PlayerThread()
        {
            Track track = Playlist.Tracks.Where(x => x.State == TrackState.Ready).FirstOrDefault();

            IsPlaying = CurrentISound == null ? false : !CurrentISound.Finished;
            if (track != null && !IsPlaying)
            {
                Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track is playing: " + track);
                TrackChanged(this, new PlayerEventArgs()
                {
                    Track = track
                });
                Playlist.Tracks.Remove(track);
                CurrentTrack = track;
                CurrentTrack.PlayPosition = TimeSpan.FromMilliseconds(0);
                CurrentISound             = Engine.Play2D(Config.GetInstance().CacheDir + track.Id + ".mp3");
                IsPlaying = CurrentISound == null ? false : !CurrentISound.Finished;
            }
            else if (!IsPlaying)
            {
                CurrentTrack = null;
            }
            else if (IsPlaying)
            {
                CurrentTrack.PlayPosition = TimeSpan.FromMilliseconds((double)CurrentISound.PlayPosition);
            }

            List <Track> tracksToRemove = Playlist.Tracks.Where(x => x.State == TrackState.Failed).ToList <Track>();

            foreach (Track t in tracksToRemove)
            {
                Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Failed track has been removed: " + t);
                Playlist.Tracks.Remove(t);
            }

            foreach (Track t in Playlist.Tracks.Where(x => x.State == TrackState.Unknown))
            {
                Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track has been enqueued: " + t);
                if (File.Exists(Config.GetInstance().CacheDir + t.GetHash() + ".mp3"))
                {
                    Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track has been loaded from cache: " + t);
                    t.State = TrackState.Ready;
                    Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track has state: {0} {1}", t, t.State);
                    TrackStateChanged(this, new PlayerEventArgs()
                    {
                        Track = t
                    });
                }
                else
                {
                    new Thread(() =>
                    {
                        t.State = TrackState.Downloading;
                        TrackStateChanged(this, new PlayerEventArgs()
                        {
                            Track = t
                        });

                        byte[] data = DataProviderManager.Instance.Download(t);
                        if ((data != null) && !File.Exists(Config.GetInstance().CacheDir + t.Id + ".mp3"))
                        {
                            File.WriteAllBytes(Config.GetInstance().CacheDir + t.Id + ".mp3", data);
                            File.AppendAllText(Config.GetInstance().CacheDir + "hashmap.txt", t.Id + "|" + t.Singer.Trim() + "|" + t.Title.Trim() + "|" + t.Duration.ToString() + "\r\n");
                        }
                        t.State = data != null ? TrackState.Ready : TrackState.Failed;
                        Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track has state: {0} {1}", t, t.State);

                        TrackStateChanged(this, new PlayerEventArgs()
                        {
                            Track = t
                        });
                    }).Start();
                }
            }

            if (!IsPlaying && Playlist.Tracks.Count == 0 && Config.GetInstance().PlayRandomFromCache)
            {
                Track t = DataProviderManager.Instance.GetRandomTrack();
                if (t != null)
                {
                    Debug.Print("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + "Track was randomly chosen: {0}", t);

                    t.State            = TrackState.Ready;
                    t.IsRandomlyChosen = true;
                    Playlist.Tracks.Add(t);
                    TrackStateChanged(this, new PlayerEventArgs()
                    {
                        Track = t
                    });
                }
            }
        }
Example #3
0
 public void RestorePoints()
 {
     foreach (User user in userDictionary.Keys)
     {
         if (user.ActionPoints < Config.GetInstance().InitialActionPoints)
         {
             userDictionary.Where(x => x.Key.UserIpAddress == user.UserIpAddress).First().Key.ActionPoints = Config.GetInstance().InitialActionPoints;
         }
         userDictionary.Where(x => x.Key.UserIpAddress == user.UserIpAddress).First().Key.VolumeLevelDiffs = 0;
     }
 }