Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPRemoteCommandCenter control center pause button doesn't update even after the audio file is paused

I’m working on an audio recording application which deals with MPRemoteCommandCenter class. I’ve setup the command center to play and pause commands. Code snippet added below. I’m facing an issue with pauseCommand.

From the application I start playing the audio file which updates the control center as well with the file being played and play button changed to pause button. Now when the file is being played, I pause the audio from control center choosing the pause button. This is invoking the pauseCommand handler in the application and the audio file is paused but the control centre continuous to update the seek bar and the pause button doesn’t change to play button.

-(void) setupRemoteControls
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

[commandCenter.pauseCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {

    if ([self pauseAudioPlayback]) 
        return MPRemoteCommandHandlerStatusSuccess;
    else
        return MPRemoteCommandHandlerStatusCommandFailed;
} ];

[commandCenter.playCommand addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent *event) {

    if ([self resumeAudioPlayback])
        return MPRemoteCommandHandlerStatusSuccess;
    else
        return MPRemoteCommandHandlerStatusCommandFailed;
} ];

}

// This method is invoked in both pauseAudioPlayback & resumeAudioPlayback methods

- (void) updateRemoteControls
{
MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

if (self.audioPlayer)
{
    if (self.isPlaying)
        commandCenter.playCommand.enabled = NO;
    else if (self.isPlayPaused)
        commandCenter.playCommand.enabled = YES;
}
else
    [self clearNowPlayingInfo];

}

Please let me know if any additional information is required.

like image 938
Dantuluri Avatar asked Dec 10 '22 22:12

Dantuluri


2 Answers

For iOs 9.2:

I've tried nearly all combinations of settings in MPNowPlayingInfoCenter and MPRemoteCommandCenter.

To stop updating the seek bar it was enough to set

MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime

But to toggle pause button it is needed to deactivate AVAudioSession after pausing audioPlayback.

AVAudioSession.sharedInstance().setActive(false)

Finally my pauseCommand handler looks like:

MPRemoteCommandCenter.sharedCommandCenter().pauseCommand.addTargetWithHandler { (e) -> MPRemoteCommandHandlerStatus in
        player?.pause()
        let infoCenter = MPNowPlayingInfoCenter.defaultCenter()
        infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyPlaybackRate] = 0.0
        infoCenter.nowPlayingInfo?[MPNowPlayingInfoPropertyElapsedPlaybackTime] = player?.currentTime

        _ = try? AVAudioSession.sharedInstance().setActive(false)

        return MPRemoteCommandHandlerStatus.Success
}

Hope, this helps somebody.

like image 80
pgmarchenko Avatar answered Jan 04 '23 23:01

pgmarchenko


bruce1337's answer seems to be correct

In Swift, the following (finally) turns the play button to pause & vice versa:

let nowPlaying = [MPMediaItemPropertyAlbumTitle: "Led Zep IV",
                  MPMediaItemPropertyTitle: "Black Dog",
                  MPNowPlayingInfoPropertyPlaybackRate: NSNumber(float: 1.0)]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = nowPlaying
like image 25
Rumpu-Jussi Avatar answered Jan 05 '23 00:01

Rumpu-Jussi