AVAudioPlayer使用数组将音频文件排队-Swift [英] AVAudioPlayer using array to queue audio files - Swift

查看:112
本文介绍了AVAudioPlayer使用数组将音频文件排队-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单地一个接一个地播放音频文件的方法. 使用数组的AVAudioPlayer似乎是最好的解决方案.实际上,我能够使用在此页上找到的Sneak的建议来播放数组的第一个元素:

I am looking for a way to simply play audio files one after another. AVAudioPlayer using an array seems to be the best solution. In fact, I was able to play the first element of the array using Sneak's recommendations found on this page : Here.

但是我不明白在哪里以及如何将第二个调用写入AVAudioPlayer以便播放第二个文件?

But I don't understand where and how to write the second call to AVAudioPlayer in order to play the second file?

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

The "audioPlayerDidFinishPlaying" function is not reacting. Why?

感谢收看.

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"
        }
    }
}

推荐答案

已更改代码...

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

}

这篇关于AVAudioPlayer使用数组将音频文件排队-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆