当另一个应用程序开始/停止播放音频时,可以通知我的应用程序吗? [英] Can my app be notified when another application starts/stops playing audio?

查看:159
本文介绍了当另一个应用程序开始/停止播放音频时,可以通知我的应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My iOS game has music and sound effects. I'd like to let users listen to their own music in place of the game's background music.

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

A simple solution is to add a new menu item that disables the game's background music. However, I'd like to avoid creating a new menu item unless I can be convinced that this approach is worse for the user.

  1. 将音频会话类别设置为AVAudioSessionCategoryAmbient,以允许将游戏音频与iPod(或其他音乐应用)混合播放.
  2. applicationDidBecomeActive中,检查[[AVAudioSession sharedInstance] isOtherAudioPlaying]并在后台播放其他应用程序时关闭游戏的音乐.
  1. Set the audio session category to AVAudioSessionCategoryAmbient to allow mixing of game audio with iPod (or other music app) playback.
  2. In applicationDidBecomeActive, check [[AVAudioSession sharedInstance] isOtherAudioPlaying] and turn off the game's music if another app is playing in the background.

这似乎在大多数情况下都有效.当游戏启动时,它会中断后台音乐应用程序的播放.如果要播放自己的音乐,则必须故意返回并按第三方音乐应用中的播放,或使用iOS 7的向上滑动控制面板.否则,游戏本身的音乐将接管.

This seems to work under most circumstances. When the game launches, it interrupts the backgrounded music app's playback. If you want to play your own music, you then have to deliberately go back and press play in the third-party music app, or use iOS 7's swipe-up control panel. Otherwise, the game's own music will take over.

这有一些缺陷:

  1. 它不会检测到何时通过iPhone/iPod耳塞内置的单击按钮遥控器开始/停止第三方音乐应用程序的播放.
  2. 尽管它可与iPod应用程序一起使用,但不适用于SoundCloud应用程序.我还没有测试过其他人.
  1. It does not detect when the third-party music app's playback is started/stopped by the click-button remote control that's built into iPhone/iPod earbuds.
  2. Although it works with the iPod app, it doesn't work with the SoundCloud app. I have not tested any others.


问题:

当第三方音乐应用程序(例如iPod应用程序)开始/停止播放时,是否可以接收通知?另外,当@property BOOL otherAudioPlaying的值更改时,是否有一种通知方式?


The question:

Is there a way to receive notifications when third-party music apps (e.g. the iPod app) start/stop playback? Alternatively, is there a way to be notified when the value of @property BOOL otherAudioPlaying changes?

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

Can this approach backfire? Is it foolish not to simply use a menu item?

推荐答案

在我的游戏中,ArunJTS解决方案无效.第一个用户被问到有关麦克风的问题.其次,它不会检测到音乐状态变化,例如,我的应用是否执行了某些操作并开始播放,或者用户是否快速将应用切换到了音乐应用.

In my game, ArunJTS solution didnt worked. First user get asked about microphone. Second it doesnt detect music state changes like, has my app did something and started playing or has user did fast app switch to Music app.

我从官方

What i did i have derived from official app docs. Paragraph: "Checking Whether Other Audio Is Playing During App Launch".

我只是定期检查另一个应用程序是否正在播放内容并做出适当反应.

I just check periodically is another app playing something and react appropriatelly.

我的解决方案:

#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];,仅此而已.

In - (void) applicationDidFinishLaunching:(UIApplication *)application i call [self startCheckingIfOtherAppIsPlayingAudio:YES]; and that's all.

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

All of this reside in app delegate implementation file.

这就是我对您问题的回答.我没有发现有关weater Music应用的任何通知,或者其他一些应用开始做某事.我已经阅读了整个《音频会话编程指南》,并查看了AVPlayer,AVAudioSession类引用,但没有找到类似的东西.如果有人知道更好,更聪明的解决方案,请告诉我.

So this would be my answer to your question. I didnt found out any notifications about weater Music app or some other app started doing something. I have read whole Audio Session Programming Guide and looked AVPlayer, AVAudioSession class references and found no such thing. If anyone know better and more smarter solution please let me know.

这篇关于当另一个应用程序开始/停止播放音频时,可以通知我的应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆