gpt4 book ai didi

iphone - AVAudioPlayer时间显示

转载 作者:太空狗 更新时间:2023-10-30 03:57:41 28 4
gpt4 key购买 nike

我有一个通过 AVAudioPlayer 播放的简单 mp3,我希望能够显示剩余时间。

我知道答案包括从 AVAudioPlayer.currentTime 中减去 AVAudioPlayer.duration 但我不知道如何实现一个在播放时计算它的函数(比如我猜是 Actionscript 中的 onEnterFrame)。目前 currentTime 是静态的,即零。

最佳答案

我会选择 NSTimer。安排它在播放媒体时每秒运行一次,这样您就可以在剩余时间更新您的 UI。

// Place this where you start to play
NSTimer * myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(updateTimeLeft)
userInfo:nil
repeats:YES];

并创建更新用户界面的方法:

- (void)updateTimeLeft {
NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

// update your UI with timeLeft
self.timeLeftLabel.text = [NSString stringWithFormat:@"%f seconds left", timeLeft];
}

关于iphone - AVAudioPlayer时间显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3571494/

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