本文介绍了我该怎么做来调用audioPlayerDidFinishPlaying:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了这个源程序.但是我不能调用 audioPlayerDidFinishPlaying:方法.播放声音后,几秒钟后由于"exc_bad_access"错误而崩溃.
I wrote this source program . But I can't call audioPlayerDidFinishPlaying: method.After playing the sound, which crashes by "exc_bad_access" error after a few seconds.
.h文件
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface SecondViewController : UIViewController<
AVAudioPlayerDelegate>{
AVAudioPlayer *aPlayer;
}
@end
.m文件
-(void)playSound{
NSString *soundName = @"red";
NSError *error = nil;
NSURL *soundUrl = [[NSBundle mainBundle] URLForResource:soundName withExtension:@"mp3"];
aPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:&error];
if (error != nil) {
NSLog(@"audio_player initialized error :(%@)",[error localizedDescription]);
[aPlayer release];
error=nil;
return;
}
NSLog(@"player Ok!");
aPlayer.delegate = self;
[aPlayer prepareToPlay];
aPlayer.volume=1.0f;
[aPlayer play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
[player release];
}
推荐答案
这是我使用的效果很好的方法,它应该会对您有所帮助.
This is what I use that works perfectly, it should help you.
-(void)playSound{
NSString *name = [[NSString alloc] initWithFormat:@"red"];
NSString *source = [[NSBundle mainBundle] pathForResource:name ofType:@"mp3"];
if (data) {
[data stop];
data = nil;
}
data=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath: source] error:NULL];
data.delegate = self;
[data play];
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)data successfully:(BOOL)flag{
NSLog(@"my log");
[data release];
}
这篇关于我该怎么做来调用audioPlayerDidFinishPlaying:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!