2

I'm looking for a solution that controls play, pause and forward music players such as Google Play or Spotify apps. Following code works fine for default music app to play/pause music:

iPodMusicPlayer = [MPMusicPlayerController iPodMusicPlayer];
if ([iPodMusicPlayer playbackState] == MPMusicPlaybackStatePlaying) {
    NSLog(@"Pause music");
    [iPodMusicPlayer pause];
}

else if ([iPodMusicPlayer playbackState] == MPMusicPlaybackStatePaused){
    NSLog(@"Play music");
    [iPodMusicPlayer play];
}

And to forward next song:

[iPodMusicPlayer skipToNextItem];

Is there any way to do the same with other Music Players?

I have tried next code but with not success:

bool active = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

if (active) {
    [[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}

else{
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}
Maria
  • 334
  • 3
  • 17
  • I have realized it works right if my app is running in foreground, but in background I get the error: The operation couldn’t be completed. (OSStatus error 560557684.) at lines: [[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; and [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; – Maria Aug 21 '14 at 11:25
  • hey could you provide more information? I am trying to do the same thing but have only been unsuccessful – Julio May 30 '15 at 17:55

1 Answers1

2

Error 560557684 is for AVAudioSessionErrorCodeCannotInterruptOthers. This happens when your background app is trying to activate an audio session that doesn't mix with other audio sessions. Background apps cannot start audio sessions that don't mix with the foreground app's audio session because that would interrupt the audio of the app currently being used by the user.

To fix this make sure to set your session category to one that is mixable, such as AVAudioSessionCategoryPlayback. Also be sure to set the category option AVAudioSessionCategoryOptionMixWithOthers (required) and AVAudioSessionCategoryOptionDuckOthers (optional).

AVAudioRecorder not recording in background after audio session interruption ended

Community
  • 1
  • 1
NPV
  • 41
  • 4