gpt4 book ai didi

ios - 再次播放文件时调用AVAudioPlayer audioPlayerDidFinishPlaying

转载 作者:行者123 更新时间:2023-12-01 19:14:41 24 4
gpt4 key购买 nike

我正在使用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方法。

我被困住了,不知道出了什么问题。

最佳答案

even when the number of loops is set to 1



这就是原因。此属性的名称和行为有点违反直觉,但是如果您阅读了文档,则很显然,它设置的是 重复次数,而不是 播放次数。 docs引用:

The value of 0, which is the default, means to play the sound once.



因此,将其设置为0(或不设置,默认值为零),它将只播放一次音频文件:
audioPlayer.numberOfLoops = 0;

关于ios - 再次播放文件时调用AVAudioPlayer audioPlayerDidFinishPlaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14080149/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com