gpt4 book ai didi

ios - AVPlayer 与 UISlider Swift 的进展

转载 作者:行者123 更新时间:2023-11-30 12:53:24 25 4
gpt4 key购买 nike

我正在尝试使用 slider 来控制音频,一切正常,但是当我尝试使 slider 值等于播放器当前时间时,它会崩溃。
但是,如果我在 updateSlider 函数中打印一些内容,它就会出现并正常工作。

override func viewDidLoad() 
{
songTitle.text = mainSongTitle
background.image = image
mainImageView.image = image
downloadFileFromURL(url: URL(string: previewURL)!)
var time = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(self.updateSlider), userInfo: nil, repeats: true)
}

func downloadFileFromURL(url : URL)
{
var downloadTask = URLSessionDownloadTask()
downloadTask = URLSession.shared.downloadTask(with: url, completionHandler:
{
customURL , response , error in
self.play(url: customURL!)
self.slider.maximumValue = Float(player.duration)
})
downloadTask.resume()
}

func play(url : URL)
{
do
{
player = try AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
player.play()
}
catch
{
print(error)
}
}

@IBAction func PlayPressed(_ sender: Any)
{
if player.isPlaying
{
player.pause()
}
else
{
player.play()
}
}

@IBAction func ChangerTimePlayer(_ sender: Any)
{
player.stop()
player.currentTime = TimeInterval(slider.value)
player.prepareToPlay()
player.play()
}

func updateSlider()
{
slider.value = Float(player.currentTime)
}

最佳答案

也许你应该这样做:

func updateSlider(){
slider.value = Float(player.currentTime/player.duration)
}

关于ios - AVPlayer 与 UISlider Swift 的进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40695019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com