简体   繁体   English

迅捷3:如何添加UISlider?

[英]swift 3: How to add UISlider?

I have ViewController with AVAudioPlayer . 我有带AVAudioPlayer ViewController Now I have this code 现在我有了这段代码

import UIKit
import AVFoundation

var audioPlayer = AVAudioPlayer()

class ViewController: UIViewController {

@IBOutlet weak var slider: UISlider!
@IBOutlet weak var playButton: UIButton!
var index = 0
var timer: Timer?

override func viewDidLoad() {
    super.viewDidLoad()

    if index == 0{

       let url = Bundle.main.url(forResource: "1", withExtension: "mp3")!

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            audioPlayer.prepareToPlay()
            //audioPlayer.play()
            //play(sender:AnyObject.self as AnyObject)

        } catch let error {
            print(error.localizedDescription)
        }
    }
}

@IBAction func slide(_ slider: UISlider) {
    audioPlayer.currentTime = TimeInterval(slider.value)
}

@IBAction func play(sender: AnyObject) {
    if !audioPlayer.isPlaying{
        audioPlayer.play()
        // **** The line below is the new line ****
        timer = Timer(timeInterval: 1.0, target: self, selector: #selector(self.updateSlider), userInfo: nil, repeats: true)
        RunLoop.main.add(timer!, forMode: .commonModes)
    } else {
        audioPlayer.pause()
        playButton.setImage(UIImage(named: "pause.png"), for: UIControlState.normal)
        timer?.invalidate()
    }
}

func updateSlider(_ timer: Timer) {
    slider.value = Float(audioPlayer.currentTime)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}

I want to add UISlider that change scrub through the audio file. 我想添加UISlider来更改音频文件中的擦洗。 How to do it? 怎么做?

First, you have to add a new IBAction from the slider to make sure your class gets notified when the sliders value changes (Look for "Responding to User Interaction" here: https://developer.apple.com/reference/uikit/uislider#2557863 ) 首先,您必须从滑块添加一个新的IBAction,以确保在滑块值更改时通知您的类(在此处查找“响应用户交互”: https : //developer.apple.com/reference/uikit/uislider #2557863

In this event handler you have to set the audios current time 在此事件处理程序中,您必须设置音频的当前时间

@IBAction func slide(_ slider: UISlider) {
    audioPlayer.currentTime = TimeInterval(slider.value)
}

To make sure the slider updates its value according to the current time, you have to add a timer when you start the player that calls a custom method which updates the sliders value. 为了确保滑块根据当前时间更新其值,您必须在启动播放器时添加计时器,该播放器调用自定义方法来更新滑块值。

@IBAction func play(sender: AnyObject) {
    if !audioPlayer.isPlaying{
        audioPlayer.play()
        // **** The line below is the new line ****
        timer = Timer(timeInterval: 1.0, target: self, selector: #selector(self.updateSlider), userInfo: nil, repeats: true)
        RunLoop.main.add(timer, forMode: .commonModes)
    } else {
        audioPlayer.pause()
        playButton.setImage(UIImage(named: "pause.png"), for: UIControlState.normal)
        timer.invalidate()
    }
}

Afterwards you have to create the method which updates the sliders value 之后,您必须创建更新滑块值的方法

func updateSlider(_ timer: Timer) {
    slider.value = Float(audioPlayer.currentTime)
}

Then you have to set the sliders maximum value 然后,您必须设置滑块的最大值

override func viewDidLoad() {
    super.viewDidLoad()

    if index == 0{

       let url = Bundle.main.url(forResource: "1", withExtension: "mp3")!

        do {
            audioPlayer = try AVAudioPlayer(contentsOf: url)
            audioPlayer.prepareToPlay()
            // **** These two lines below are new ****
            slider.maximumValue = Float(audioPlayer.duration)
            slider.value = 0.0
        } catch let error {
            print(error.localizedDescription)
        }
    }
}

And finally you have to create a property below your IBOutlets which holds the timer 最后,您必须在IBOutlets下创建一个包含计时器的属性

var timer: Timer?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM