2014-02-24 57 views

回答

6

很有用的指南 - Lock screen 「Now Playing」 with MPNowPlayingInfoCenter;

這隻適用於iOS 5+,並做了這樣的事情。

- (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]; 
    } 
} 

下一次,嘗試使用搜索:

+0

非常感謝您! – jpblack

+0

問題已被標記爲重複,除了解決問題的代碼我添加了相當有用的教程以解釋以下代碼... – ignotusverum

相關問題