【问题标题】:Playing 1 sound at a time with AudioServicesPlaySystemSound使用 AudioServicesPlaySystemSound 一次播放 1 个声音
【发布时间】:2011-12-10 13:14:42
【问题描述】:

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

stop sound in iPhone

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

-(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


    【解决方案1】:

    试试AudioServicesDisposeSystemSoundID

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多