Problem with Sprite Kit, how to prevent brake light

I have a problem using the Sprite Kit and when I want to play sound after user interaction.

The user listens to music using an application (such as Spotify).
A user wants to play my game while he is listening to his song. Currently, when an application launches my application, its song stops.

I would like to prevent this behavior: in fact: how to not play sound if another application uses “sound” or how can I play sound? And continue playing the user's music.

This is currently my code:

@property (strong, nonatomic) SKAction *hitSound;

In the init method:

 _hitSound = [SKAction playSoundFileNamed:@"laugh.caf" waitForCompletion:NO];

After clicking on the user screen:

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
 SKAction *sequence = [SKAction sequence:@[_hitSound]];
    [node runAction:sequence completion:^{
       //Stuff
}];

}

+4
1

AVAudioSession, . , .

AVAudioSession AVFoundation. AVFoundation . .

#import <AVFoundation/AVFoundation.h>

, : didFinishLaunchingWithOptions:.

// activate a mixable session
// set audio session category AVAudioSessionCategoryPlayback

     [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil];
     [[AVAudioSession sharedInstance] setMode:AVAudioSessionModeDefault error:nil];
     [[AVAudioSession sharedInstance] setActive:YES error: nil];

, BOOL say 'mute' , SKAction . , , " " . , . .

, :

BOOL isPlayingAudio = [[AVAudioSession sharedInstance] otherAudioPlaying];

, self.mute .

AVAudioSession - AVAudioSession

+6

Source: https://habr.com/ru/post/1541273/


All Articles