iphone - AVAudioPlayer时间显示

标签 iphone objective-c avaudioplayer

我有一个通过 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/

相关文章:

iphone - 无法在 iphone 3G 上使用 AVAssetWriter 从 UIImage 数组制作电影

ios - 在 Objective-C 中使用 for 循环减慢 NSTimer

ios - avplayer 在 ios 4.3 中不工作

ios - Swift - AVAudioPlayer 不工作

ios - AVAudioPlayer 没有从临时文件路径播放声音

iphone - utc 和本地日期时间转换

iphone - 切换icloud帐户时创建永久存储时应用程序崩溃

ios - UIScrollView 的 RTL 支持

iphone - XCode 在 "Run Script"阶段未完成测试构建

iphone - 想要从无线键盘检测 ipad 应用程序中按下的选项卡按钮