gpt4 book ai didi

swift:音频 slider 控件

转载 作者:搜寻专家 更新时间:2023-10-31 08:18:15 25 4
gpt4 key购买 nike

问题:

当我转到包含按钮和 slider 的 View Controller 时,它会使应用程序崩溃并在 slider 行上给我 Thread 1:EXC_BAD_ACCESS (code=1, address=0x38) .maximumValue = Float(audioPlayer.duration)。在 View Controller 中,我有一个播放/暂停按钮、一个停止按钮和一个控制音频播放位置的 slider 。

代码:

import UIKit
import AVFoundation

class AgnesAudioViewController: UIViewController {

var audioPlayer = AVAudioPlayer()
var toggleState = 1
@IBOutlet var slider: UISlider!

@IBAction func done(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
}

@IBAction func play(sender: AnyObject) {
audioPlayer.play()
}

@IBAction func pause(sender: AnyObject) {
audioPlayer.pause()
}

@IBAction func stop(sender: AnyObject) {
audioPlayer.stop()
}

@IBAction func scrubAudio(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = NSTimeInterval(slider.value)
audioPlayer.prepareToPlay()
audioPlayer.play()
}

@IBAction func playPauseButton(sender: AnyObject) {
var playBtn = sender as! UIButton
if toggleState == 1 {
audioPlayer.play()
toggleState = 2
playBtn.setImage(UIImage(named:"pause.png"),forState:UIControlState.Normal)
} else {
audioPlayer.pause()
toggleState = 1
playBtn.setImage(UIImage(named:"play.png"),forState:UIControlState.Normal)
}
}

override func viewDidLoad() {
super.viewDidLoad()

var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true)

slider.maximumValue = Float(audioPlayer.duration)

let pathString = NSBundle.mainBundle().pathForResource("agnes", ofType: "mp2")

if let pathString = pathString {

let pathURL = NSURL(fileURLWithPath: pathString)

do {

try audioPlayer = AVAudioPlayer(contentsOfURL: pathURL)

} catch {

print("error")
}


}

}

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

最佳答案

这解决了问题:

import UIKit
import AVFoundation

class AgnesAudioViewController: UIViewController {

var audioPlayer = AVAudioPlayer()
var toggleState = 1
@IBOutlet var slider: UISlider!
@IBOutlet var playedTime: UILabel!

@IBAction func done(sender: AnyObject) {
dismissViewControllerAnimated(true, completion: nil)
audioPlayer.stop()
}

@IBAction func play(sender: AnyObject) {
audioPlayer.play()
updateTime()
}

@IBAction func pause(sender: AnyObject) {
audioPlayer.pause()
updateTime()
}

@IBAction func stop(sender: AnyObject) {
audioPlayer.stop()
updateTime()
}

@IBAction func scrubAudio(sender: AnyObject) {
audioPlayer.stop()
audioPlayer.currentTime = NSTimeInterval(slider.value)
audioPlayer.prepareToPlay()
audioPlayer.play()
}

func updateTime() {
var currentTime = Int(audioPlayer.currentTime)
var duration = Int(audioPlayer.duration)
var total = currentTime - duration
var totalString = String(total)

var minutes = currentTime/60
var seconds = currentTime - minutes / 60

playedTime.text = NSString(format: "%02d:%02d", minutes,seconds) as String
}

@IBAction func playPauseButton(sender: AnyObject) {
//1 = play
//2 = pausw
var playBtn = sender as! UIButton
if toggleState == 1 {
audioPlayer.play()
toggleState = 2
playBtn.setImage(UIImage(named:"pause2.png"),forState:UIControlState.Normal)
} else {
audioPlayer.pause()
toggleState = 1
playBtn.setImage(UIImage(named:"play2.png"),forState:UIControlState.Normal)
}
}

override func viewDidLoad() {
super.viewDidLoad()

var updateTimer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateTime"), userInfo: nil, repeats: true)
var timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: Selector("updateSlider"), userInfo: nil, repeats: true)

let pathString = NSBundle.mainBundle().pathForResource("agnes", ofType: "mp3")

if let pathString = pathString {

let pathURL = NSURL(fileURLWithPath: pathString)

do {

try audioPlayer = AVAudioPlayer(contentsOfURL: pathURL)

} catch {

print("error")
}


}

slider.maximumValue = Float(audioPlayer.duration)
}

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

关于swift:音频 slider 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34971317/

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