Why do the Lock Screen audio controls disappear when I pause AVAudioPlayer?

青春壹個敷衍的年華 提交于 2019-12-05 13:24:47

问题


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.

Here's the 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?


回答1:


You're on the right track ...

You seem to be missing setting;

  MPNowPlayingInfoCenter nowPlayingInfo

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



回答2:


Add this Code in ViewDidLoad() For background play. It work for me. You should try it

    UIApplication.sharedApplication().beginReceivingRemoteControlEvents()

    let session:AVAudioSession = AVAudioSession.sharedInstance()

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


来源:https://stackoverflow.com/questions/21892151/why-do-the-lock-screen-audio-controls-disappear-when-i-pause-avaudioplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!