1

我的问题是 - 当我的 iPad 在后台播放音频时,“我的应用程序图标”不会出现。我正在使用 iPodMusicPlayer。为了在后台播放音频,我编写了这些代码..

- (void)viewDidAppear:(BOOL)animated {
      [super viewDidAppear:animated];
      [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
      [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated
{
     [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
     [self resignFirstResponder];
     [super viewWillDisappear:animated];
}

- (BOOL)canBecomeFirstResponder {
        return YES;
}

// 当应用程序在后台时,iPod 控件将发送这些事件

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {
       if (receivedEvent.type == UIEventTypeRemoteControl) {
           switch (receivedEvent.subtype) {

           case UIEventSubtypeRemoteControlTogglePlayPause:
               [self performSelector:@selector(playPause:)];
               break;

           case UIEventSubtypeRemoteControlPreviousTrack:
            //[self previousSong:nil];
            //[self performSelector:@selector(previousSong:)];
            break;

           case UIEventSubtypeRemoteControlNextTrack:
               //[self performSelector:@selector(nextSong:)];
               break;
          default:
              break;
         }
     }
}

和 info.plist 我还设置了“必需的后台模式”

4

1 回答 1

2

您还必须添加UIBackgroundModes到您的Info.plist并将其值设置为audio.

请查看 iOS 应用程序编程指南,特别是应用程序状态和多任务部分,以获取有关如何在后台执行任务的详细信息。

[更新]

还将这两行添加到application:didFinishLaunchingWithOptions:您的AppDelegate.m

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];

当您开始播放音频时,此代码:

UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
[_player play];
newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

这应该这样做。在模拟器和设备上进行了测试。

于 2012-04-10T09:17:39.913 回答