首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVAudioPlayer时间显示

AVAudioPlayer时间显示
EN

Stack Overflow用户
提问于 2010-08-26 09:41:51
回答 2查看 8.7K关注 0票数 4

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

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-08-26 09:57:13

我会去读NSTimer。将其安排为在媒体播放期间每秒运行一次,以便您可以根据剩余时间更新您的UI。

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

并创建用于更新UI的方法:

代码语言:javascript
复制
- (void)updateTimeLeft {
    NSTimeInterval timeLeft = self.player.duration - self.player.currentTime;

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

Stack Overflow用户

发布于 2019-04-12 20:30:51

我在swift中使用它,它起作用了:

代码语言:javascript
复制
// this for show remain time
let duration = Int((player1?.duration - (player1?.currentTime))!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String

//this for current time
let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3571494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档