3

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.


My current approach:

  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.

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.

This comes with a couple flaws:

  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.

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?

elbowpa
  • 498
  • 1
  • 5
  • 9

2 Answers2

1

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.

My solution:

#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();
    }
}

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

All of this reside in app delegate implementation file.

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.

Martin Berger
  • 1,639
  • 3
  • 18
  • 39
0

I had similar issue in my iOS application and got it resolved through 'Bamsworld's solution. Check the link for the detailed information.

The solution:

Try this in you applicationDelegates method - (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");
}
Community
  • 1
  • 1
ArunJTS
  • 264
  • 5
  • 15