gpt4 book ai didi

ios - iOS 中检查音频是否已结束播放

转载 作者:行者123 更新时间:2023-11-29 04:03:47 25 4
gpt4 key购买 nike

我想检查音频是否已结束播放。如果是这样,我需要将按钮更改为播放按钮而不是暂停按钮。我如何在 iOS 中做到这一点?

我的代码如下所示:

-(void)playAudioFile:(BOOL)status
{
NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"audio" ofType:@"mp3"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];


NSError *error;

if(status)
{

if(!audioPlayer)
{
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:&error];
audioPlayer.currentTime = currentPlayTime;
audioPlayer.numberOfLoops = 0; //Infinite
audioDuration = audioPlayer.duration;

audioPlaySlider.minimumValue = 0;
audioPlaySlider.maximumValue = audioDuration;
audioPlaySlider.value = currentPlayTime;

[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(audioTimer:) userInfo:[NSDictionary dictionaryWithObject:audioPlayer forKey:@"playerObject"] repeats:YES];
}

[audioPlayer play];

if([audioPlayer rate] != 1.0 )
{
[audioPlayer play];
}
else
{
[self->audioPlayBtn setBackgroundImage:[UIImage imageNamed:@"playBtn.png"] forState:UIControlStateNormal];
}


NSLog(@"Play duration : %f",audioDuration);
}
else
{
if(audioPlayer && audioPlayer.playing)
{
[audioPlayer pause];
}
}

NSLog(@"Error for file : %@",error);

}

我尝试过这个:

            if([audioPlayer rate] != 1.0 )
{
[audioPlayer play];
}
else
{
[self->audioPlayBtn setBackgroundImage:[UIImage imageNamed:@"playBtn.png"] forState:UIControlStateNormal];
}

但不起作用..需要一些指导...

最佳答案

使用AVAudioPlayerDelegate的方法

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

带有音频播放器实例的 View Controller /类必须是音频播放器的委托(delegate)。当音频播放器完成播放时将调用该方法。

http://developer.apple.com/library/ios/#documentation/AVFoundation/Reference/AVAudioPlayerDelegateProtocolReference/Reference/Reference.html

关于ios - iOS 中检查音频是否已结束播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15503441/

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