avaudioplayer播放进度

时间:2010-02-24 10:20:54

标签: iphone objective-c avaudioplayer volume

是否有人有指向AVAudioPlayer添加播放进度条的教程的链接?

我在这个网站上广泛搜索并在谷歌上无济于事

3 个答案:

答案 0 :(得分:18)

我弄清楚它并没有太糟糕。

只需在计时器中更新

playbackTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
                                     target:self 
                                                 selector:@selector(myMethod:) 
                                   userInfo:nil 
                                    repeats:YES];

}


-(void)myMethod:(NSTimer*)timer {

    float total=audioPlayer.duration;
    float f=audioPlayer.currentTime / total;

    NSString *str = [NSString stringWithFormat:@"%f", f];

    playbackProgress.progress=f;

    NSLog(str);
}

答案 1 :(得分:0)

添加定期时间观察器并更新滑块 - 在Swift中它会是这样的 -

   func addPlayBackSlider()
   {
    playbackSlider = UISlider(frame:CGRect(x:10, y:300, width:300, height:20))
    playbackSlider?.minimumValue = 0
    let duration : CMTime = playerItem.asset.duration
    let seconds : Float64 = CMTimeGetSeconds(duration)
    playbackSlider?.maximumValue = Float(seconds)
    playbackSlider?.isContinuous = true

    //If you want to manually set progressBar
    playbackSlider?.addTarget(self, action: #selector(AddAudioController.playbackSliderValueChanged(_:)), for: .valueChanged)

    //Here is the Observer
    player!.addPeriodicTimeObserver(forInterval: CMTimeMakeWithSeconds(1, 1), queue: DispatchQueue.main) { (CMTime) -> Void in
        if self.player!.currentItem?.status == .readyToPlay {
            let time : Float64 = CMTimeGetSeconds(self.player!.currentTime());
            self.playbackSlider!.value = Float ( time );
        }
    }
   }

   //If you want to manually set progressBar
   func playbackSliderValueChanged(_ playbackSlider:UISlider)
   {
    let seconds : Int64 = Int64(playbackSlider.value)
    targetTime = CMTimeMake(seconds, 1)
    print(targetTime)

    player!.seek(to: targetTime)

    if player!.rate == 0
    {
        player?.play()
        playButton!.setTitle("Pause", for: UIControlState.normal)
    }
}

答案 2 :(得分:0)

CADisplayLink类,它将在屏幕重绘后立即自动调用您定义的方法

Timer不能提供精确的触发,并且可以比请求的更新更早或更晚地漂移,并且也不了解屏幕重绘,因此可以在刚刚发生屏幕重绘后的10毫秒内愉快地触发。

let displayLink = CADisplayLink(target: self,
                                selector: #selector(update))
displayLink.add(to: .current, forMode: .common)


@objc func update() {
    let currentTime = avAudioPlayer.currentTime
    let totalTime = avAudioPlayer.duration
    let progress = currentTime / totalTime
}