开发者

Can't hear System Sound in simulator or on device

开发者 https://www.devze.com 2023-01-11 02:25 出处:网络
I can\'t seem to get the AudioServices sounds to play, either on the device or in the simulator. Here is my code:

I can't seem to get the AudioServices sounds to play, either on the device or in the simulator. Here is my code:

NSString *sndPath 开发者_Python百科= [[NSBundle mainBundle] pathForResource:@"click" ofType:@"aiff"];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:sndPath], &soundID);
AudioServicesPlaySystemSound(soundID);
AudioServicesDisposeSystemSoundID(soundID);

I have checked my system prefs, and yes I do have "Play user interface sound effects" ticked on... What might I be doing wrong?

Thanks!

EDIT: I'm attempting to create an audio click as the user moves touches across the device, so I need it to rapidly play the same sound. Should I use AVAudioPlayer instead?


Don't dispose the sound until after it finishes playing. You can do something like this:

AudioServicesAddSystemSoundCompletion(soundID, NULL, NULL, (AudioServicesSystemSoundCompletionProc)AudioServicesDisposeSystemSoundID, NULL);

Slightly icky, since we're assuming that it doesn't use the Pascal calling convention (i.e. AudioServicesDisposeSystemSoundID() can handle the extra NULL argument).

0

精彩评论

暂无评论...
验证码 换一张
取 消