public void OnPrepare()
        {
            _mediaSource.Clear();
            foreach (var mediaItem in mediaManager.MediaQueue)
            {
                _mediaSource.AddMediaSource(mediaItem.ToMediaSource());
            }
            _player.Prepare(_mediaSource);

            //Only in case of Prepare set PlayWhenReady to true because we use this to load in the whole queue
            _player.PlayWhenReady = true;
        }
 /// <summary>
 /// Invoked with the results from a <see cref="IRendererBuilder"/>.
 /// </summary>
 /// <param name="renderers">Renderers indexed by <see cref="MvxVideoPlayer"/>. TYPE_* constants. An individual element may be null if there do not exist tracks of the corresponding type.</param>
 /// <param name="bandwidthMeter">Provides an estimate of the currently available bandwidth. May be null.</param>
 internal void OnRenderers(TrackRenderer[] renderers, IBandwidthMeter bandwidthMeter)
 {
     for (var i = 0; i < RendererCount; i++)
     {
         if (renderers[i] == null)
         {
             // Convert a null renderer to a dummy renderer.
             renderers[i] = new DummyTrackRenderer();
         }
     }
     // Complete preparation.
     _videoRenderer = renderers[TypeVideo];
     _codecCounters = _videoRenderer is MediaCodecTrackRenderer
         ? ((MediaCodecTrackRenderer)_videoRenderer).CodecCounters
         : renderers[TypeAudio] is MediaCodecTrackRenderer
             ? ((MediaCodecTrackRenderer)renderers[TypeAudio]).CodecCounters
             : null;
     _bandwidthMeter = bandwidthMeter;
     PushSurface(false);
     _player.Prepare(renderers);
     _rendererBuildingState = RendererBuildingStateBuilt;
 }
        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
            }
        }