【问题标题】:I have an iPad app that plays audio in background but app icon is not appear with ipod controls我有一个在后台播放音频的 iPad 应用程序,但 ipod 控件没有显示应用程序图标
【发布时间】:2012-04-10 07:29:07
【问题描述】:

我的问题是 - 当我的 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我也设置了“必需的后台模式”

【问题讨论】:

    标签: iphone objective-c ios ios5


    【解决方案1】:

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

    请查看 iOS 应用程序编程指南,特别是 App States and Multitasking section,了解有关如何在后台执行任务的详细信息。

    [更新]

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

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

    当你开始播放音频时,这段代码:

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

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

    【讨论】:

    • 我已经设置了这个值。我认为问题是因为 iPodMusicPlayer 但不确定
    猜你喜欢
    • 2014-06-11
    • 2013-09-20
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2021-01-05
    相关资源
    最近更新 更多