AVAudioPlayer使用数组来排队音频文件--Swift

时间:2017-07-23 23:14:29

标签: arrays swift macos audio avaudioplayer

我正在寻找一种简单地播放音频文件的方法。 使用数组的AVAudioPlayer似乎是最好的解决方案。事实上,我可以使用此页面上的Sneak推荐来播放数组的第一个元素:Here

但是我不明白在第二次调用AVAudioPlayer的地方和方式,以便播放第二个文件?

" audioPlayerDidFinishPlaying"功能没有反应。为什么呢?

感谢收看。

import Cocoa
import AVFoundation

var action = AVAudioPlayer()
let path = Bundle.main.path(forResource: "test.aif", ofType:nil)!
let url = URL(fileURLWithPath: path)
let path2 = Bundle.main.path(forResource: "test2.aif", ofType:nil)!
let url2 = URL(fileURLWithPath: path2)
let array1 = NSMutableArray(array: [url, url2])

class ViewController: NSViewController
{
    @IBOutlet weak var LanceStop: NSButton!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        do
        {
            action = try AVAudioPlayer(contentsOf: array1[0] as! URL)
            action.numberOfLoops = 0
            action.prepareToPlay()
            action.volume = 1
        }catch{print("error")}
    }
...
@IBAction func Lancer(_ sender: NSButton)
    {
        if action.isPlaying == true
        {
            action.stop()
            action.currentTime = 0.0
            LanceStop.title = "Lancer"
        }
        else
        {
            action.play()
            LanceStop.title = "Stopper"
        }
    }

    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
    {
        if flag == true
        {
            LanceStop.title = "Lancer"
        }
    }
}

2 个答案:

答案 0 :(得分:1)

  

但我不明白在哪里以及如何写第二次致电   AVAudioPlayer为了播放第二个文件?

因此,为了播放第二个文件,您需要编写一个方法,您需要初始化audioplayer并在audioplayer委托方法audioPlayerDidFinishPlaying中调用相同的方法,如下所示: -

func playAudioFile(_ index: Int) {
  do
{
  action = try AVAudioPlayer(contentsOf: array1[index] as! URL)
  action.numberOfLoops = 0
  action.prepareToPlay()
  action.volume = 1
 } catch{print("error")
}

 func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool) {
     //This delegate method will called once it finished playing audio file.
    //Here you can invoke your second file. You can declare counter variable 
   //and increment it based on your file and stop playing your file acordingly.
  counter = counter + 1
  playAudioFile(counter)
}
  

注意: - 将Audioplayer委托设置为ViewController以进行调用   audioPlayerDidFinishPlaying方法就像这样。

action.delegate = self

答案 1 :(得分:0)

更改了代码......

class ViewController: NSViewController, AVAudioPlayerDelegate
{

    @IBOutlet weak var LanceStop: NSButton!

    override func viewDidLoad()
    {
        super.viewDidLoad()
    }
    override var representedObject: Any?
    {
        didSet
        {
        // Update the view, if already loaded.
        }
    }

    func playAudioFile(_ index: Int)
    {
        do
        {
            action = try AVAudioPlayer(contentsOf: array1[index] as! URL)
            action.delegate = self
            action.numberOfLoops = 0
            action.prepareToPlay()
            action.volume = 1
            action.play()
        }
        catch{print("error")}
    }

    @IBAction func Lancer(_ sender: NSButton)
    {
      playAudioFile(0)
    }


    func audioPlayerDidFinishPlaying(_ player: AVAudioPlayer, successfully flag: Bool)
    {
        if flag == true
        {
            playAudioFile(1)
        }
    }

}