0
我需要播放短暂的声音。这是我写的无法播放声音(wav)事件
在我的头我的代码有:
#import <AVFoundation/AVFoundation.h>
@interface AddItemController : UIViewController < ... , AVAudioPlayerDelegate> {
...
AVAudioPlayer *audio;
}
...
@property (nonatomic, retain) IBOutlet AVAudioPlayer *audio;
@end
在.m文件:
- (void)playSound {
NSURL *url;
if (error) {
url =[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Resources/error.wav"]];
} else {
url =[NSURL fileURLWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Resources/finished.wav"]];
}
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
audio.delegate = self;
[audio play];
}
-(IBAction)addItem:(id)sender {
if (...) {
error = true;
[self playSound];
return;
}
...
error = false;
[self playSound];
}
没有警告也没有问题的代码。所有其他功能正在工作。只是我听不到任何声音。
#
好的!我自己找到了解决方案。对不起,如果我困扰你。
希望我的解决方案对别人有用。
- (void)playSound
{
NSURL *url;
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"error.aif"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
url = [NSURL fileURLWithPath:path];
audio = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
[audio prepareToPlay];
}
[audio play];
}
AVSudioPlayer无意以任何方式呈现播放器,您正在考虑使用AVPlayer。 –
Ooops,我的坏...更正了答案,Thanx ...:) – Macmade
Ehm,我的解决方案是对还是错? – Oiproks