当我暂停AVAudioPlayer时,为什么锁屏音频控件会消失?

时间:2014-02-19 20:51:14

标签: ios objective-c audio avaudioplayer uievent

我正在使用AVAudioPlayer的实例来播放音频文件。该应用程序配置为在后台播放音频,并设置适当的音频会话。我也成功接收了遥控事件。

以下是代码:

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()

@property (nonatomic) AVAudioPlayer *player;

@end

@implementation ViewController

@synthesize player;

- (BOOL)canBecomeFirstResponder { return YES; }

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Turn on remote control event delivery

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

    // Set ourselves as the first responder

    [self becomeFirstResponder];

    // Set the audio session

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *setCategoryError = nil;
    BOOL success = [audioSession setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
    NSError *activationError = nil;
    success = [audioSession setActive:YES error:&activationError];

    // Play an mp3 with AVAudioPlayer

    NSString *audioFileName = @"%@/Via_Aurora.mp3";
    NSURL *audioURL = [NSURL fileURLWithPath:[NSString stringWithFormat:audioFileName, [[NSBundle mainBundle] resourcePath]]];
    player = [[AVPlayer alloc] initWithURL:audioURL];

    [player play];
}

- (void)viewWillDisappear:(BOOL)animated {

    // Turn off remote control event delivery & Resign as first responder

    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    [self resignFirstResponder];

    // Don't forget to call super

    [super viewWillDisappear:animated];
}

- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {

    if (receivedEvent.type == UIEventTypeRemoteControl) {

        switch (receivedEvent.subtype) {

            case UIEventSubtypeRemoteControlPreviousTrack:
                NSLog(@"prev");
                break;

            case UIEventSubtypeRemoteControlNextTrack:
                NSLog(@"next");
                break;

            case UIEventSubtypeRemoteControlPlay:
                [player play];
                break;

            case UIEventSubtypeRemoteControlPause:
                [player pause];
                break;

            default:
                break;
        }
    }
}

@end

当我运行应用程序时,视频加载时会播放音频。应用程序进入后台模式后继续播放音频。我能够从控制中心成功暂停和/或播放音频(可以从应用程序或锁定屏幕中访问),但是,如果我访问锁定屏幕音频控件并暂停播放器,则音乐暂停并且锁定屏幕控制消失。我希望音乐暂停,但不会让控件消失。

在我使用的其他音频应用中,您可以暂停,然后播放锁定屏幕中的音频。我忽略了什么吗?这是一个正确的做法吗?

2 个答案:

答案 0 :(得分:22)

你走在正确的轨道上......

您似乎缺少设置;

  MPNowPlayingInfoCenter nowPlayingInfo

没有它,您将获得所描述的结果,按下暂停后的IE,锁定屏幕不再显示暂停,或者确实它正在播放歌曲。这是一个关于如何设置它的指南(我从工作代码中拿走了这些,我做了一段时间,但我相信你可以弄清楚它是什么)。

    MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc]initWithImage:albumImage];
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = [NSDictionary dictionaryWithObjectsAndKeys:aSong.songTitle, MPMediaItemPropertyTitle,
                                                             aSong.artistName, MPMediaItemPropertyArtist, artwork, MPMediaItemPropertyArtwork,  1.0f, MPNowPlayingInfoPropertyPlaybackRate, nil];

答案 1 :(得分:0)

在ViewDidLoad()中添加此代码用于后台播放。它对我有用。你应该尝试一下

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

    let session:AVAudioSession = AVAudioSession.sharedInstance()

    do
    {
        try session.setCategory(AVAudioSessionCategoryPlayback)
    }
    catch
    {
        print("Background Play Error")
    }