Develop

[ios] Sprite Kit & 사운드 재생시 백그라운드 진입시 앱이 비정상적으로 종료됨

by hooni posted Apr 18, 2014
?

Shortcut

PrevPrev Article

NextNext Article

ESCClose

Larger Font Smaller Font Up Down Go comment Print

Sprite Kit & playing sound leads to app termination


게임에서 중요한 효과 중에 하나인 사운드.

Sprite Kit을 이용하는 경우 아래와 같이 SKAction class method를 이용해 사운드를 재생할 수 있다.

@implementation Scene{
    . . .
    SKAction *pointSound;
}

- (void) initSounds
{
    . . .
    pointSound = [SKAction playSoundFileNamed:@"point.mp3"
        waitForCompletion:NO];
}

- (void) updateScore:(NSTimeInterval) currentTime
{
    . . .
    [self runAction:pointSound];
}

하지만, 앱이 백그라운드로 진입하면 비정상적으로 종료되는 현상이 발생된다.

(이 현상은 설치된 앱보다 디버깅 상태에서 쉽게 발견할 수 있음)


앱이 백그라운드에 진입하면 AVAudioSession이 활성화 될 수 없기 때문이다.

하지만, Sprite Kit 내부적으로 AVAudioSession을 사용한다는 언급이 없기 때문에 명확한 원인은 아닐 수 있다.

이런 원인으로 접근했을 때 개선하는 방법은 간단하다.

아래처럼 AVAudioSessoin을 앱이 백그라운드 상태 동안 비활성, 포그라운드로 진입하면 활성화로 설정하는 코드를 추가하면 된다.


AVAudioSession 활성화 설정 코드

#import <AVFoundation/AVFoundation.h>
...

- (void)applicationWillResignActive:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // prevent audio crash
    [[AVAudioSession sharedInstance] setActive:NO error:nil];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // resume audio
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}


[출처] http://stackoverflow.com/questions/18976813/sprite-kit-playing-sound-leads-to-app-termination