OGeek|极客世界-中国程序员成长平台

标题: ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 18:29
标题: ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音

我正在阅读有关使用 AudioServicesPlaySystemSound 一次播放一种声音的堆栈溢出文章。

stop sound in iPhone

我遇到了同样的问题,使用以下代码,当您在另一个按钮停止播放之前按下一个按钮时,第二个声音会在第一个声音的顶部播放,这样两个声音就会重叠。

-(IBAction)button1id)sender{
SystemSoundID soundID;   
NSString *soundFile = [[NSBundle mainBundle]    
                       pathForResource"sound1"
                       ofType"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile],   &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
} 

-(IBAction)button2id)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)sound2id)sender{
AudioServicesDisposeSystemSoundID
SystemSoundID soundID;   
NSString *soundFile = [[NSBundle mainBundle]    
                       pathForResource"sound2"
                       ofType"wav"];
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath:soundFile], &soundID);
AudioServicesPlaySystemSound(soundID);
[soundFile release];
}

目前,我正在使用 AVAudioPlayer,但声音开始延迟,如果可能,我想使用 AudioServicesPlaySystemSound,因为它会立即播放声音。

请问有其他人遇到过同样的问题吗?

非常感谢您的宝贵时间。



Best Answer-推荐答案


尝试使用 AudioServicesDisposeSystemSoundID

这只是解决您问题的解决方案。 如果需要再次播放,则必须重新创建声音。

关于ios - 使用 AudioServicesPlaySystemSound 一次播放 1 个声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8456731/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4