2016-12-03 4 views
1

다음 코드를 사용하여 버튼을 누를 때 간단한 클릭 사운드를 재생합니다.첫 번째 사운드가 지연됩니다.

import AudioToolbox 

private var clickSound: SystemSoundID! 

func createSound(soundName: String) -> SystemSoundID { 
    var id: SystemSoundID = 0 

    let url = CFBundleCopyResourceURL(CFBundleGetMainBundle(), soundName, "wav", nil) 
    AudioServicesCreateSystemSoundID(url, &id) 

    return id 
} 

    //Load click sound. 
    clickSound = createSound("ButtonClick") 

이 기능은 작동하지만 버튼을 처음 누르면 소리가 조금 지연됩니다. 이후의 모든 버튼을 누르면 사운드가 즉시 재생됩니다. 그것은 내가 처음 연주 될 때까지 사운드가로드되지 않는다고 믿게합니다.

지연을 제거 할 수있는 방법이 있습니까?

+0

앱에서 몇 개의 다른 사운드를 재생해야합니까? –

답변

2

알려진 문제입니다.

시동 중에 소리가 약간 작게 들릴 수 있습니다. 그렇게하면 사운드 루틴이로드되고 필요할 때 바로 실행할 수 있습니다.

AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SILENCE" ofType:@"WAV"]], &alarm); 
AudioServicesPlaySystemSound (alarm); 
+0

감사합니다. 그게 정확히 내가 한 일이고 완벽하게 작동합니다. –