如何保存当前录制时间

如何保存当前录制时间

当我按暂停再播放时,音乐再播放怎么解决这个问题?
我知道我需要使用seekTime(: CMTime),但是
Gif
都是密码

class ViewControllerAudioDetail: UIViewController {

@IBOutlet var list: TableViewControllerAudioList!

@IBOutlet weak var playChange: UIButton!
@IBOutlet weak var timeAudio: UILabel!
@IBOutlet weak var playbackSlider: UISlider!


var player = AVPlayer()

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.title = "БИБЛИОТЕКА"
   }

@IBAction func play(_ sender: Any) {
    if player.rate == 0 {

        let url = URL(string: ViewControllerAudioDetail.urlAudio[0])
        let playerItem = AVPlayerItem(url: url!)
        player = AVPlayer(playerItem:playerItem)
        player.rate = 1.0;
        player.play()
        playChange.setImage(UIImage(named:"Pause.png"), for: UIControlState.normal)

        let duration : CMTime = playerItem.asset.duration
        let seconds : Float64 = CMTimeGetSeconds(duration)
        playbackSlider.tintColor = UIColor.green

        let _ = player.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main) { [weak self] (time) in
            self?.playbackSlider.value = Float(CMTimeGetSeconds(time)) / Float(seconds)

       }

    } else {

        playChange.setImage(UIImage(named:"Play.png"), for: UIControlState.normal)
        player.pause()

    }
}


@IBAction func audioPlaybackSlider(_ sender: Any) {


    //перемотка аудиозвука
     let duration : CMTime = player.currentItem!.duration
    let totalDuration : Float64 = CMTimeGetSeconds(duration)

    let value = self.playbackSlider.value
    let durationToSeek = Float(totalDuration) * value
    player.seek(to: CMTimeMakeWithSeconds(Float64(durationToSeek),player.currentItem!.duration.timescale)) { [](state) in
    }

}


}

最佳答案

代码中的问题是,总是在play方法中创建一个新的player对象。这就是为什么它总是从音频开始。
这应该能解决你的问题

class ViewControllerAudioDetail: UIViewController {

    @IBOutlet var list: TableViewControllerAudioList!

    @IBOutlet weak var playChange: UIButton!
    @IBOutlet weak var timeAudio: UILabel!
    @IBOutlet weak var playbackSlider: UISlider!


    var player:AVPlayer?

    override func viewDidLoad() {
        super.viewDidLoad()

        navigationItem.title = "БИБЛИОТЕКА"
        setupAudioPlayer()

    }

    func setupAudioPlayer(){

        let url = URL(string: ViewControllerAudioDetail.urlAudio[0])
        let playerItem = AVPlayerItem(url: url!)
        player = AVPlayer(playerItem:playerItem)
        playbackSlider.tintColor = UIColor.green

        let _ = player!.addPeriodicTimeObserver(forInterval: CMTime(seconds: 1, preferredTimescale: CMTimeScale(NSEC_PER_SEC)), queue: DispatchQueue.main) { [weak self] (time) in
            self?.updateSlider(time: time)
        }

    }

    func updateSlider(time:CMTime){

        let duration = CMTimeGetSeconds(player!.currentItem!.asset.duration)
        self.playbackSlider.value = Float(CMTimeGetSeconds(time)) / Float(duration)

    }

    @IBAction func play(_ sender: Any) {

        if player?.rate == 0 {

            player?.rate = 1.0;
            player?.play()
            playChange.setImage(UIImage(named:"Pause.png"), for: UIControlState.normal)

        } else {

            playChange.setImage(UIImage(named:"Play.png"), for: UIControlState.normal)
            player?.pause()

        }
    }

    @IBAction func audioPlaybackSlider(_ sender: Any) {

        //перемотка аудиозвука
        let duration = CMTimeGetSeconds(player!.currentItem!.asset.duration)
        let value = self.playbackSlider.value
        let durationToSeek = Float(duration) * value

        self.player?.seek(to: CMTimeMakeWithSeconds(Float64(durationToSeek),player!.currentItem!.duration.timescale)) { [](state) in

        }

    }

}

关于ios - AVPlayer,如何保存当前录制时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44364922/

10-10 21:06