Ejemplo n.º 1
0
        public async void Init(AutocompleteSearch autocompleteSearch)
        {
            if (string.IsNullOrEmpty(autocompleteSearch.Id) || string.IsNullOrEmpty(autocompleteSearch.ResultType))
            {
                return;
            }

            this.IsBusy      = true;
            this.currentItem = new SongListInitItem()
            {
                ItemType = SongListEnum.AutoComplete, Item = autocompleteSearch
            };
            if (!string.IsNullOrEmpty(autocompleteSearch.PicUrl))
            {
                this.PicUrl = autocompleteSearch.PicUrl.Replace("Small", string.Empty);
            }
            if (autocompleteSearch.AutoCompleteType == AutoCompleteType.Xiami)
            {
                List <SongNetease> tempSong = new List <SongNetease>();
                tempSong = await this.netEase.GetXiamiSongs(autocompleteSearch);

                this.Songs = new ObservableCollection <ItemWrap>();

                if (tempSong == null)
                {
                    this.dialog.ShowAlert(Texts.ErrorGettingResults, 5000);
                }
                else
                {
                    foreach (var item in tempSong)
                    {
                        this.Songs.Add(new ItemWrap(item, this));
                    }
                }
            }
            else
            {
                //this.Songs = await this.netEase.Autocomplete(toSearch);
                List <SongNetease> tempSong = new List <SongNetease>();
                if (autocompleteSearch.ResultType == "Album")
                {
                    tempSong = await this.netEase.GetSongsForAlbum(autocompleteSearch.Id, autocompleteSearch.ArtistName);
                }
                else if (autocompleteSearch.ResultType == "Song")
                {
                    List <SongNetease> songs = new List <SongNetease>();
                    SongNetease        song  = await this.netEase.GetSong(autocompleteSearch.Id);

                    if (song != null)
                    {
                        tempSong.Add(song);
                    }
                    else
                    {
                        tempSong = null;
                    }
                }
                else if (autocompleteSearch.ResultType == "Artist")
                {
                    this.IsArtist = true;
                    tempSong      = await this.netEase.SearchArtist(autocompleteSearch.Id, autocompleteSearch.Name);

                    this.Albums = await this.netEase.SearchAlbums(autocompleteSearch.Id, autocompleteSearch.Name);

                    this.SimilarArtists = await this.netEase.GetSimilar(autocompleteSearch.Name);

                    if ((this.Albums == null) || (this.SimilarArtists == null))
                    {
                        this.dialog.ShowAlert(Texts.ErrorGettingResults, 5000);
                    }
                }
                this.Songs = new ObservableCollection <ItemWrap>();

                if (tempSong == null)
                {
                    this.dialog.ShowAlert(Texts.ErrorGettingResults, 5000);
                }
                else
                {
                    foreach (var item in tempSong)
                    {
                        this.Songs.Add(new ItemWrap(item, this));
                    }
                }
            }
            this.RaisePropertyChanged(() => this.Songs);
            this.IsBusy = false;
            this.RaisePropertyChanged(() => this.TitleView);
        }
Ejemplo n.º 2
0
 private void SelectItemHandler(SongNetease item)
 {
     //ShowViewModel<ListSongViewModel>(new { plid = item.Id, plname = item.Name });
     this.player.PlayWithoutClear(item);
 }
Ejemplo n.º 3
0
        private void GetValue(JToken songs, JToken data, List <SongNetease> hotSongs)
        {
            foreach (var songJson in songs)
            {
                SongNetease song = new SongNetease();
                if (songJson["h"].HasValues && (songJson["h"]["fid"].ToString() != "0"))
                {
                    song.Title  = songJson["name"].ToString();
                    song.Id     = songJson["id"].ToString();
                    song.Artist = songJson["ar"][0]["name"].ToString();
                    song.Url    = "http://p3.music.126.net/" + this.Decrypt(songJson["h"]["fid"].ToString()) + "/" +
                                  songJson["h"]["fid"].ToString() + ".mp3";
                    if (data != null)
                    {
                        song.Artist = data["artist"]["name"].ToString();
                    }
                }
                else if (songJson["m"].HasValues && (songJson["m"]["fid"].ToString() != "0"))
                {
                    song.Title  = songJson["name"].ToString();
                    song.Id     = songJson["id"].ToString();
                    song.Artist = songJson["ar"][0]["name"].ToString();
                    song.Url    = "http://p3.music.126.net/" + this.Decrypt(songJson["m"]["fid"].ToString()) + "/" +
                                  songJson["m"]["fid"].ToString() + ".mp3";
                    if (data != null)
                    {
                        song.Artist = data["artist"]["name"].ToString();
                    }
                }
                else if (songJson["l"].HasValues && (songJson["l"]["fid"].ToString() != "0"))
                {
                    song.Title  = songJson["name"].ToString();
                    song.Id     = songJson["id"].ToString();
                    song.Artist = songJson["ar"][0]["name"].ToString();
                    song.Url    = "http://p3.music.126.net/" + this.Decrypt(songJson["l"]["fid"].ToString()) + "/" +
                                  songJson["l"]["fid"].ToString() + ".mp3";
                    if (data != null)
                    {
                        song.Artist = data["artist"]["name"].ToString();
                    }
                }
                else
                {
                    song.Title  = songJson["name"].ToString();
                    song.Id     = songJson["id"].ToString();
                    song.Artist = songJson["ar"][0]["name"].ToString();
                    song.Url    = string.Empty;
                    if (data != null)
                    {
                        song.Artist = data["artist"]["name"].ToString();
                    }
                }

                /*var dur = songJson["duration"].ToString();
                 * int duration;
                 * if (int.TryParse(dur, out duration))
                 * {
                 *  song.Duration = duration;
                 * }
                 * else
                 * {
                 *  song.Duration = 0;
                 * }*/

                hotSongs.Add(song);
            }
        }
Ejemplo n.º 4
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
            }
        }
Ejemplo n.º 5
0
        public async Task <SongNetease> GetSong(string songId)
        {
            try
            {
                string url = Domain + "/api/song/detail?ids=[" + songId + "]";

                SongNetease song = new SongNetease();
                using (HttpClient client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Referrer = new Uri("http://music.163.com");
                    client.DefaultRequestHeaders.Host     = "music.163.com";

                    string content = await client.GetStringAsync(url);

                    var data      = JToken.Parse(content);
                    var songsJson = data["songs"];
                    var asdfasdf  = JsonConvert.DeserializeObject <IEnumerable <SongDto> >(songsJson.ToString()).ToList();
                    var artist    = data;

                    string extension = ".mp3";

                    foreach (var songJson in songsJson)
                    {
                        var artistJson = songJson["artists"].FirstOrDefault();

                        // TODO Improve this f*****g shit.
                        try
                        {
                            song.Title = songJson["hMusic"]["name"].ToString();
                            song.Id    = songJson["hMusic"]["dfsId"].ToString();
                        }
                        catch (Exception)
                        {
                            try
                            {
                                song.Title = songJson["mMusic"]["name"].ToString();
                                song.Id    = songJson["mMusic"]["dfsId"].ToString();
                            }
                            catch (Exception)
                            {
                                try
                                {
                                    song.Title = songJson["lMusic"]["name"].ToString();
                                    song.Id    = songJson["lMusic"]["dfsId"].ToString();
                                }
                                catch (Exception)
                                {
                                    try
                                    {
                                        song.Title = songJson["bMusic"]["name"].ToString();
                                        song.Id    = songJson["bMusic"]["dfsId"].ToString();
                                    }
                                    catch (Exception)
                                    {
                                        try
                                        {
                                            song.Title = songJson["audition"]["name"].ToString();
                                            song.Id    = songJson["audition"]["dfsId"].ToString();
                                            extension  = ".m4a";
                                        }
                                        catch (Exception)
                                        {
                                        }
                                    }
                                }
                            }
                        }

                        song.Artist = artistJson["name"].ToString();
                        var dur = songJson["duration"].ToString();
                        int duration;
                        if (int.TryParse(dur, out duration))
                        {
                            song.Duration = duration;
                        }
                        else
                        {
                            song.Duration = 0;
                        }

                        song.Url = "http://p3.music.126.net/" + this.Decrypt(song.Id) + "/" +
                                   song.Id + extension;
                        break;
                    }
                }

                return(song);
            }
            catch (Exception)
            {
                return(null);
            }
        }