在带有AVPlayer的iOS中,使用背景音频模式中的遥控器管理播放头

时间:2014-09-03 20:43:18

标签: ios audio background avplayer

我尝试控制用户可以在歌曲播放中提前几秒。我管理的只是用户可以看到播放头而不与它互动。

我在AVAudioSessionCategoryPlayback模式下使用AVAudioSession。 AVPlayer和AVPlayerItem

_playerItem = [AVPlayerItem playerItemWithURL:url];
_player = [AVPlayer playerWithPlayerItem:self.playerItem];

[编辑]这样,我控制了远程事件:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl)
    {
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlPlay");
        } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlPause");
        } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlTogglePlayPause");
        } else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingBackward) {
            NSLog(@"NEVER Responds!!!");
        } else if (event.subtype == UIEventSubtypeRemoteControlBeginSeekingForward) {
            NSLog(@"NEVER Responds!!!");
        } else if (event.subtype == UIEventSubtypeRemoteControlNextTrack){
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlNextTrack");
        } else if (event.subtype == UIEventSubtypeRemoteControlPreviousTrack){
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlPreviousTrack");
        } else if (event.subtype == UIEventSubtypeRemoteControlStop){
            NSLog(@"Responds nice to the event UIEventSubtypeRemoteControlStop");
        }
    }
}

如何激活在backgroud音频中运行的遥控器(见屏幕截图)中的栏(iPhone被阻止)。甚至不知道是否可能。

¿有可能吗?在'音乐原生App iOS'工作正常!

ScreemShot background audio player

1 个答案:

答案 0 :(得分:0)

AVPlayer类有seekToTime:方法,可以执行您想要的操作。

在进度条回调中,将进度条位置所代表的百分比乘以媒体总时间,并将该值传递给seekToTime: