[英]Playing wav sound file with AVFoundation
我正在使用AVFoundation播放wav文件。 但我無法發揮它。 也沒有出現任何錯誤或警告。
XCode是4.2,設備是iOS 5。
- (IBAction) playSelectedAlarm:(id)sender {
UIButton *button = (UIButton *)sender;
int bTag = button.tag;
NSString *fileName = [NSString stringWithFormat:@"%d23333", bTag];
NSLog(@"%@", fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
AVAudioPlayer *theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
theAudio.volume = 1.0;
theAudio.delegate = self;
[theAudio prepareToPlay];
[theAudio play];
}
這是解決方案;
我在頭文件中設置AVAudioPlayer ..
@property(nonatomic,retain)AVAudioPlayer * theAudio;
我猜'保留'解決了我的問題。 因為在我發送“播放”之后,它發送“釋放”來平衡分配。 取消分配AVAudioPlayer后,它將停止播放音頻。
我在Xcode 4.5上遇到了同樣的問題。 使AVAudioPlayer成為一個強類型屬性使它發揮作用。
因此,以下代碼需要添加到@interface:
@property (nonatomic, strong) AVAudioPlayer *theAudio;
@implementation將成為:
- (IBAction) playSelectedAlarm:(id)sender {
UIButton *button = (UIButton *)sender;
int bTag = button.tag;
NSString *fileName = [NSString stringWithFormat:@"%d23333", bTag];
NSLog(@"%@", fileName);
NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"wav"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: path];
self.theAudio=[[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:NULL];
self.theAudio.volume = 1.0;
self.theAudio.delegate = self;
[self.theAudio prepareToPlay];
[self.theAudio play];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.