2014-09-11 244 views

回答

0

MPMusicPlayerController有一個方法:

+(MPMusicPlayerController *)iPodMusicPlayer; 

的iPod音樂播放器採用了iPod應用程序以您的名義。在 實例化中,它將呈現當前的iPod應用程序狀態並控制 ,該狀態表示您的應用程序正在運行。具體而言,共享狀態包括 以下:

重複模式(見「重複模式」)隨機播放模式(見「隨機模式」 現在扮演選項(見nowPlayingItem)播放狀態(參見 playbackState)的其它方面。iPod的狀態,如在這去 播放列表,不是共享的音樂正在播放繼續 您的應用程序移動到後臺時發揮

您可以檢查其playbackState這可能是:

enum { 
    MPMusicPlaybackStateStopped, 
    MPMusicPlaybackStatePlaying, 
    MPMusicPlaybackStatePaused, 
    MPMusicPlaybackStateInterrupted, 
    MPMusicPlaybackStateSeekingForward, 
    MPMusicPlaybackStateSeekingBackward 
}; 
typedef NSInteger MPMusicPlaybackState; 

如果playbackState更改爲MPMusicPlayerControllerPlaybackStateDidChangeNotification,您也可以收到通知。

@property (nonatomic, strong) MPMusicPlayerController *musicPlayer; 
-(void)iPodMusicPlayer 
{ 
    musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
    switch ([musicPlayer playbackState]) 
    { 
     case: MPMusicPlaybackStateStopped: 
      NSLog(@"iPod player is stopped)"; 
      //Do something 
      break; 
     case: MPMusicPlaybackStatePaused: 
      NSLog(@"iPod player is paused"); 
      //Do something 
      break; 
     case: MPMusicPlaybackStatePlaying: 
      NSLog(@"iPod player is playing"); 
      //Do something 
      break; 
     //Etc. 
     default: 
      break; 
    } 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(musicPlayerPlayBackStatusChanged:) 
               name:MPMusicPlayerControllerPlaybackStateDidChangeNotification 
               object:nil]; 

    [musicPlayer beginGeneratingPlaybackNotifications]; 
} 

-(void)musicPlayerPlayBackStatusChanged:(NSNotification *)notification 
{ 
    switch ([musicPlayer playbackState]) 
    { 
     case: MPMusicPlaybackStateStopped: 
      NSLog(@"iPod player is stopped)"; 
      //Do something 
      break; 
     case: MPMusicPlaybackStatePaused: 
      NSLog(@"iPod player is paused"); 
      //Do something 
      break; 
     case: MPMusicPlaybackStatePlaying: 
      NSLog(@"iPod player is playing"); 
      //Do something 
      break; 
     //Etc. 
     default: 
      break; 
    } 
} 
+0

這似乎是正確的! :)我只是不能放在我的代碼中,你能告訴我如何在IF語句中使用,lika:「if(Music.player == pause){} – flang1 2014-09-12 07:34:23

+0

添加示例代碼 – Larme 2014-09-12 08:58:20

+0

完美工作!! :)你知道是否有辦法在我的iPhone上使用所有音樂嗎?(Spotify,podcaster,soundcloud等)?也許我需要som kind api:/ TY! – flang1 2014-09-12 10:53:51