设备锁定时播放iPod Library中的AVAudio [英] Playing AVAudio from iPod Library while device is locked

查看:85
本文介绍了设备锁定时播放iPod Library中的AVAudio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的问题.

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

I've set up my program to be able to play AVAudioPlayer and AVPlayer in the background, which is working fine. I can play a song, lock my screen and the sound will continue to play.

我遇到的麻烦是在屏幕已锁定的情况下拨打了[AVPlayer play]的电话.最终导致没有音乐播放.

What I'm having trouble with is calling [AVPlayer play] whilst my screen is ALREADY locked. This ultimately results in no music being played.

推荐答案

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

You need to tell your player to listen for control events:

- (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];
}

然后您可以像这样对它们执行操作:

Then you can act on them like so:

- (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];
            }
        }
}

这篇关于设备锁定时播放iPod Library中的AVAudio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆