【问题标题】:Can my app be notified when another application starts/stops playing audio?当另一个应用程序开始/停止播放音频时可以通知我的应用程序吗?
【发布时间】:2013-11-21 05:42:54
【问题描述】:

我的 iOS 游戏有音乐和音效。我想让用户听他们自己的音乐来代替游戏的背景音乐。

一个简单的解决方案是添加一个禁用游戏背景音乐的新菜单项。但是,我想避免创建新的菜单项,除非我确信这种方法对用户来说更糟。


我目前的做法:

  1. 将音频会话类别设置为 AVAudioSessionCategoryAmbient 以允许将游戏音频与 iPod(或其他音乐应用程序)播放混合。
  2. applicationDidBecomeActive 中,选中[[AVAudioSession sharedInstance] isOtherAudioPlaying],如果后台正在播放其他应用,请关闭游戏音乐。

这似乎在大多数情况下都有效。当游戏启动时,它会中断背景音乐应用程序的播放。如果你想播放自己的音乐,那么你必须特意返回并在第三方音乐应用程序中按下播放,或者使用 iOS 7 的向上滑动控制面板。否则,游戏自己的音乐将接管。

这有几个缺陷:

  1. 它不会检测第三方音乐应用程序的播放何时由 iPhone/iPod 耳塞内置的点击按钮遥控器开始/停止。
  2. 虽然它适用于 iPod 应用程序,但它不适用于 SoundCloud 应用程序。我没有测试过任何其他人。

问题:

有没有办法在第三方音乐应用(例如 iPod 应用)开始/停止播放时接收通知?或者,有没有办法在@property BOOL otherAudioPlaying的值发生变化时得到通知?

这种方法会适得其反吗?不简单地使用菜单项是愚蠢的吗?

【问题讨论】:

    标签: ios cocoa-touch avfoundation avaudiosession


    【解决方案1】:

    在我的游戏中,ArunJTS 解决方案不起作用。第一个用户被问及麦克风。其次,它没有检测到音乐状态变化,例如,我的应用程序做了什么并开始播放,或者用户快速应用程序切换到音乐应用程序。

    我所做的一切来自官方app docs。 段落:“在应用启动期间检查其他音频是否正在播放”。

    我只是定期检查是否有其他应用在播放并做出适当的反应。

    我的解决方案:

    #pragma mark - Audio
    
    - (void) startCheckingIfOtherAppIsPlayingAudio:(BOOL)shouldStart
    {
        if (shouldStart == YES)
        {
            if (self.timerMusicCheck.isValid)
            {
                return;
            }
    
            //[self.timerMusicCheck fire];
        }
        else
        {
            [self.timerMusicCheck invalidate];
            self.timerMusicCheck = nil;
        }
    }
    
    - (NSTimer*) timerMusicCheck
    {
        if (_timerMusicCheck)
        {
            return _timerMusicCheck;
        }
    
        _timerMusicCheck = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(checkIsOtherAudioPlaying) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timerMusicCheck forMode:NSDefaultRunLoopMode];
    
        return _timerMusicCheck;
    }
    
    - (void) checkIsOtherAudioPlaying
    {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        if (!audioSession.otherAudioPlaying)
        {
            K_LOG("game audio resume");
            KMiscTools::resumeBackgroundMusic();
        }
        else
        {
            K_LOG("game audio suspend");
            KMiscTools::suspendBackgroundMusic();
        }
    }
    

    - (void) applicationDidFinishLaunching:(UIApplication *)application 我打电话给[self startCheckingIfOtherAppIsPlayingAudio:YES]; 就是这样。

    所有这些都驻留在应用委托实现文件中。

    所以这就是我对你问题的回答。我没有发现任何关于weater Music 应用程序或其他应用程序开始做某事的通知。我已经阅读了整个音频会话编程指南并查看了 AVPlayer、AVAudioSession 类引用,但没有发现这样的东西。如果有人知道更好更智能的解决方案,请告诉我。

    【讨论】:

    • 有点不像苹果的方式,仍然没有代表或通知吗?
    【解决方案2】:

    我在我的 iOS 应用程序中遇到了类似的问题,并通过“Bamsworld 的解决方案”解决了这个问题。 详情请查看link

    解决办法:

    在你的 applicationDelegates 方法中试试这个 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    // set audio session category AVAudioSessionCategoryPlayAndRecord with no options
    success = [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
    if (!success) {
        NSLog(@"setCategoryError");
    }
    
    // set audio session mode to default
    success = [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
    if (!success) {
        NSLog(@"setModeError");
    }
    
    // activate audio session
    success = [[AVAudioSession sharedInstance] setActive:YES error:nil];
    if (!success) {
        NSLog(@"activationError");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多