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

2

17

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?

Bibliotheca answered 19/2, 2014 at 20:51 Comment(0)
P
22

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];
Philadelphia answered 19/2, 2014 at 23:48 Comment(4)
Thanks @mdb983 - that was the missing piece!Bibliotheca
Total life saver! I had the same problem as the original author and this made the trick for me :)Madiemadigan
@Madiemadigan .. your up-vote was much appreciated. Thanks!Philadelphia
Tried everything relating to background playback and nothing helped. Who could have guessed it was about showing more info to the user? This is soooo Appley :)Trilinear
P
0

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")
    }
Pagel answered 7/5, 2016 at 5:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.