0

我是新的 iOS 开发者。我正在尝试使正在播放栏(主屏幕底部栏)中的播放/暂停按钮模拟我的应用程序中的播放/暂停按钮。请提供任何帮助。

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback
                                       error:nil];
[[AVAudioSession sharedInstance] setActive:YES
                                     error:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self.player play];

UIBackgroundTaskIdentifier newTaskId = UIBackgroundTaskInvalid;
if([player play]){
    newTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
        [[UIApplication sharedApplication] endBackgroundTask: bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    //Your bg code here

    [[UIApplication sharedApplication] endBackgroundTask: bgTask];
    bgTask = UIBackgroundTaskInvalid;
4

1 回答 1

0

调用后:

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

在您的应用程序委托中,您将在按下按钮时收到事件......像这样处理它们:

-(void)remoteControlReceivedWithEvent:(UIEvent *)event
{
    switch(event.subtype)
    {
        case UIEventSubtypeRemoteControlPause:
            [player pause];
            break;
        case UIEventSubtypeRemoteControlPlay:
            [player play];
            break;

    }
}

并为下一个/上一个/等添加其他案例......

于 2013-12-05T16:59:33.787 回答