0

对于我正在使用的音频

- (void)viewDidLoad {

    NSString *soundFilePath = [[NSBundle mainBundle] pathForResource: @"Music" ofType: @"m4a"];

    NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: soundFilePath];

    AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
    self.audioPlayer = newPlayer;
    [newPlayer release];

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

    if (self.audioPlayer) {
        [self updateViewForPlayerInfo];
        [self updateViewForPlayerState];
        [self.audioPlayer setDelegate:self];
                           }

    [fileURL release];
    [super viewDidLoad];

                    }

并且设置了plist“应用程序播放音频”,
按下主页时播放背景声音,但来自多任务切换器或锁定屏幕的遥控器不起作用,背景音频无法暂停或转发/倒带。

怎么了?

谢谢!

4

1 回答 1

0

您需要添加一些东西来接收这些事件调用。

在您的视图控制器中添加

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

在您的应用委托中添加

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

        case UIEventSubtypeRemoteControlTogglePlayPause:
            //handle play button
            break;

        case UIEventSubtypeRemoteControlPreviousTrack:
            //handle rew button
            break;

        case UIEventSubtypeRemoteControlNextTrack:
            //handle ff button
            break;

        default:
            break;
        }
    }
}
于 2013-05-01T21:05:37.360 回答