【发布时间】:2011-12-10 13:14:42
【问题描述】:
我正在阅读有关使用 AudioServicesPlaySystemSound 一次播放一种声音的堆栈溢出文章。
我遇到了同样的问题,使用以下代码,当您在另一个按钮停止播放之前按下一个按钮时,第二个声音会在第一个声音的顶部播放,这样两个声音就会重叠。
-(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,因为它会立即播放声音。
请问有其他人遇到过同样的问题吗?
非常感谢您的宝贵时间。
【问题讨论】:
标签: ios iphone objective-c audio overlap