以下是我的代碼 我無法播放SnareHitSample.wav文件。 我不知道我在哪裏出錯。如何播放Wav文件
-(void)playAudioFiles
{
NSString * path;
AVAudioPlayer * snd;
NSError * err;
NSString *name;
name = @"SnareHitSample.wav";
path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
NSURL * url = [NSURL fileURLWithPath:path];
snd = [[AVAudioPlayer alloc] initWithContentsOfURL:url
error:&err] ;
if (! snd) {
NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
} else {
[snd prepareToPlay];
}
} else {
NSLog(@"Sound file '%@' doesn't exist at '%@'", name, path);
}
}
當audioPlayer/theAudio超出範圍時,它將被取消分配,然後聲音播放將停止。像你一樣向類中添加一個屬性,它將在對象的生命週期中保持不變。 – 2013-09-25 15:03:20