【问题标题】:iOS. Play/Pause and Forward non iPod music players in backgroundIOS。在后台播放/暂停和转发非 iPod 音乐播放器
【发布时间】:2016-07-30 06:44:42
【问题描述】:

我正在寻找一种可以控制播放、暂停和转发音乐播放器(例如 Google Play 或 Spotify 应用)的解决方案。 以下代码适用于默认音乐应用播放/暂停音乐:

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

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

然后转发下一首歌曲:

[iPodMusicPlayer skipToNextItem];

有没有办法对其他音乐播放器做同样的事情?

我尝试了下一个代码但没有成功:

bool active = [[AVAudioSession sharedInstance] isOtherAudioPlaying];

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

else{
    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
}

【问题讨论】:

  • 我已经意识到如果我的应用程序在前台运行它可以正常工作,但在后台我收到错误:无法完成操作。 (OSStatus 错误 560557684。)在行:[[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];和 [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil];
  • 你能提供更多信息吗?我正在尝试做同样的事情,但没有成功

标签: ios events avaudiosession mpmusicplayercontroller


【解决方案1】:

错误 560557684 是针对 AVAudioSessionErrorCodeCannotInterruptOthers。当您的后台应用程序尝试激活不与其他音频会话混合的音频会话时,就会发生这种情况。后台应用无法启动不与前台应用的音频会话混合的音频会话,因为这会中断用户当前正在使用的应用的音频。

要解决此问题,请确保将会话类别设置为可混合的类别,例如 AVAudioSessionCategoryPlayback。还要确保设置类别选项 AVAudioSessionCategoryOptionMixWithOthers(必需)和 AVAudioSessionCategoryOptionDuckOthers(可选)。

AVAudioRecorder not recording in background after audio session interruption ended

【讨论】:

    猜你喜欢
    • 2017-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    相关资源
    最近更新 更多