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 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 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 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; })); }
// 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 })); }
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); * }*/ }
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); }
// Get the songs on the device public Dictionary <string, List <Song> > queryForSongs() { MPMediaQuery query = MPMediaQuery.ArtistsQuery; /* * 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.IsCloudItemProperty); 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); }
// 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]; }