Qua*_*aso 3 xcode wav avfoundation ios
我正在使用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];
}
Run Code Online (Sandbox Code Playgroud)
这是解决方案;
我在头文件中设置AVAudioPlayer ..
@property(nonatomic,retain)AVAudioPlayer*theAudio;
我猜'保留'解决了我的问题.因为在我发送"播放"之后,它发送"释放"来平衡分配.取消分配AVAudioPlayer后,它将停止播放音频.