我正在尝试使用AudioToolBox框架点击按钮播放一个wav文件。没有抛出任何错误,但声音没有播放。
-(IBAction)buttonPressed
{
NSString *path;
NSURL *url;
AVAudioPlayer *player;
NSLog(@"Hai");
path = [[NSBundle mainBundle] pathForResource:@"sound" ofType:@"wav"];
url=[NSURL fileURLWithPath:path];
player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:NULL];
[player setVolume:5.0];
[player play];
}
还试过这段代码
NSString *path = [NSString stringWithFormat:@"%@%@", [[NSBundle mainBundle] resourcePath], @"censor-beep-01.wav"];
SystemSoundID soundID;
NSURL *filePath = [NSURL fileURLWithPath:path isDirectory:NO];
//Use audio sevices to create the sound
AudioServicesCreateSystemSoundID((__bridge CFURLRef)filePath, &soundID);
//Use audio services to play the sound
AudioServicesPlaySystemSound(soundID);
答案 0 :(得分:0)
在头文件中设置AVAudioPlayer框架..并声明AVAudioPlayer *播放器;在.h文件中
@property(非原子,保留)AVAudioPlayer *播放器; 在按钮动作AVAudioPlayer *播放器中合成并删除此行; 你应该在.h文件中声明。