代码之家  ›  专栏  ›  技术社区  ›  daihovey

AVAudioPlayer时间显示

  •  4
  • daihovey  · 技术社区  · 15 年前

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

    我知道答案包括减法 AVAudioPlayer.duration AVAudioPlayer.currentTime 但我不知道如何实现一个函数,在它运行时计算它(我猜像是Actionscript中的onEnterFrame)。目前 currentTime

    1 回复  |  直到 15 年前
        1
  •  12
  •   vfn    15 年前

    我想找个时间。安排它在播放媒体时每秒钟运行一次,这样您就可以随时更新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];
    }
    
        2
  •  2
  •   Masoud Roosta    7 年前

    // 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