首页 文章

从iOS应用程序中控制非iPod音乐播放器(如Spotify)

提问于
浏览
2

在我的应用程序中,我需要在播放自己的声音时暂停并重新启动背景音乐 .

iPod的方法是使用:

[[MPMusicPlayerController iPodMusicPlayer] pause];

并且:

[[MPMusicPlayerController iPodMusicPlayer] play];

对于非iPod应用程序,这没有任何影响 .

第二次尝试是启用闪避:

UInt32 property = sender.on;
    AudioSessionSetProperty(kAudioSessionProperty_OtherMixableAudioShouldDuck,sizeof(property),&property);
    AudioSessionSetActive(property);

也适用于iPod音乐播放器,而非Spotify .

设置 kAudioSessionCategory_SoloAmbientSound 会停止所有音乐,但不会重新启动它 .

2 回答

  • 0

    控制它的方法是使用 AVAudioSessionsetActive 方法 .

    如果要播放声音,请将会话设置为活动状态:

    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    

    当您停止播放时,将会话设置为非活动状态,但使用该标志也会通知其他人 . 此标志应该导致其他应用程序重新启动(应该适用于iPod和其他应用程序) . 对于带有AVAudioSession的iOS6,你可以这样做:

    [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersonDeactivation error:nil];
    

    iOS 6之前版:

    [[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation error:nil];
    
  • 3

    你也不需要 . iOS只允许一个应用程序一次播放音频,并会自动暂停音乐播放器,以便您可以播放音乐,因为您的应用程序具有音乐播放优先权 . 只要玩你的,让iOS处理它 .

相关问题