我正在使用Audiotoolbox

.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'文件。

如何解决此问题以在手机上运行?

最佳答案

正如stackmonster所说,确保在Build Phases> Link Binary with Library
您还需要在要使用它的类中将其包含在内(也许也尝试包含audioUnit,但看不到为什么会有)

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

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

theAudio.volume = 1.0;

theAudio.delegate = self;

[theAudio prepareToPlay];

[theAudio play];

10-08 05:36