使用AVFoundation播放WAV声音文件 [英] Playing wav sound file with AVFoundation

查看:116
本文介绍了使用AVFoundation播放WAV声音文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AVFoundation播放wav文件.但我无法发挥作用.也没有错误或警告出现.

I am using AVFoundation to play wav files. But i could not make it play. Also no errors or warnings showed up.

XCode是4.2,设备是iOS 5.

XCode is 4.2 and device is 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.

I set AVAudioPlayer in header file..

@属性(非原子的,保留)AVAudioPlayer * theAudio;

@property (nonatomic, retain) AVAudioPlayer *theAudio;

我想保留"解决了我的问题.因为在我发送播放"之后,它会发送释放"以平衡分配.释放AVAudioPlayer后,它将停止播放音频.

i guess 'retain' is solved my problem. Because after i sending "play", it sends "release" to balance the alloc. When AVAudioPlayer is deallocated it stops playing audio.

这篇关于使用AVFoundation播放WAV声音文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆