2

我正在使用音频工具箱

.h 文件

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

@interface ViewController : UIViewController {

}

-(IBAction)buttonPressedWithSound:(id)sender;

@end

并在 .m 文件中

-(IBAction)buttonPressedWithSound:(id)sender {

int randomSoundNumber = arc4random() % 4; //random number from 0 to 3

NSLog(@"random NR = %i", randomSoundNumber);

NSString *effectTitle;

switch (randomSoundNumber) {
    case 0:
        effectTitle = @"Come at me BRO!";
        break;

    default:
        break;
}

SystemSoundID soundID;

NSString *soundPath = [[NSBundle mainBundle] pathForResource:effectTitle ofType:@"mp3"];
NSSound *sound = [[[NSSound alloc] initWithContentsOfFile: soundPath byReference: NO]       autorelease];

[sound play];

AudioServicesCreateSystemSoundID ((CFURLRef)CFBridgingRetain(soundUrl), &soundID);
AudioServicesPlaySystemSound(soundID);
}

但是当我在我的 iphone 上运行时,它出现在 AUGraph.h 中的日志中,其中 #include AudioUnit/AudioUnit.h 和“AudioUnit/AudioUnit.h”文件在它的右侧找不到。

我该如何解决这个问题才能在我的手机上运行它?

4

2 回答 2

1

正如stackmonster所说,确保它在Build Phases>Link Binary with Library中你还需要#include它在你试图使用它的类中(也许也尝试包括audioUnit,但不明白你为什么会有)

您也可以尝试使用 AVAudioPlayer,这对您来说可能更容易(我知道它适合我)

AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];

theAudio.volume = 1.0;

theAudio.delegate = self;

[theAudio prepareToPlay];

[theAudio play];
于 2012-08-10T19:58:33.270 回答
-1

在 Build Phases...链接库中将 audiotoolbox 框架添加到您的应用程序

于 2012-08-10T16:11:40.383 回答