简体   繁体   English

在iPhone中使用AudioServices播放WAV文件

[英]Playing wav files with AudioServices in iPhone

I am trying to play two .wav files in my appllication, but i am only able to play the first wav file , the second file is not at all playing. 我正在尝试播放两个.wav文件,但是我只能播放第一个wav文件,而第二个文件根本无法播放。

I have added the AudioToolbox framework. 我添加了AudioToolbox框架。 In my .h file i have included the Audiotoolbox.h file and has the following declarations 在我的.h文件中,我包含了Audiotoolbox.h文件,并具有以下声明

//Beep Sound
SystemSoundID   soundFileObject;
SystemSoundID   recycleFileObject;

In my .m file i am registering both the wav files as following 在我的.m文件中,我同时注册了两个wav文件,如下所示

CFURLRef soundFileURLRef = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("Beep"),CFSTR ("wav"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef,&soundFileObject );

CFURLRef soundFileURLRef2 = CFBundleCopyResourceURL (CFBundleGetMainBundle (),CFSTR ("Recycle"),CFSTR ("wav"),NULL );
AudioServicesCreateSystemSoundID (soundFileURLRef2,&recycleFileObject );

And i have written two methods for playing each file individually. 而且我编写了两种方法来分别播放每个文件。

-(void)playBeepSound
{
    NSLog(@"Beep sound called : %d", soundFileObject);
    AudioServicesPlaySystemSound (soundFileObject);
}


-(void)playRecycleSound
{
    NSLog(@"Recycle sound called : %d", recycleFileObject);
    AudioServicesPlaySystemSound (recycleFileObject);
}

But i am only able to play the Beep sound , but not the second sound wav file. 但是我只能播放Beep声音,但不能播放第二个声音wav文件。

What is the error ??? 有什么错误?

Thanks 谢谢

If I were you I would wrap the Audio Toolbox code in a class so that you can factor out common code and play sounds with more comfort ( example code ). 如果您是我,我会将Audio Toolbox代码包装在一个类中,以便您可以分解出通用代码并以更舒适的方式播放声音( 示例代码 )。 This could also solve your problem, as it seems to be a small typo somewhere – the sample code you gave us looks fine. 这也可以解决您的问题,因为这似乎是一个小错字-您提供给我们的示例代码看起来不错。 (Did you try to log everything? No nulls, everything gets called as expected? Are you trying to play both sounds at once, or is there a delay between them? Does it matter which sound runs first?) (您是否尝试记录所有内容?没有空值,一切都按预期方式调用了吗?您是要同时播放两种声音还是它们之间存在延迟?首先播放哪种声音有关系吗?)

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

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