gpt4 book ai didi

ios - 检查avaudioplayer的当前播放时间

转载 作者:可可西里 更新时间:2023-11-01 04:27:53 25 4
gpt4 key购买 nike

这是检查 AVAudioPlayer 的 当前播放时间的正确方法吗?

[audioPlayer play];

float seconds = audioPlayer.currentTime;

float seconds = CMTimeGetSeconds(duration);

之后我如何编码

  [audioPlayer play]; 

当 currentTime 等于 11 秒时

[self performSelector:@selector(firstview) withObject:nil]; 

当 currentTime 等于 23 秒时 firstview 之后

[self performSelector:@selector(secondview) withObject:nil];

最佳答案

您可以设置一个 NSTimer 来定期检查时间。你需要这样的方法:

- (void) checkPlaybackTime:(NSTimer *)theTimer
{
float seconds = audioPlayer.currentTime;
if (seconds => 10.5 && seconds < 11.5) {
// do something
} else if (seconds >= 22.5 && seconds < 23.5) {
// do something else
}
}

然后设置 NSTimer 对象以每秒(或您喜欢的任何时间间隔)调用此方法:

NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 
target:self
selector:@selector(checkPlaybackTime:)
userInfo:nil
repeats:YES];

查看 NSTimer 文档以获取更多详细信息。当你用完它时,你确实需要在正确的时间停止(使)计时器:

https://developer.apple.com/documentation/foundation/nstimer

编辑:seconds 可能不会正好是 11 或 23;您将不得不调整粒度。

关于ios - 检查avaudioplayer的当前播放时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9525357/

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