我正在使用 AVAudioPlayer 类播放音频,但问题是当我播放音频时,当文件完成播放并且文件再次启动时,即使循环数设置为 1,我也没有收到 audioPlayerDidFinishPlaying 中的回调
这是代码。
。H
@interface AudioController : NSObject <AVAudioPlayerDelegate>
-(void) playBigGameTheme{
-(void) playMusic:(NSString*) fileName isLooping: (BOOL) isLooping;
@end
.m
@implementation AudioController{
AVAudioPlayer *audioPlayer;
}
-(void) playMusic:(NSString*) fileName isLooping: (BOOL) isLooping{
int loop = 1;
if(isLooping){
loop = -1;
}
[audioPlayer stop];
NSURL* soundUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:fileName ofType:@"mp3"]];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundUrl error:nil];
audioPlayer.numberOfLoops = loop;
[audioPlayer setDelegate:self];
NSLog(@"===Number of loops : %d", audioPlayer.numberOfLoops);
[audioPlayer play];
}
- (void) audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
NSLog(@"HERE");
}
-(void) playBigGameTheme{
[self playMusic:BGAME_MAIN_MUSIC_FILE_NAME isLooping:NO];
}
@end
当我调用 playBigGameTheme 时,音频开始正确播放。但它会在声音完成时重新启动,并且当音频第二次完成时,只会调用 audioPlayerDidFinishPlaying 方法。
我被卡住了,不知道出了什么问题..