Resolved Getting track index with Mediaplayer

Discussion in 'iOS Programming' started by xArtx, Dec 28, 2012.

  1. xArtx, Dec 28, 2012
    Last edited: Jan 2, 2013

    xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    Hi Guys,

    Above the timeline slider in the stock iPhone music player,
    you see a feild something like "33 of 58" where 58 is the total number of tracks in the current playlist,
    and 33 is the current index of the track playing.
    (at least in the iPhone 5 media player "Now Playing" screen)

    I would like to know how that information is retrieved.
    I can't see it in mediaplayer documentation.
    Thanks:)
    Art.
     
  2. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #2
    Well the first part was easy:
    Code:
    itemindex = myPlayer.indexOfNowPlayingItem;
    
    Now I have "33 of XX".

    Just looking for the way to find the total number of tracks in the current playlist now..
     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Just so you know, the built in music player app is allowed to use private apis whereas your app is not... it's possible that it finds that number using private apis.

    (I'm not saying that's the case, just letting you know that's a possibility... don't think that something must be possible within your own app just because a built in app can do it.)
     
  4. xArtx, Dec 28, 2012
    Last edited: Dec 28, 2012

    xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #4
    It would be fine if the playlist was created in the app like the AddMusic sample.
    I could just increment a counter as I cycled through the list array,
    but I'm using the active playlist initiated by the stock music player.

    I CAN cycle through all playlists and count the elements in each playlist,
    but I don't yet know how to tell which playlist is active.
    The joy!

    edit.. also a possibility to turn on repeat, turn off shuffle, silently jump back to track index 0,
    then jump back one more and get the total index from the last track in the playlist.
    It just depends how slow that makes program launch if the playlist is large.


    [​IMG]
     
  5. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #5
    Hehe..

    Code:
    
        nowPlayingItem = myPlayer.nowPlayingItem; // get intital info for comparison
        origindex = myPlayer.indexOfNowPlayingItem;
        ititleLabel = @" ";
        NSString *ititleString = [nowPlayingItem valueForProperty:MPMediaItemPropertyTitle];
        if (ititleString) {
        ititleLabel = [NSString stringWithFormat:@"%@",ititleString];
        } else {
        ititleLabel = @" ";
        }
            
            
            
            MPMediaQuery *myPlaylistsQuery = [MPMediaQuery playlistsQuery];
            NSArray *playlists = [myPlaylistsQuery collections];
            
            for (MPMediaPlaylist *playlist in playlists) {
                oitemcount = 0;
                
               MPMediaPlaylistPropertyName]);
                
                NSArray *songs = [playlist items];
                for (MPMediaItem *song in songs) {
                    
                    songTitle =
                    [song valueForProperty: MPMediaItemPropertyTitle];
                    
                    if (oitemcount == origindex) {
                    if ([ititleLabel isEqualToString:songTitle]) {playlistmatch = 1;}
                    }
                    
                    oitemcount++;
                }
                
                if (playlistmatch == 1) {itemcount = oitemcount; playlistmatch = 0; showindex = 1;}
            }
            
            
    
    if showindex = 1, a song title matching the nowplaying song title
    was found in a playlist, and it also shares the same queue index!
    In that case, the total tracks in the current playlist is in itemcount,
    and show index is the flag to display the "currentindex of totalindex" info
    :D
     
  6. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #7
    As much as it's going to be, but I can't edit the thread title
     

Share This Page