锁定屏幕音响控制为什么会消失,当我停下AVAudioPlayer? [英] Why do the Lock Screen audio controls disappear when I pause AVAudioPlayer?

查看:421
本文介绍了锁定屏幕音响控制为什么会消失,当我停下AVAudioPlayer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using an instance of AVAudioPlayer to play an audio file. The app is configured to play audio in the background and an appropriate audio session is set. I am also successfully receiving remote control events.

这里的code:

#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

当我运行应用程序的音频播放时的观点负载。该应用程序将继续当它进入背景模式播放音频。我能够成功地暂停和/或从控制中心播放音频(访问无论从应用程序或锁定屏幕内),但是,如果我进入锁屏音频控制和暂停播放,音乐暂停和锁屏控制消失。我希望在音乐暂停,但不能用于控制消失。

When I run the app the audio plays when the view loads. The app continues to play audio when it goes into background mode. I am able to successfully pause and/or play audio from the Control Center (accessed either from within the app or the Lock Screen) but, if I access the Lock Screen audio controls and pause the player, the music pauses and the lock screen controls disappear. I expect the music to pause, but not for the controls to disappear.

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

In other audio apps that I use you can pause, then play, audio from the Lock Screen. Have I overlooked something? Is this a correct approach to do something like this?

推荐答案

您是在正确的轨道上......

You're on the right track ...

您似乎缺少设置;

  MPNowPlayingInfoCenter nowPlayingInfo

如果没有它,你将得到的结果说明,IE pressing暂停后,锁屏不再显示暂停,或者实际上它是播放歌曲。下面是关于如何设置的指南(我已经采取了这种从工作code我做了一段时间回来,但我相信你能弄清楚什么是什么)。

Without it, you will get the results described, IE after pressing pause, the lock screen no longer shows the pause, or indeed that it is playing a song. Here's a guide on how to set it (i've taken this from working code I did some time back, but I'm sure you can figure out what's what).

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

这篇关于锁定屏幕音响控制为什么会消失,当我停下AVAudioPlayer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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