3

只是一个快速的问题。

我已经将我的程序设置为能够在后台播放AVAudioPlayerAVPlayer效果很好。我可以播放歌曲,锁定屏幕,声音将继续播放。

我遇到的问题是[AVPlayer play]在我的屏幕已经锁定时打电话。这最终导致没有音乐被播放。

4

1 回答 1

4

你需要告诉你的播放器监听控制事件:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

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

然后你可以像这样对它们采取行动:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    if (event.type == UIEventTypeRemoteControl) 
        {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) 
            {
            [AVPlayer play];
            } 

        else if (event.subtype == UIEventSubtypeRemoteControlPause) 
            {
            [AVPlayer pause];
            } 
        else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) 
            {
                if (!AVPlayer.playing) 
                    {
                        [AVPlayer play];

                    } else if (AVPlayer.playing) 
                    {
                        [AVPlayer pause];
                    }
            }
        else if (event.subtype == UIEventSubtypeRemoteControlNextTrack)
            {
            [self myNextTrackMethod];
            }
        else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack)
            {
            [self myLastTrackMethod];
            }
        }
}
于 2012-07-17T04:17:42.793 回答