Search code examples
ios7titleavplayerremote-control

How to set the title on remote control when playing music in background using AVPlayer on ios7


How can I set title music on remote control on ios 7 during I use AVPlayer to play the music in background?


Solution

  • the class you want is MPNowPlayingInfoCenter .

    create a dictionary of values and pass them to it sorta like this...

    NSMutableDictionary *info=[NSMutableDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Song title",@"Artist name",nil] forKeys:[NSArray arrayWithObjects: MPMediaItemPropertyTitle,MPMediaItemPropertyArtist,nil]];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
    

    Make sure you import the following:

    #import <MediaPlayer/MPNowPlayingInfoCenter.h>
    #import <MediaPlayer/MPMediaItem.h>
    #import <MediaPlayer/MPMediaQuery.h>
    

    the keys in the dictionary are as follows:

    MPMediaItemPropertyAlbumTitle
    MPMediaItemPropertyAlbumTrackCount
    MPMediaItemPropertyAlbumTrackNumber
    MPMediaItemPropertyArtist
    MPMediaItemPropertyArtwork
    MPMediaItemPropertyComposer
    MPMediaItemPropertyDiscCount
    MPMediaItemPropertyDiscNumber
    MPMediaItemPropertyGenre
    MPMediaItemPropertyPersistentID
    MPMediaItemPropertyPlaybackDuration
    MPMediaItemPropertyTitle
    MPNowPlayingInfoPropertyElapsedPlaybackTime
    MPNowPlayingInfoPropertyPlaybackRate;
    MPNowPlayingInfoPropertyPlaybackQueueIndex;
    MPNowPlayingInfoPropertyPlaybackQueueCount;
    MPNowPlayingInfoPropertyChapterNumber;
    MPNowPlayingInfoPropertyChapterCount;
    

    source:

    https://developer.apple.com/library/ios/documentation/mediaplayer/reference/MPNowPlayingInfoCenter_Class/Reference/Reference.html