【问题标题】:Is MPNowPlayingInfoCenter compatible with AVAudioPlayer?MPNowPlayingInfoCenter 是否与 AVAudioPlayer 兼容?
【发布时间】:2013-09-18 23:49:52
【问题描述】:

我以 AVAudioPlayer 开始 -play,然后像这样设置 nowPlaying 字典:

NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];
[songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

锁定屏幕始终显示暂停按钮。我正确接收到遥控器事件,我可以通过遥控器事件切换播放/暂停,但锁定屏幕即使在播放时也一直显示“暂停”。

现在我看到了 MPMoviePlayerController 的这项工作。有人能解释一下 MPNowPlayingInfoCenter 是如何确定它应该显示播放还是暂停按钮的吗?

【问题讨论】:

    标签: iphone ios audio media-player mpnowplayinginfocenter


    【解决方案1】:

    您是否在AudioSession 上设置了正确的AVAudioSessionCategory?我相信它必须是AVAudioSessionCategoryPlayback 才能让它工作。

    【讨论】:

      【解决方案2】:

      我目前没有使用MPNowPlaying,但显然我必须这样做,才能在锁定屏幕上显示音频信息。

      但是,除了@user3061915 所说的,为了管理play/pause 按钮,我使用了UIEventTypeRemoteControl,它非常适合控制play/pause 按钮:

      - (void)remoteControlReceivedWithEvent:(UIEvent *)event {
          //if it is a remote control event handle it correctly
          if (event.type == UIEventTypeRemoteControl)
          {
              if (event.subtype == UIEventSubtypeRemoteControlPlay)
              {
                  [self playAudio];
              }
              else if (event.subtype == UIEventSubtypeRemoteControlPause)
              {
                  [self pauseAudio];
              }
              else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
              {
                  [self togglePlayPause]; //This method will handle the toggling.
              }
          }
      

      【讨论】:

        【解决方案3】:

        我刚刚在自己的应用程序中解决了这样的问题。我最初使用 [[AVAudioSession sharedInstance] setCategory:withOptions:error:] 并提供了 AVAudioSessionCategoryOptionMixWithOthers 和 AVAudioSessionCategoryOptionDuckOthers。这原来是我的问题。如果您与其他人设置混音,则不会获得远程控制事件。他们仍然使用 iPod 应用程序。如果你设置了其他人,你会得到远程控制事件,但它似乎会导致你描述的问题:播放/暂停按钮显示错误的东西。我不确定为什么。我通过将选项设置为 0 或实际上只是调用 setCategory:error: 来获得播放/暂停按钮的行为。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-07
          • 2011-05-23
          • 2011-10-22
          • 2017-02-12
          • 1970-01-01
          相关资源
          最近更新 更多