public void Prepare()
 {
     if (_rendererBuildingState == RendererBuildingStateBuilt)
     {
         _player.Stop();
     }
     _rendererBuilder.Cancel();
     _videoFormat           = null;
     _videoRenderer         = null;
     _rendererBuildingState = RendererBuildingStateBuilding;
     MaybeReportPlayerState();
     _rendererBuilder.BuildRenderers(this);
 }
Пример #2
0
        private void Stop()
        {
            if (Player == null)
            {
                return;
            }

            if (Player.PlayWhenReady)
            {
                Player.Stop();
                if (this.remoteControlClient != null)
                {
                    this.remoteControlClient.SetPlaybackState(RemoteControlPlayState.Stopped);
                }
            }

            //Player.Reset();
            this.paused = false;
            this.StopForeground(true);
            this.ReleaseWifiLock();
            this.UnregisterRemoteClient();
        }
Пример #3
0
        private async void Play()
        {
            try
            {
                if ((CurrentTrack == null) && (CurrentTrackId == null))
                {
                    if (Queue.Count > 0)
                    {
                        Song firstOrDefault = Queue.FirstOrDefault();
                        if (firstOrDefault != null)
                        {
                            CurrentTrack   = firstOrDefault.Url;
                            CurrentTrackId = firstOrDefault.Id;
                        }
                        else
                        {
                            CurrentTrack   = null;
                            CurrentTrackId = null;
                        }
                    }
                    else
                    {
                        return;
                    }
                }

                Song currentSong = Queue.FirstOrDefault(p => p.Url == CurrentTrack);
                if (currentSong == null || CurrentTrack == null)
                {
                    currentSong = Queue.FirstOrDefault(p => p.Id == CurrentTrackId);
                }

                Player?.Stop();

                Player = null;

                if (this.paused && Player != null)
                {
                    this.paused = false;
                    //We are simply paused so just start again
                    Player.PlayWhenReady = true;
                    if (currentSong != null)
                    {
                        if (!string.IsNullOrEmpty(currentSong.Artist) && !string.IsNullOrEmpty(currentSong.Title))
                        {
                            this.StartForeground(currentSong.Artist + " - " + currentSong.Title);
                        }
                        else if (!string.IsNullOrEmpty(currentSong.Artist))
                        {
                            this.StartForeground(currentSong.Artist);
                        }
                        else if (!string.IsNullOrEmpty(currentSong.Title))
                        {
                            this.StartForeground(currentSong.Title);
                        }
                    }

                    this.RegisterRemoteClient();
                    this.remoteControlClient.SetPlaybackState(RemoteControlPlayState.Playing);
                    this.UpdateMetadata();
                    return;
                }

                if (Player == null)
                {
                    this.IntializePlayer();
                }

                if (Player.PlayWhenReady)
                {
                    this.Stop();
                }
                else
                {
                    //Player.Reset();
                    this.paused = false;
                    this.StopForeground(true);
                    this.ReleaseWifiLock();
                }

                this.starting = true;
                var netEase = Mvx.Resolve <INetEase>();
                var newSong = new SongNetease
                {
                    Artist = currentSong.Artist,
                    Title  = currentSong.Title,
                    Url    = CurrentTrack,
                    Id     = currentSong.Id
                };


                if (string.IsNullOrEmpty(newSong.Url))
                {
                    var tempsong = await netEase.GetSong(newSong.Id);

                    if (tempsong != null)
                    {
                        tempsong.Id     = newSong.Id;
                        tempsong.Title  = newSong.Title;
                        tempsong.Artist = newSong.Artist;
                        newSong         = tempsong;
                    }
                }


                var currenTrackToPlay = (await netEase.FixUrl(newSong, true)).Url;
                Dictionary <string, string> headers = new Dictionary <string, string>();
                if (currenTrackToPlay.StartsWith("http://221.228.64.228/"))
                {
                    headers.Add("Host", "m1.music.126.net");
                }
                headers.Add(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

                var trackUrl = Uri.Parse(currenTrackToPlay);
                FrameworkSampleSource sampleSource = new FrameworkSampleSource(Main, trackUrl, headers);

                TrackRenderer aRenderer = new MediaCodecAudioTrackRenderer(sampleSource, MediaCodecSelector.Default);

                if (QueueChanged != null)
                {
                    QueueChanged(this, new EventArgs());
                }

                var focusResult = this.audioManager.RequestAudioFocus(this, Stream.Music, AudioFocus.Gain);
                if (focusResult != AudioFocusRequest.Granted)
                {
                    //could not get audio focus
                    Console.WriteLine("Could not get audio focus");
                }

                Player.Prepare(aRenderer);
                Player.PlayWhenReady = true;

                this.AquireWifiLock();

                if (currentSong != null)
                {
                    if (!string.IsNullOrEmpty(currentSong.Artist) && !string.IsNullOrEmpty(currentSong.Title))
                    {
                        this.StartForeground(currentSong.Artist + " - " + currentSong.Title);
                    }
                    else if (!string.IsNullOrEmpty(currentSong.Artist))
                    {
                        this.StartForeground(currentSong.Artist);
                    }
                    else if (!string.IsNullOrEmpty(currentSong.Title))
                    {
                        this.StartForeground(currentSong.Title);
                    }
                }

                this.RegisterRemoteClient();
                this.remoteControlClient.SetPlaybackState(RemoteControlPlayState.Buffering);
                this.UpdateMetadata();
            }
            catch
            {
                // ignored
            }
        }