Skip to content
This repository was archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
fix(ios): update playback state using :setPlaybackState in iOS 11+
Browse files Browse the repository at this point in the history
Fixes gh-242
  • Loading branch information
timmywil authored and tanguyantoine committed Apr 26, 2019
1 parent 55ce2c2 commit b2b1acc
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions ios/MusicControlManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,18 @@ - (dispatch_queue_t)methodQueue
}

NSMutableDictionary *details = [originalDetails mutableCopy];
NSString *state = [details objectForKey:MEDIA_STATE];

// Set the playback rate from the state if no speed has been defined
// If they provide the speed, then use it
if ([details objectForKey:MEDIA_STATE] != nil && [details objectForKey:MEDIA_SPEED] == nil) {
NSNumber *speed = [[details objectForKey:MEDIA_STATE] isEqual:MEDIA_STATE_PAUSED]
if (state != nil && [details objectForKey:MEDIA_SPEED] == nil) {
NSNumber *speed = [state isEqual:MEDIA_STATE_PAUSED]
? [NSNumber numberWithDouble:0]
: [NSNumber numberWithDouble:1];

[details setValue:speed forKey:MEDIA_SPEED];
}
if ([[details objectForKey:MEDIA_STATE] isEqual:MEDIA_STATE_STOPPED]) {
if ([state isEqual:MEDIA_STATE_STOPPED]) {
MPRemoteCommandCenter *remoteCenter = [MPRemoteCommandCenter sharedCommandCenter];
[self toggleHandler:remoteCenter.stopCommand withSelector:@selector(onStop:) enabled:false];
}
Expand All @@ -91,6 +92,17 @@ - (dispatch_queue_t)methodQueue

center.nowPlayingInfo = [self update:mediaDict with:details andSetDefaults:false];

// Playback state is separated in 11+
if (@available(iOS 11.0, *)) {
if ([state isEqual:MEDIA_STATE_PLAYING]) {
center.playbackState = MPNowPlayingPlaybackStatePlaying;
} else if ([state isEqual:MEDIA_STATE_PAUSED]) {
center.playbackState = MPNowPlayingPlaybackStatePaused;
} else if ([state isEqual:MEDIA_STATE_STOPPED]) {
center.playbackState = MPNowPlayingPlaybackStateStopped;
}
}

NSString *artworkUrl = [self getArtworkUrl:[originalDetails objectForKey:@"artwork"]];
[self updateArtworkIfNeeded:artworkUrl];
}
Expand Down Expand Up @@ -371,7 +383,7 @@ - (void)audioInterrupted:(NSNotification *)notification {
NSInteger interruptionType = [notification.userInfo[AVAudioSessionInterruptionTypeKey] integerValue];
NSInteger interruptionOption = [notification.userInfo[AVAudioSessionInterruptionOptionKey] integerValue];
bool delayedSuspendedNotification = (@available(iOS 10.0, *)) && [notification.userInfo[AVAudioSessionInterruptionWasSuspendedKey] boolValue];

if (interruptionType == AVAudioSessionInterruptionTypeBegan && !delayedSuspendedNotification) {
// Playback interrupted by an incoming phone call.
[self sendEvent:@"pause"];
Expand Down

0 comments on commit b2b1acc

Please sign in to comment.