public async Task <IList <Song> > GetPlaylistSongs(ulong playlistId) { return(await Task.Run <IList <Song> >(() => { IList <Song> songs = new ObservableCollection <Song>(); MPMediaQuery mq = MPMediaQuery.SongsQuery; var value = NSNumber.FromUInt64(playlistId); MPMediaPropertyPredicate predicate = MPMediaPropertyPredicate.PredicateWithValue(value, MPMediaPlaylistProperty.PersistentID); mq.AddFilterPredicate(predicate); var items = mq.Items; foreach (var item in items) { if (item != null && item.AssetURL != null) { songs.Add(new Song { Id = item.PersistentID, Title = item.Title, Artist = item.Artist, Album = item.AlbumTitle, Genre = item.Genre, Artwork = item.Artwork, Duration = (ulong)item.PlaybackDuration, Uri = item.AssetURL.AbsoluteString }); } } return songs; })); }
public string GetMusicDirectory() { var query = new MPMediaQuery(); var result = query.Items; return(""); }
public async Task <IList <Song> > GetAllSongs() { return(await Task.Run <IList <Song> >(() => { IList <Song> songs = new ObservableCollection <Song>(); MPMediaQuery mq = new MPMediaQuery(); mq.GroupingType = MPMediaGrouping.Title; var value = NSNumber.FromInt32((int)MPMediaType.Music); var predicate = MPMediaPropertyPredicate.PredicateWithValue(value, MPMediaItem.MediaTypeProperty); mq.AddFilterPredicate(predicate); var items = mq.Items; foreach (var item in items) { if (item != null && !item.IsCloudItem && item.AssetURL != null) { songs.Add(new Song { Id = item.PersistentID, Title = item.Title, Artist = item.Artist, Album = item.AlbumTitle, Genre = item.Genre, Artwork = item.Artwork, Duration = (ulong)item.PlaybackDuration, Uri = item.AssetURL.AbsoluteString }); } } return songs; })); }
public async Task AddToPlaylist(Playlist playlist, Song song) { await Task.Run(() => { MPMediaQuery mq = MPMediaQuery.PlaylistsQuery; MPMediaItemCollection[] playlistArray = mq.Collections; foreach (MPMediaPlaylist pl in playlistArray) { if (pl.PersistentID == playlist.Id) { MPMediaQuery m = MPMediaQuery.SongsQuery; var p = MPMediaPropertyPredicate.PredicateWithValue(NSNumber.FromUInt64(song.Id), MPMediaItem.PersistentIDProperty); m.AddFilterPredicate(p); if (m.Items.Length > 0) { pl.AddMediaItems(m.Items, (err) => { if (err != null) { err.ToString(); } }); } } } }); }
public void queryiTunesLibraryForMediaItems(List <LibraryItem> libraryItems) { #if ENABLE_ITUNES_ITEMS try { var mq = new MPMediaQuery(); var value = NSNumber.FromInt32((int)MPMediaType.TypeAnyVideo); var predicate = MPMediaPropertyPredicate.PredicateWithValue(value, MPMediaItem.MediaTypeProperty); mq.AddFilterPredicate(predicate); List <MPMediaItem> mediaItems = new List <MPMediaItem>(mq.Items); foreach (MPMediaItem mediaItem in mediaItems) { AVAsset asset = AVAsset.FromUrl(mediaItem.AssetURL); if ((asset != null) && isRecording(asset)) { LibraryItem libraryItem = new LibraryItem(); libraryItem.Storage = LibraryItemStorage.iTunes; libraryItem.ID = mediaItem.PersistentID.ToString(); libraryItem.LocalFilePath = mediaItem.AssetURL.ToString(); fetchMetadata(asset, ref libraryItem); libraryItems.Add(libraryItem); } } } catch (Exception ex) { Logger.Log("ERROR: LocalLibrary.queryiTunesLibraryForMediaItems: " + ex); } #endif }
public void DefaultValues() { if (Runtime.Arch != Arch.DEVICE) { Assert.Inconclusive("This test only works on device (the simulator does not have an iPod Music library)."); } using (var q = new MPMediaQuery()) { var items = q.Items; if (items.Length == 0) { Assert.Inconclusive("This test needs music in the music library on the device."); } var six_dot_oh = TestRuntime.CheckSystemAndSDKVersion(6, 0); foreach (var i in items) { object dummy; Assert.DoesNotThrow(() => dummy = i.AlbumArtist, "AlbumArtist"); Assert.DoesNotThrow(() => dummy = i.AlbumArtistPersistentID, "AlbumArtistPersistentID"); Assert.DoesNotThrow(() => dummy = i.AlbumPersistentID, "AlbumPersistentID"); Assert.DoesNotThrow(() => dummy = i.AlbumTitle, "AlbumTitle"); Assert.DoesNotThrow(() => dummy = i.AlbumTrackCount, "AlbumTrackCount"); Assert.DoesNotThrow(() => dummy = i.AlbumTrackNumber, "AlbumTrackNumber"); Assert.DoesNotThrow(() => dummy = i.Artist, "Artist"); Assert.DoesNotThrow(() => dummy = i.ArtistPersistentID, "ArtistPersistentID"); Assert.DoesNotThrow(() => dummy = i.Artwork, "Artwork"); Assert.DoesNotThrow(() => dummy = i.AssetURL, "AssetURL"); Assert.DoesNotThrow(() => dummy = i.BeatsPerMinute, "BeatsPerMinute"); Assert.DoesNotThrow(() => dummy = i.BookmarkTime, "BookmarkTime"); Assert.DoesNotThrow(() => dummy = i.Comments, "Comments"); Assert.DoesNotThrow(() => dummy = i.Composer, "Composer"); Assert.DoesNotThrow(() => dummy = i.ComposerPersistentID, "ComposerPersistentID"); Assert.DoesNotThrow(() => dummy = i.DiscCount, "DiscCount"); Assert.DoesNotThrow(() => dummy = i.DiscNumber, "DiscNumber"); Assert.DoesNotThrow(() => dummy = i.Genre, "Genre"); Assert.DoesNotThrow(() => dummy = i.GenrePersistentID, "GenrePersistentID"); if (six_dot_oh) { Assert.DoesNotThrow(() => dummy = i.IsCloudItem, "IsCloudItem"); } Assert.DoesNotThrow(() => dummy = i.IsCompilation, "IsCompilation"); Assert.DoesNotThrow(() => dummy = i.LastPlayedDate, "LastPlayedDate"); Assert.DoesNotThrow(() => dummy = i.Lyrics, "Lyrics"); Assert.DoesNotThrow(() => dummy = i.MediaType, "MediaType"); Assert.DoesNotThrow(() => dummy = i.PersistentID, "PersistentID"); Assert.DoesNotThrow(() => dummy = i.PlaybackDuration, "PlaybackDuration"); Assert.DoesNotThrow(() => dummy = i.PlayCount, "PlayCount"); Assert.DoesNotThrow(() => dummy = i.PodcastPersistentID, "PodcastPersistentID"); Assert.DoesNotThrow(() => dummy = i.PodcastTitle, "PodcastTitle"); Assert.DoesNotThrow(() => dummy = i.Rating, "Rating"); Assert.DoesNotThrow(() => dummy = i.ReleaseDate, "ReleaseDate"); Assert.DoesNotThrow(() => dummy = i.SkipCount, "SkipCount"); Assert.DoesNotThrow(() => dummy = i.Title, "Title"); Assert.DoesNotThrow(() => dummy = i.UserGrouping, "UserGrouping"); } } }
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); }
public Dictionary <string, List <MediaItem> > GetSongsByAlbum() { Dictionary <string, List <MediaItem> > albumSongs = new Dictionary <string, List <MediaItem> >(); try { MPMediaQuery mediaQuery = MPMediaQuery.AlbumsQuery; MPMediaItemCollection[] songsByAlbum = mediaQuery.Collections; List <MediaItem> songs; foreach (MPMediaItemCollection album in songsByAlbum) { MPMediaItem[] songItems = album.Items; string albumName = ""; 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 (albumName == "") { albumName = songItem.AlbumTitle; } SetMediaItem(ref song, songItem); songs.Add(song); } catch (Exception ex) { Messages.Add(GetExceptionDetail(ex)); } } if (!albumSongs.ContainsKey(albumName)) { albumSongs.Add(albumName, songs); } else { List <MediaItem> temp = null; albumSongs.TryGetValue(albumName, out temp); if (temp != null) { temp.AddRange(songs); } } } } catch (Exception ex) { Messages.Add(GetExceptionDetail(ex)); _ = ex; } return(albumSongs); }
internal static IEnumerable <Genre> ToGenres(this MPMediaQuery mediaQuery, GenreSortOrder sortOrder) { foreach (var mediaItem in mediaQuery.Items) { if (mediaItem != null) { yield return(mediaItem.ToGenre()); } } }
internal static IEnumerable <Artist> ToArtists(this MPMediaQuery mediaQuery, ArtistSortOrder sortOrder) { foreach (var mediaItem in mediaQuery.Items) { if (mediaItem != null) { yield return(mediaItem.ToArtist()); } } }
internal static IEnumerable <Song> ToSongs(this MPMediaQuery mediaQuery, SongSortOrder sortOrder) { foreach (var mediaItem in mediaQuery.Items) { if (mediaItem != null && mediaItem.AssetURL != null) { yield return(mediaItem.ToSong()); } } }
internal static IEnumerable <Playlist> ToPlaylists(this MPMediaQuery mediaQuery, PlaylistSortOrder sortOrder) { foreach (var mediaItem in mediaQuery.Items) { if (mediaItem != null && mediaItem.AssetURL != null) { yield return(mediaItem.ToPlaylist()); } } }
// Get a song with a particular id public MPMediaItem queryForSongWithId(ulong songPersistenceId) { MPMediaPropertyPredicate mediaItemPersistenceIdPredicate = MPMediaPropertyPredicate.PredicateWithValue(new NSNumber(songPersistenceId), MPMediaItem.PersistentIDProperty); MPMediaQuery songQuery = new MPMediaQuery(); songQuery.AddFilterPredicate(mediaItemPersistenceIdPredicate); var items = songQuery.Items; return(items[items.Length - 1]); }
private async void LoadMediaItemsForMediaTypeAsync(MPMediaType mediaType) { await Task.Run(() => { var query = new MPMediaQuery(); var mediaTypeNumber = NSNumber.FromInt32((int)mediaType); var predicate = MPMediaPropertyPredicate.PredicateWithValue(mediaTypeNumber, MPMediaItem.MediaTypeProperty); query.AddFilterPredicate(predicate); allMediaItems = query.Items.ToList(); }); TableView.ReloadData(); }
public async Task <List <MediaItem> > GetItemsAsync() { return(await Task.Run(() => { var query = new MPMediaQuery(); var mediaTypeNumber = NSNumber.FromInt32((int)MPMediaType.Music); var predicate = MPMediaPropertyPredicate.PredicateWithValue(mediaTypeNumber, MPMediaItem.MediaTypeProperty); query.AddFilterPredicate(predicate); var unknownArtist = NSBundle.MainBundle.LocalizedString("unknownArtist", "Unknown Artist"); return query.Items.Select(item => new MediaItem(item.Title, (item.Artist != null) ? item.Artist : unknownArtist, GetDisplayTime((int)item.PlaybackDuration))).ToList(); })); }
public IEnumerable <ITrack> GetTracks() { MPMediaQuery mq = new MPMediaQuery(); var value = NSObject.FromObject(PersistentID); var type = MPMediaItem.AlbumPersistentIDProperty; var predicate = MPMediaPropertyPredicate.PredicateWithValue(value, type); mq.AddFilterPredicate(predicate); return(mq.Items.Select(source => new Track() { Title = source.Title, AlbumTrackNumber = source.AlbumTrackNumber, ContentUrl = source.AssetURL.AbsoluteString, Duration = (int)source.PlaybackDuration })); }
public IEnumerable <IAlbum> GetAllAlbums() { if (PermissionGranted) { MPMediaQuery mq = MPMediaQuery.AlbumsQuery; foreach (var mediaItem in mq.Items) { if (mediaItem.AlbumTitle != null) { Debug.WriteLine(mediaItem.AlbumTitle); } if (mediaItem.PersistentID != null) { Debug.WriteLine(mediaItem.PersistentID); } if (mediaItem.AlbumPersistentID != null) { Debug.WriteLine(mediaItem.AlbumPersistentID); } if (mediaItem.Title != null) { Debug.WriteLine(mediaItem.Title); } Debug.WriteLine(mediaItem.AssetURL); Debug.WriteLine(mediaItem.MediaType.ToString()); } return (mq.Items.Select( mediaItem => new Album(mediaItem.AlbumPersistentID, mediaItem.Artwork) { Title = mediaItem.AlbumTitle, Artist = mediaItem.Artist, TrackCount = mediaItem.AlbumTrackCount, })); } return(null); }
public IList <Playlist> GetPlaylists() { IList <Playlist> playlists = new ObservableCollection <Playlist>(); MPMediaQuery mq = MPMediaQuery.PlaylistsQuery; MPMediaItemCollection[] playlistArray = mq.Collections; foreach (MPMediaPlaylist playlist in playlistArray) { playlists.Add(new Playlist { Id = ulong.Parse(playlist.ValueForProperty(MPMediaPlaylistProperty.PersistentID).ToString()), Title = playlist.ValueForProperty(MPMediaPlaylistProperty.Name).ToString(), IsDynamic = playlist.PlaylistAttributes == MPMediaPlaylistAttribute.None ? true : false, DateModified = DateTime.Now }); } return(playlists); }
void ExportFirstSong() { MPMediaQuery query = MPMediaQuery.SongsQuery(); if (query.items.Length == 0) { Log("No songs in Music Library."); return; } // get first song MPMediaItem mediaItem = query.items[0] as MPMediaItem; Log("Exporting song: " + mediaItem.Value(MPMediaItem.PropertyTitle)); // use null for outputFolder and outputFile to name it [artist] - [title] if (!MediaExporter.ExportAudio(mediaItem, null, null, true)) { Log("Export error or song has DRM."); } }
public void DefaultValues() { if (Runtime.Arch != Arch.DEVICE) { Assert.Inconclusive("This test only works on device (the simulator does not have an iPod Music library)."); } TestRuntime.RequestMediaLibraryPermission(true); using (var q = new MPMediaQuery()) { var items = q.Items; if (items == null) { Assert.Inconclusive("This test needs media library privacy permission to be executed."); } if (items.Length == 0) { Assert.Inconclusive("This test needs music in the music library on the device."); } var six_dot_oh = true; var nine_dot_two = TestRuntime.CheckSystemVersion(PlatformName.iOS, 9, 2); var ten_dot_oh = TestRuntime.CheckSystemVersion(PlatformName.iOS, 10, 0); var ten_dot_three = TestRuntime.CheckSystemVersion(PlatformName.iOS, 10, 3); foreach (var i in items) { object dummy; Assert.DoesNotThrow(() => dummy = i.AlbumArtist, "AlbumArtist"); Assert.DoesNotThrow(() => dummy = i.AlbumArtistPersistentID, "AlbumArtistPersistentID"); Assert.DoesNotThrow(() => dummy = i.AlbumPersistentID, "AlbumPersistentID"); Assert.DoesNotThrow(() => dummy = i.AlbumTitle, "AlbumTitle"); Assert.DoesNotThrow(() => dummy = i.AlbumTrackCount, "AlbumTrackCount"); Assert.DoesNotThrow(() => dummy = i.AlbumTrackNumber, "AlbumTrackNumber"); Assert.DoesNotThrow(() => dummy = i.Artist, "Artist"); Assert.DoesNotThrow(() => dummy = i.ArtistPersistentID, "ArtistPersistentID"); Assert.DoesNotThrow(() => dummy = i.Artwork, "Artwork"); Assert.DoesNotThrow(() => dummy = i.AssetURL, "AssetURL"); Assert.DoesNotThrow(() => dummy = i.BeatsPerMinute, "BeatsPerMinute"); Assert.DoesNotThrow(() => dummy = i.BookmarkTime, "BookmarkTime"); Assert.DoesNotThrow(() => dummy = i.Comments, "Comments"); Assert.DoesNotThrow(() => dummy = i.Composer, "Composer"); Assert.DoesNotThrow(() => dummy = i.ComposerPersistentID, "ComposerPersistentID"); Assert.DoesNotThrow(() => dummy = i.DiscCount, "DiscCount"); Assert.DoesNotThrow(() => dummy = i.DiscNumber, "DiscNumber"); Assert.DoesNotThrow(() => dummy = i.Genre, "Genre"); Assert.DoesNotThrow(() => dummy = i.GenrePersistentID, "GenrePersistentID"); if (six_dot_oh) { Assert.DoesNotThrow(() => dummy = i.IsCloudItem, "IsCloudItem"); } Assert.DoesNotThrow(() => dummy = i.IsCompilation, "IsCompilation"); Assert.DoesNotThrow(() => dummy = i.LastPlayedDate, "LastPlayedDate"); Assert.DoesNotThrow(() => dummy = i.Lyrics, "Lyrics"); Assert.DoesNotThrow(() => dummy = i.MediaType, "MediaType"); Assert.DoesNotThrow(() => dummy = i.PersistentID, "PersistentID"); Assert.DoesNotThrow(() => dummy = i.PlaybackDuration, "PlaybackDuration"); Assert.DoesNotThrow(() => dummy = i.PlayCount, "PlayCount"); Assert.DoesNotThrow(() => dummy = i.PodcastPersistentID, "PodcastPersistentID"); Assert.DoesNotThrow(() => dummy = i.PodcastTitle, "PodcastTitle"); Assert.DoesNotThrow(() => dummy = i.Rating, "Rating"); Assert.DoesNotThrow(() => dummy = i.ReleaseDate, "ReleaseDate"); Assert.DoesNotThrow(() => dummy = i.SkipCount, "SkipCount"); Assert.DoesNotThrow(() => dummy = i.Title, "Title"); Assert.DoesNotThrow(() => dummy = i.UserGrouping, "UserGrouping"); if (nine_dot_two) { Assert.DoesNotThrow(() => dummy = i.HasProtectedAsset, "HasProtectedAsset"); } if (ten_dot_oh) { Assert.DoesNotThrow(() => dummy = i.IsExplicitItem, "IsExplicitItem"); Assert.DoesNotThrow(() => dummy = i.DateAdded, "DateAdded"); } if (ten_dot_three) { Assert.DoesNotThrow(() => dummy = i.PlaybackStoreID, "PlaybackStoreID"); } } } }
private void PlatformLoad(Action<int> progressCallback) { MPMediaQuery mediaQuery = new MPMediaQuery(); var value = NSObject.FromObject(MPMediaType.Music); var type = MPMediaItem.MediaTypeProperty; var predicate = MPMediaPropertyPredicate.PredicateWithValue(value, type); mediaQuery.AddFilterPredicate(predicate); mediaQuery.GroupingType = MPMediaGrouping.Album; List<Song> songList = new List<Song>(); List<Album> albumList = new List<Album>(); for (int i = 0; i < mediaQuery.Collections.Length; i++) { MPMediaItemCollection itemCollection = mediaQuery.Collections[i]; List<Song> albumSongs = new List<Song>((int)itemCollection.Count); var nsAlbumArtist = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.AlbumArtistProperty); var nsAlbumName = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.AlbumTitleProperty); var nsAlbumGenre = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.GenreProperty); string albumArtist = nsAlbumArtist == null ? "Unknown Artist" : nsAlbumArtist.ToString(); string albumName = nsAlbumName == null ? "Unknown Album" : nsAlbumName.ToString(); string albumGenre = nsAlbumGenre == null ? "Unknown Genre" : nsAlbumGenre.ToString(); MPMediaItemArtwork thumbnail = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.ArtworkProperty) as MPMediaItemArtwork; var album = new Album(new SongCollection(albumSongs), albumName, new Artist(albumArtist), new Genre(albumGenre), thumbnail); albumList.Add(album); for (int j = 0; j < itemCollection.Count; j++) { var nsArtist = itemCollection.Items[j].ValueForProperty(MPMediaItem.ArtistProperty); var nsTitle = itemCollection.Items[j].ValueForProperty(MPMediaItem.TitleProperty); var nsGenre = itemCollection.Items[j].ValueForProperty(MPMediaItem.GenreProperty); var assetUrl = itemCollection.Items[j].ValueForProperty(MPMediaItem.AssetURLProperty) as NSUrl; if (nsTitle == null || assetUrl == null) // The Asset URL check will exclude iTunes match items from the Media Library that are not downloaded, but show up in the music app continue; string artist = nsArtist == null ? "Unknown Artist" : nsArtist.ToString(); string title = nsTitle.ToString(); string genre = nsGenre == null ? "Unknown Genre" : nsGenre.ToString(); TimeSpan duration = TimeSpan.FromSeconds(((NSNumber)itemCollection.Items[j].ValueForProperty(MPMediaItem.PlaybackDurationProperty)).FloatValue); var song = new Song(album, new Artist(artist), new Genre(genre), title, duration, itemCollection.Items[j], assetUrl); albumSongs.Add(song); songList.Add(song); } } albumCollection = new AlbumCollection(albumList); songCollection = new SongCollection(songList); /*_playLists = new PlaylistCollection(); MPMediaQuery playlists = new MPMediaQuery(); playlists.GroupingType = MPMediaGrouping.Playlist; for (int i = 0; i < playlists.Collections.Length; i++) { MPMediaItemCollection item = playlists.Collections[i]; Playlist list = new Playlist(); list.Name = playlists.Items[i].ValueForProperty(MPMediaPlaylistPropertyName).ToString(); for (int k = 0; k < item.Items.Length; k++) { TimeSpan time = TimeSpan.Parse(item.Items[k].ValueForProperty(MPMediaItem.PlaybackDurationProperty).ToString()); list.Duration += time; } _playLists.Add(list); }*/ }
private void PlatformLoad(Action <int> progressCallback) { MPMediaQuery mediaQuery = new MPMediaQuery(); var value = NSObject.FromObject(MPMediaType.Music); var type = MPMediaItem.MediaTypeProperty; var predicate = MPMediaPropertyPredicate.PredicateWithValue(value, type); mediaQuery.AddFilterPredicate(predicate); mediaQuery.GroupingType = MPMediaGrouping.Album; List <Song> songList = new List <Song>(); List <Album> albumList = new List <Album>(); for (int i = 0; i < mediaQuery.Collections.Length; i++) { MPMediaItemCollection itemCollection = mediaQuery.Collections[i]; List <Song> albumSongs = new List <Song>(itemCollection.Count); var nsAlbumArtist = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.AlbumArtistProperty); var nsAlbumName = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.AlbumTitleProperty); var nsAlbumGenre = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.GenreProperty); string albumArtist = nsAlbumArtist == null ? "Unknown Artist" : nsAlbumArtist.ToString(); string albumName = nsAlbumName == null ? "Unknown Album" : nsAlbumName.ToString(); string albumGenre = nsAlbumGenre == null ? "Unknown Genre" : nsAlbumGenre.ToString(); MPMediaItemArtwork thumbnail = itemCollection.RepresentativeItem.ValueForProperty(MPMediaItem.ArtworkProperty) as MPMediaItemArtwork; var album = new Album(new SongCollection(albumSongs), albumName, new Artist(albumArtist), new Genre(albumGenre), thumbnail); albumList.Add(album); for (int j = 0; j < itemCollection.Count; j++) { var nsArtist = itemCollection.Items[j].ValueForProperty(MPMediaItem.ArtistProperty); var nsTitle = itemCollection.Items[j].ValueForProperty(MPMediaItem.TitleProperty); var nsGenre = itemCollection.Items[j].ValueForProperty(MPMediaItem.GenreProperty); var assetUrl = itemCollection.Items[j].ValueForProperty(MPMediaItem.AssetURLProperty) as NSUrl; if (nsTitle == null || assetUrl == null) // The Asset URL check will exclude iTunes match items from the Media Library that are not downloaded, but show up in the music app { continue; } string artist = nsArtist == null ? "Unknown Artist" : nsArtist.ToString(); string title = nsTitle.ToString(); string genre = nsGenre == null ? "Unknown Genre" : nsGenre.ToString(); TimeSpan duration = TimeSpan.FromSeconds(((NSNumber)itemCollection.Items[j].ValueForProperty(MPMediaItem.PlaybackDurationProperty)).FloatValue); var song = new Song(album, new Artist(artist), new Genre(genre), title, duration, itemCollection.Items[j], assetUrl); albumSongs.Add(song); songList.Add(song); } } albumCollection = new AlbumCollection(albumList); songCollection = new SongCollection(songList); /*_playLists = new PlaylistCollection(); * * MPMediaQuery playlists = new MPMediaQuery(); * playlists.GroupingType = MPMediaGrouping.Playlist; * for (int i = 0; i < playlists.Collections.Length; i++) * { * MPMediaItemCollection item = playlists.Collections[i]; * Playlist list = new Playlist(); * list.Name = playlists.Items[i].ValueForProperty(MPMediaPlaylistPropertyName).ToString(); * for (int k = 0; k < item.Items.Length; k++) * { * TimeSpan time = TimeSpan.Parse(item.Items[k].ValueForProperty(MPMediaItem.PlaybackDurationProperty).ToString()); * list.Duration += time; * } * _playLists.Add(list); * }*/ }
// Get a song with a particular id public MPMediaItem queryForSongWithId(ulong songPersistenceId) { MPMediaPropertyPredicate mediaItemPersistenceIdPredicate = MPMediaPropertyPredicate.PredicateWithValue(new NSNumber(songPersistenceId), MPMediaItem.PersistentIDProperty); MPMediaQuery songQuery = new MPMediaQuery(); songQuery.AddFilterPredicate(mediaItemPersistenceIdPredicate); var items = songQuery.Items; return items[items.Length - 1]; }
// Get the songs on the device public Dictionary <string, List <Song> > queryForSongs() { MPMediaQuery query = MPMediaQuery.AlbumsQuery; /* * TigerMending album (12 missing on 5s) Picked up in app on 4 (and iPad Air 2!!) but not on 5s… not filtered out, just not picked up by app???? * Casey James (“Let’s do…"Missing on 4) <<<<<<<<<<<< filtered out as they should be as they ARE icloud items (not on computer or device) * Israel K (2 extra versions on 5s) <<<<<<<<<<<<<<<<< * Muse (2 extra “Hysteria” and “Time is running out” on 5s) <<<<<<<<<<<< * Owsley (“Undone" missing on 4) <<<<<<<<<<<<<<<<<<< * Radiohead (6 “Nude” single and stems missing on 4) <<<<<<<<<<<<<<< * U2 (1 “Vertigo” extra on 5s) <<<<<<<<<<<<<<<<<<< */ //MPMediaPropertyPredicate filter = MPMediaPropertyPredicate.PredicateWithValue(NSNumber.FromBoolean(false), MPMediaItem.IsCompilationProperty); //query.AddFilterPredicate(filter); MPMediaItemCollection[] songsByArtist = query.Collections; Dictionary <string, List <Song> > artistSongs = new Dictionary <string, List <Song> >(); List <Song> songs; foreach (MPMediaItemCollection album in songsByArtist) { MPMediaItem[] albumSongs = album.Items; string artistName = ""; songs = new List <Song>(); foreach (MPMediaItem songMediumItem in albumSongs) { // Create a new song type and add the info from this song to it Song song = new Song(); song.album = songMediumItem.AlbumTitle.ToString(); song.artist = songMediumItem.Artist.ToString(); if (artistName == "") { artistName = song.artist; } song.song = songMediumItem.Title.ToString(); song.songID = songMediumItem.PersistentID; song.artwork = songMediumItem.Artwork; song.duration = songMediumItem.PlaybackDuration; // Add the song to the list songs.Add(song); } /* The reason Tigermending was not getting picked up is that it was deleivered * by the iPhone 5s MediaQuery separately from the rest of the Carina ROund albums * So without the below else clause, It was not added to the existing Carina Round song list * This is good to do anyway, so be it. */ if (!artistSongs.ContainsKey(artistName)) { artistSongs.Add(artistName, songs); } else { List <Song> temp = null; artistSongs.TryGetValue(artistName, out temp); if (temp != null) { temp.AddRange(songs); } } } return(artistSongs); }
public List <MediaItem> GetSongs() { List <MediaItem> songs = new List <MediaItem>(); try { MPMediaQuery mediaQuery = new MPMediaQuery(); MPMediaItem[] songItems = mediaQuery.Items; foreach (var songItem in songItems) { // Create a new song type and add the info from this song to it MediaItem song = new MediaItem(); try { SetMediaItem(ref song, songItem); songs.Add(song); } catch (Exception ex) { Messages.Add(GetExceptionDetail(ex)); } } #region //return SetMediaItem(mediaQuery.Items); //List<MediaItem> SetMediaItem(MPMediaItem[] songItems) //{ // return (from songItem in songItems // select new MediaItem() // { // Title = songItem.Title, // Album = songItem.AlbumTitle, // Artist = songItem.Artist, // SongID = songItem.PersistentID, // Artwork = songItem.Artwork != null // ? ImageSource.FromStream(() => // songItem.Artwork.ImageWithSize(new CGSize(115.0f, 115.0f)).AsPNG().AsStream()) // : null, // Duration = songItem.PlaybackDuration, // SongURL = songItem.AssetURL, // AlbumArtist = songItem.AlbumArtist, // AlbumArtistPersistentID = songItem.AlbumArtistPersistentID, // AlbumPersistentID = songItem.AlbumPersistentID, // AlbumTrackCount = songItem.AlbumTrackCount, // AlbumTrackNumber = songItem.AlbumTrackNumber, // ArtistPersistentID = songItem.ArtistPersistentID, // AssetURL = songItem.AssetURL, // BeatsPerMinute = songItem.BeatsPerMinute, // BookmarkTime = songItem.BookmarkTime, // Comments = songItem.Comments, // Composer = songItem.Composer, // ComposerPersistentID = songItem.ComposerPersistentID, // DateAdded = songItem.DateAdded != null // ? songItem.DateAdded.ToDateTime() // : new DateTime(1900, 1, 1), // DiscCount = songItem.DiscCount, // DiscNumber = songItem.DiscNumber, // Genre = songItem.Genre, // GenrePersistentID = songItem.GenrePersistentID, // HasProtectedAsset = songItem.HasProtectedAsset, // IsCloudItem = songItem.IsCloudItem, // IsCompilation = songItem.IsCompilation, // IsExplicitItem = songItem.IsExplicitItem, // LastPlayedDate = songItem.LastPlayedDate != null // ? songItem.LastPlayedDate.ToDateTime() // : new DateTime(1900, 1, 1), // Lyrics = songItem.Lyrics, // MediaType = songItem.MediaType.ToString(), // PlayCount = songItem.PlayCount, // PlaybackStoreID = songItem.PlaybackStoreID, // PodcastPersistentID = songItem.PodcastPersistentID, // PodcastTitle = songItem.PodcastTitle, // Rating = songItem.Rating, // ReleaseDate = songItem.ReleaseDate != null // ? songItem.ReleaseDate.ToDateTime() // : new DateTime(1900, 1, 1), // SkipCount = songItem.SkipCount, // UserGrouping = songItem.UserGrouping, // }).ToList(); //} #endregion } catch (Exception ex) { Messages.Add(GetExceptionDetail(ex)); _ = ex; } return(songs); }
public void ResumeBook(string titleToResume, string resumingAuthor) { string chosenTitle = titleToResume; double aVeryGoodPlaceToStart = 0; dbWorker = new DBWorker (); dbWorker.StartDBWorker (); dbPath = dbWorker.GetPathToDb (); var conn = new SQLiteConnection (dbPath, false); var resumeQuery = conn.Table<SongToSave> ().Where (q => q.BookTitle == chosenTitle); foreach (var result in resumeQuery) { aVeryGoodPlaceToStart = result.PlayPosition - 30; } ResumePointVault = aVeryGoodPlaceToStart; _mediaQuery = new MPMediaQuery (); var value = NSNumber.FromInt32 ((int)MPMediaType.Music); //type of media to return var property = MPMediaItem.MediaTypeProperty; var predicate = MPMediaPropertyPredicate.PredicateWithValue (value, property); _mediaQuery.AddFilterPredicate (predicate); var valueTwo = NSString.FromObject ((String)chosenTitle); var propertyTwo = MPMediaItem.TitleProperty; var predicateTwo = MPMediaPropertyPredicate.PredicateWithValue (valueTwo, propertyTwo); _mediaQuery.AddFilterPredicate (predicateTwo); _musicPlayer = new MPMusicPlayerController (); // volume is dicpercated in ios7 _musicPlayer.SetQueue (_mediaQuery); _musicPlayer.CurrentPlaybackTime = aVeryGoodPlaceToStart; Console.WriteLine ("afterQueSet: {0}", _musicPlayer.CurrentPlaybackTime); positionSkipBtn.Enabled = true; // set the end file length double fileLengthRaw = _musicPlayer.NowPlayingItem.PlaybackDuration; int fileLengthInt = Convert.ToInt32 (fileLengthRaw); string fileLengthDisplay = string.Format ("{0:##}:{1:00}:{2:00}", fileLengthInt / 3600, (fileLengthInt / 60) % 60, fileLengthInt % 60); lengthLbl.Text = fileLengthDisplay; int startingPlaceInt = Convert.ToInt32 (aVeryGoodPlaceToStart); string aVeryGoodPlaceToStartDisplay = string.Format("{0:#0}:{1:00}:{2:00}",startingPlaceInt/3600,(startingPlaceInt/60)%60,startingPlaceInt%60); currentTimeLbl.Text = aVeryGoodPlaceToStartDisplay; Console.WriteLine ("resume point: {0}", aVeryGoodPlaceToStart); // debugging positionSld.MaxValue = (float)(fileLengthRaw); positionSld.SetValue ((float)(aVeryGoodPlaceToStart), true); titleLbl.Text = chosenTitle; artistLbl.Text = resumingAuthor; playPauseBtn.Enabled = true; Console.WriteLine ("ran righ over it"); // debugging Console.WriteLine ("attheresumeend: {0}", _musicPlayer.CurrentPlaybackTime); }