Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play/Pause next/Prev buttons are greyed out in control center

In my application, playback is controlled from control center. When playback is going on in AVPlayer(At this time playback controls are working fine from control center), I am loading a webview with other streaming URL.

Once streaming is done again I am starting playback from AVPlayer. After this, Playback controls are greyed out in control center.

I am using [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo to enable playback control in control center.

What would be the problem?

like image 669
jailani Avatar asked Dec 14 '22 11:12

jailani


1 Answers

I ran into this problem as well working with an AVPlayer instance. You can use MPRemoteCommandCenter to set up controls on the lock screen and command center.

MPRemoteCommandCenter *commandCenter = [MPRemoteCommandCenter sharedCommandCenter];

commandCenter.previousTrackCommand.enabled = YES;
[commandCenter.previousTrackCommand addTarget:self action:@selector(previousTapped:)];

commandCenter.playCommand.enabled = YES;
[commandCenter.playCommand addTarget:self action:@selector(playAudio)];

commandCenter.pauseCommand.enabled = YES;
[commandCenter.pauseCommand addTarget:self action:@selector(pauseAudio)];

commandCenter.nextTrackCommand.enabled = YES;
[commandCenter.nextTrackCommand addTarget:self action:@selector(nextTapped:)];

previousTapped:, playAudio, pauseAudio, and nextTapped are all methods in my view controller that call respective methods to control my AVPlayer instance. To enable an action, you must explicitly set enabled to YES and provide a command with a target and selector.

If you need to disable a specific action, you must explicitly set the enabled property to NO in addition to adding a target.

commandCenter.previousTrackCommand.enabled = NO;
[commandCenter.previousTrackCommand addTarget:self action:@selector(previousTapped:)];

If you do not set enabled for the command, the item will not appear at all on the lock screen or in command center.

In addition, remember to set your app up for background playback (add the UIBackgroundModes audio value to your Info.plist file.), set the player active, and check for errors:

NSError *setCategoryError;
NSError *setActiveError;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:&setCategoryError];
[[AVAudioSession sharedInstance] setActive:YES error:&setActiveError];
like image 109
JAL Avatar answered Apr 01 '23 07:04

JAL