Swift - 停止avaudioplayer

时间:2016-07-16 09:47:48

标签: ios swift avfoundation avaudioplayer

我正在尝试将音板构建到应用程序中,并找到了一种使用标签来控制播放声音的有效方法。但是我现在正在尝试集成一个暂停按钮,该按钮可以与.stop()上的AVAudioPlayer方法一起使用但是我的当前代码出错:

EXC_BAD_ACCESS

这就是我现在使用的,任何想法?

import UIKit
import AVFoundation

let soundFilenames = ["sound","sound2","sound3"]
var audioPlayers = [AVAudioPlayer]()

class SecondViewController: UIViewController {

     var audioPlayer = AVAudioPlayer()

    override func viewDidLoad() {
        super.viewDidLoad()

     for sound in soundFilenames {
          do {
               let url = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource(sound, ofType: "mp3")!)
               let audioPlayer = try AVAudioPlayer(contentsOfURL: url)
               audioPlayers.append(audioPlayer)
          } catch {
               //Catch error thrown
               audioPlayers.append(AVAudioPlayer())
          }
     }
     }


     @IBAction func buttonPressed(sender: UIButton) {
          let audioPlayer = audioPlayers[sender.tag]
          audioPlayer.play()
     }
     @IBAction func stop(sender: UIButton) {
          audioPlayer.stop()
     }

}

3 个答案:

答案 0 :(得分:2)

停止功能中的audioPlayer不是播放播放器。您应该在buttonPressed函数中分配它。

@IBAction func buttonPressed(sender: UIButton) {
      audioPlayer = audioPlayers[sender.tag]
      audioPlayer.play()
 }

顺便说一句,您可以将audioPlayer标记为"?"属性,初始化此控制器时效率会更高。

class SecondViewController: UIViewController {

     var audioPlayer: AVAudioPlayer?

     let enableMuiltPlayers = false
....


    @IBAction func buttonPressed(sender: UIButton) {
          if sender.tag < audioPlayers.count else {
               print("out of range")
               return
          }
          if enableMuiltPlayers {
             audioPlayers[sender.tag].play()
          } else {
             audioPlayer?.stop()
             //set the current playing player
             audioPlayer = audioPlayers[sender.tag]
             audioPlayer?.play()
          }
     }

     @IBAction func stop(sender: UIButton) {
          let wantToStopAll = false
          if enableMuiltPlayers  && wantToStopAll {
              stopAll()
          } else {
              audioPlayer?.stop()
          }
          audioPlayer = nil
     }
}

停止所有:

fun stopAll() {
    for player in audioPlayers {
        player.stop()
    }
}

答案 1 :(得分:2)

您的代码可能有其他错误,但有一点可以肯定:

您不应使用默认初始值设定项AVAudioPlayer来实例化AVAudioPlayer()

更改此行:

     var audioPlayer = AVAudioPlayer()

为:

     var playingAudioPlayer: AVAudioPlayer?

更改此部分:

          } catch {
               //Catch error thrown
               audioPlayers.append(AVAudioPlayer())
          }

这样的事情:

          } catch {
               //Catch error thrown
               fatalError("Sound resource: \(sound) could not be found")
          }

(后一部分对于解决这个问题非常重要。但是我发现在编辑之后它已经变成了郝答案的某些部分的重复......)

start方法:

     @IBAction func start(sender: UIButton) {
          let audioPlayer = audioPlayers[sender.tag]
          audioPlayer.start()
          playingAudioPlayer = audioPlayer
     }

stop应该是:

     @IBAction func start(sender: UIButton) {
          playingAudioPlayer?.stop()
     }

答案 2 :(得分:0)

if audioPlayer != nil {
    if audioPlayer.playing {
        audioPlayer.stop()
    }
}