简体   繁体   English

iPhone SDK:声音问题

[英]Iphone SDK: sound problems

i've created a drum app. 我已经创建了一个鼓应用程序。 As of now, the app's kinda satisfying but not great. 截至目前,该应用程序还算令人满意,但还不是很好。

The problem is a strange delay or "lag" that sometimes happens when i press my sound buttons. 问题是我按下声音按钮时有时会发生奇怪的延迟或“滞后”。 When i spam a button it begins to freeze, when unfreezed again, it plays the same sound stacked on itself about 4 times :( 当我向按钮发送垃圾邮件时,它开始冻结,再次被冻结时,它播放相同的声音,其自身堆积了大约4次:(

What am i doing wrong? 我究竟做错了什么?

Are my samples bad? 我的样品不好吗?

Should i use an imageView and touchesBegan to simulate the buttons? 我应该使用imageView和touchesBegan模拟按钮吗?

This is the code that i use to play a sound when a button is pushed: 这是按下按钮时用来播放声音的代码:

- (IBAction) HHC {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"HHClosed"ofType:@"wav"];

    SystemSoundID soundID;

    AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);

    AudioServicesPlaySystemSound (soundID);

}

Please give me some good advice :) 请给我一些好的建议:)

Thanks in advance 提前致谢

-DD -DD

Every time you hit the button you are loading the sound file from the filesystem, bringing it into memory, and then playing it. 每次您按下按钮时,您都在从文件系统中加载声音文件,将其放入内存,然后播放。

This is very inefficient. 这是非常低效的。 Your audio samples should already be loaded in memory at launch, so they don't need to be read in from the filesystem. 您的音频样本应该在启动时已经加载到内存中,因此不需要从文件系统中读取它们。 Also, AudioServicesPlaySystemSound isn't really designed for this kind of usage - its main purpose is playing back short alert sounds (ie, a system alert). 另外, AudioServicesPlaySystemSound并不是真正为这种使用而设计的-它的主要目的是播放短警报声音(即系统警报)。 If you need low-latency audio playback you should be looking at the Core Audio framework instead. 如果您需要低延迟音频播放,则应改用Core Audio框架。

Try doing this and see if it makes your performance better. 尝试这样做,看看是否可以改善您的性能。

In your .h file declare this: 在您的.h文件中声明:

SystemSoundID soundID;

In your .m file in the -viewDidLoad method add this code: 在您的.m文件的-viewDidLoad方法中,添加以下代码:

  -(void) viewDidLoad {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"HHClosed"ofType:@"wav"];
    AudioServicesCreateSystemSoundID((CFURLRef) [NSURL fileURLWithPath:path], &soundID);
}

And in the method you showed just keep this one line of code: 在显示的方法中,只需保留以下一行代码:

- (IBAction) HHC {
    AudioServicesPlaySystemSound (soundID);
}

Let me know if that makes the freezing going away. 让我知道这是否会使冻结消失。 If it doesn't try Core Audio 如果没有尝试Core Audio

For low latency sound appropriate for a drum app, you will want to use the RemoteIO Audio Unit API, which is not simple. 对于适合鼓应用程序的低延迟声音,您将需要使用RemoteIO Audio Unit API,这并不简单。 You will need to know how to handle, buffer and mix PCM sound samples. 您将需要知道如何处理,缓冲和混合PCM声音样本。 Start by reading a book on digital audio technology. 首先阅读有关数字音频技术的书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM