Swift - 停止 avaudioplayer

Swift - Stop avaudioplayer

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

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()
     }

}

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

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

顺便说一下,您可以将 audioPlayer 标记为“?” 属性,初始化这个Controller时效率会更高

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()
    }
}

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

您不应使用默认初始化程序 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()
     }
if audioPlayer != nil {
    if audioPlayer.playing {
        audioPlayer.stop()
    }
}