Is MPNowPlayingInfoCenter compatible with AVAudioPlayer?
Asked Answered
O

3

7

I start -play with AVAudioPlayer, and then set the nowPlaying dictionary like this:

NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
        
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage: [UIImage imagedNamed:@"AlbumArt"]];
[songInfo setObject:@"Audio Title" forKey:MPMediaItemPropertyTitle];
[songInfo setObject:@"Audio Author" forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"Audio Album" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];

The lock screen always shows a pause button. I receive the remote control events correctly and I can toggle play/pause through the remote control events, but the lock screen keeps showing "pause" even when it is playing.

Now I seen this work with MPMoviePlayerController. Can someone explain how does the MPNowPlayingInfoCenter determine if it should show a play or a pause button?

Orpha answered 14/9, 2013 at 10:26 Comment(0)
V
1

Have you set the correct AVAudioSessionCategory on the AudioSession? it needs to be AVAudioSessionCategoryPlayback I believe to get it to work.

Vend answered 3/12, 2013 at 15:13 Comment(0)
A
0

I'm not using MPNowPlaying at the moment, but apparently I have to, in order to get the audio info displayed on the lock screen.

However, in addition to what @user3061915 said, to manage the play/pause button, I've used UIEventTypeRemoteControl and it works perfect for controlling the play/pause button:

- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    //if it is a remote control event handle it correctly
    if (event.type == UIEventTypeRemoteControl)
    {
        if (event.subtype == UIEventSubtypeRemoteControlPlay)
        {
            [self playAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlPause)
        {
            [self pauseAudio];
        }
        else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause)
        {
            [self togglePlayPause]; //This method will handle the toggling.
        }
    }
Arin answered 3/12, 2013 at 21:53 Comment(0)
L
0

I just fixed a problem like this in my own app. I originally used [[AVAudioSession sharedInstance] setCategory:withOptions:error:] and supplied AVAudioSessionCategoryOptionMixWithOthers and AVAudioSessionCategoryOptionDuckOthers. This turned out to be my problem. If you set mix with others, you get no remote control events. They still go to the iPod app. If you set duck others, you get remote control events, but it appears as though it causes the problem you describe: the play/pause button shows the wrong thing. I'm not sure why. I got the play/pause button to behave by setting options to 0, or actually just calling setCategory:error:.

Lorenlorena answered 21/9, 2014 at 22:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.