用AudioServicesPlaySystemSound一次播放1个声音
问题描述:
我正在阅读关于使用AudioServicesPlaySystemSound一次播放一个声音的堆栈溢出的这篇文章。用AudioServicesPlaySystemSound一次播放1个声音
我描述了同样的问题,用下面的代码,当你的另一停止播放之前按下一个按钮,第二个声音放在了第一位,以使2个声音重叠的顶部播放。
-(IBAction)button1:(id)sender{
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"sound1"
ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}
-(IBAction)button2:(id)sender{
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"sound2"
ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}
按照苹果的documentation:
AudioServicesPlaySystemSound
应该一次只播放一个声音。
此外,当您使用AudioServicesPlaySystemSound
功能:
- 声音在当前系统音量播放,可用无编程音量控制
- 声音播放立即
- 循环和立体声定位不可用
- 同时播放不可用:您一次只能播放一个声音
所以我不明白为什么不止一个声音可以同时播放。
我尝试使用AudioServicesDisposeSystemSoundID
,但它的声音仍然重叠,我做错了吗?
-(IBAction)sound2:(id)sender{
AudioServicesDisposeSystemSoundID
SystemSoundID soundID;
NSString *soundFile = [[NSBundle mainBundle]
pathForResource:@"sound2"
ofType:@"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}
目前,我使用AVAudioPlayer
代替,但有一个良好的开端延迟,我想用AudioServicesPlaySystemSound
,如果可能的话,它会立即播放声音。
请问有其他人有过同样的问题吗?
非常感谢您的时间。
答
试用AudioServicesDisposeSystemSoundID
。
这只是解决您的问题的解决方案。 如果需要再次播放,您必须再次创建声音。