示例#1
0
        // Play song from persistentSongID
        public void playSong(Song song)
        {
            currentSong = song;
            if (song.streamingURL == null)
            {
                MusicQuery  musicQuery = new MusicQuery();
                MPMediaItem mediaItem  = musicQuery.queryForSongWithId(song.songID);
                if (mediaItem != null)
                {
                    NSUrl Url = mediaItem.AssetURL;
                    item = AVPlayerItem.FromUrl(Url);
                    if (item != null)
                    {
                        this.avPlayer.ReplaceCurrentItemWithPlayerItem(item);
                    }

                    MPNowPlayingInfo np = new MPNowPlayingInfo();
                    SetNowPlayingInfo(song, np);
                    this.play();
                }
            }
            else
            {
                NSUrl nsUrl = NSUrl.FromString(song.streamingURL);
                MyMusicPlayer.myMusicPlayer?.streamingItem?.RemoveObserver(MyMusicPlayer.myMusicPlayer, "status");
                streamingItem = AVPlayerItem.FromUrl(nsUrl);
                streamingItem.AddObserver(this, new NSString("status"), NSKeyValueObservingOptions.New,
                                          avPlayer.Handle);
                avPlayer.ReplaceCurrentItemWithPlayerItem(streamingItem);
                streamingItemPaused = false;
                //NSNotificationCenter.DefaultCenter.AddObserver(this, new Selector("playerItemDidReachEnd:"), AVPlayerItem.DidPlayToEndTimeNotification, streamingItem);
            }
        }
示例#2
0
        public Dictionary <string, List <MediaItem> > GetSongsByArtist()
        {
            Dictionary <string, List <MediaItem> > artistSongs = new Dictionary <string, List <MediaItem> >();

            try
            {
                MusicQuery musicQuery   = new MusicQuery();
                var        artistSongs1 = musicQuery.queryForSongs();

                MPMediaQuery            mediaQuery    = MPMediaQuery.ArtistsQuery;
                MPMediaItemCollection[] songsByArtist = mediaQuery.Collections;
                List <MediaItem>        songs;
                foreach (MPMediaItemCollection artist in songsByArtist)
                {
                    MPMediaItem[] songItems  = artist.Items;
                    string        artistName = "";
                    songs = new List <MediaItem>();
                    foreach (MPMediaItem songItem in songItems)
                    {
                        // Create a new song type and add the info from this song to it
                        MediaItem song = new MediaItem();
                        try
                        {
                            if (artistName == "")
                            {
                                artistName = songItem.Artist;
                            }
                            SetMediaItem(ref song, songItem);
                            songs.Add(song);
                        }
                        catch (Exception ex)
                        {
                            Messages.Add(GetExceptionDetail(ex));
                        }
                    }

                    if (!artistSongs.ContainsKey(artistName))
                    {
                        artistSongs.Add(artistName, songs);
                    }
                    else
                    {
                        List <MediaItem> temp = null;
                        artistSongs.TryGetValue(artistName, out temp);
                        if (temp != null)
                        {
                            temp.AddRange(songs);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Messages.Add(GetExceptionDetail(ex));
                _ = ex;
            }

            return(artistSongs);
        }
示例#3
0
        // Get the songs from the music library
        public static void querySongs()
        {
            MusicQuery musicQuery = new MusicQuery();

            artistSongs = musicQuery.queryForSongs();
            artistCount = artistSongs.Count;

            var artists = artistSongs.Values;

            songCount = 0;
            foreach (List <Song> songs in artists)
            {
                songCount += songs.Count;
            }

            searchSongCount   = 0;
            searchArtistCount = 0;
        }