我正试图在我的iOS应用程序中使用以下代码来播放我的音频文件,即1.9秒:
NSString *path = [[NSBundle mainBundle] pathForResource:@"bell" ofType:@"wav"];
NSURL *pathURL = [NSURL fileURLWithPath : path];
SystemSoundID audioEffect;
AudioServicesCreateSystemSoundID((__bridge CFURLRef) pathURL, &audioEffect);
AudioServicesPlaySystemSound(audioEffect);
AudioServicesDisposeSystemSoundID(audioEffect);音频文件从不播放,但代码会被调用。

编辑: file bell.wav bell.wav: RIFF (little-endian) data, WAVE audio, Microsoft PCM, 16 bit, stereo 48000 Hz
发布于 2013-12-29 21:33:59
问题是,在声音有机会开始播放之前,你正在处理它。
我的书教你怎么做。下面是我在书中提供的代码:
void SoundFinished (SystemSoundID snd, void* context) {
// NSLog(@"finished!");
AudioServicesRemoveSystemSoundCompletion(snd);
AudioServicesDisposeSystemSoundID(snd);
}
- (IBAction)doButton:(id)sender {
NSURL* sndurl = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"aif"];
SystemSoundID snd;
AudioServicesCreateSystemSoundID((__bridge CFURLRef)sndurl, &snd);
AudioServicesAddSystemSoundCompletion(snd, nil, nil, SoundFinished, nil);
AudioServicesPlaySystemSound(snd);
}你可以调整它来使用你的声音,并以你需要的方式触发你的演奏。
但是,请注意,您不应该使用1.9秒的声音作为系统声音。使用AVAudioPlayer要好得多(我的书也教你这么做)。
https://stackoverflow.com/questions/20830249
复制相似问题