Search code examples
iosiphoneios7lockscreenaudio-player

Best Way To Display Music Play Controls On Lock Screen iOS7


I have a music app. What is the best way to have the song info and music controls show up in the lock screen like SoundCloud - see (http://cl.ly/image/1a1H041A1Z34)

Thank you very much!


Solution

  • Pretty useful guide - Lock screen “Now Playing” with MPNowPlayingInfoCenter;

    This only works with iOS 5+ and is done something like this.

    - (void)setupNowPlayingInfoCenter:(MPMediaItem *)currentSong
    {
        NSString *ver = [[UIDevice currentDevice] systemVersion];
        CGFloat version = 4.0;
        if ([ver length] >= 3)
        {
            version = [[ver substringToIndex:3] floatValue];
        }
    
        if (version >= 5.0)
        {
            MPMediaItemArtwork *artwork = [currentSong valueForProperty:MPMediaItemPropertyArtwork];
    
            MPNowPlayingInfoCenter *infoCenter = [MPNowPlayingInfoCenter defaultCenter];
    
            if (currentSong == nil)
            {
                infoCenter.nowPlayingInfo = nil;
                return;
            }
    
            infoCenter.nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                    [currentSong valueForKey:MPMediaItemPropertyTitle], MPMediaItemPropertyTitle,
                    [currentSong valueForKey:MPMediaItemPropertyArtist], MPMediaItemPropertyArtist,
                    [currentSong valueForKey:MPMediaItemPropertyAlbumTitle], MPMediaItemPropertyAlbumTitle,
                    [currentSong valueForKey:MPMediaItemPropertyAlbumTrackCount], MPMediaItemPropertyAlbumTrackCount,
                    [currentSong valueForKey:MPMediaItemPropertyAlbumTrackNumber], MPMediaItemPropertyAlbumTrackNumber,
                    artwork, MPMediaItemPropertyArtwork,
                    [currentSong valueForKey:MPMediaItemPropertyComposer], MPMediaItemPropertyComposer,
                    [currentSong valueForKey:MPMediaItemPropertyDiscCount], MPMediaItemPropertyDiscCount,
                    [currentSong valueForKey:MPMediaItemPropertyDiscNumber], MPMediaItemPropertyDiscNumber,
                    [currentSong valueForKey:MPMediaItemPropertyGenre], MPMediaItemPropertyGenre,
                    [currentSong valueForKey:MPMediaItemPropertyPersistentID], MPMediaItemPropertyPersistentID,
                    [currentSong valueForKey:MPMediaItemPropertyPlaybackDuration], MPMediaItemPropertyPlaybackDuration,
                    [NSNumber numberWithInt:self.mediaCollection.nowPlayingIndex + 1], MPNowPlayingInfoPropertyPlaybackQueueIndex,
                    [NSNumber numberWithInt:[self.mediaCollection count]], MPNowPlayingInfoPropertyPlaybackQueueCount, nil];
        }
    }
    

    next time, try to use search: