swift:音频滑块控制

swift:音频滑块控制,第1张

概述问题: 当我转向持有按钮和滑块的视图控制器时,它会崩溃应用程序,并在slider.maximumValue = Float(audioPlayer.duration)行上提供线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x38).在视图控制器中,我有一个播放/暂停按钮,一个停止按钮和一个控制音频播放位置的滑块. 码: import UIKitimport AVFoundation 问题:

当我转向持有按钮和滑块的视图控制器时,它会崩溃应用程序,并在slIDer.maximumValue = float(audioPlayer.duration)行上提供线程1:EXC_BAD_ACCESS(代码= 1,地址= 0x38).在视图控制器中,我有一个播放/暂停按钮,一个停止按钮和一个控制音频播放位置的滑块.

码:

import UIKitimport AVFoundationclass 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 UIKitimport AVFoundationclass 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,selector: Selector("updateTime"),repeats: true)    var timer = NSTimer.scheduledTimerWithTimeInterval(0.01,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:音频滑块控制全部内容,希望文章能够帮你解决swift:音频滑块控制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1008767.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存