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

标签 ios objective-c audio avaudioplayer uievent

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

代码如下:

#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

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

在我使用的其他音频应用程序中,您可以暂停,然后从锁定屏幕播放音频。我忽略了什么吗?这是执行此类操作的正确方法吗?

最佳答案

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

您似乎缺少设置;

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

关于ios - 为什么当我暂停 AVAudioPlayer 时锁屏音频控件会消失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21892151/

相关文章:

audio - 简单的录音功能,录音 5 秒后保存。

ios - iOS 中的多点连接框架问题

iphone - 优化慢代码——字典的枚举

ios - 如何在 iOS 中制作网格布局?

ios - 自动布局 - 使父 View 高度等于 subview 高度 + subview 为 textView 的常量

objective-c - 将音频从iPhone麦克风转换为iLBC

audio - 使用 SIMD 指令解交错音频 channel

ios - swift 中的静态字典类型变量声明?

ios - UITableview reloadRows 在 Swift 3 中无法正常工作

objective-c - 如何更改 NSMutableArray 中 NSString 的值?