Swift 3 多个 AVAudioPlayer

Posted

技术标签:

【中文标题】Swift 3 多个 AVAudioPlayer【英文标题】:Swift 3 multiple AVAudioPlayer 【发布时间】:2017-01-04 12:57:20 【问题描述】:

对 Swift 很陌生,所以请原谅我的问题太天真了。 我构建了一个非常简单的应用程序,其中包含 3 个按钮,每个按钮播放不同的声音。它工作正常,但代码效率不高,因为我为每个要播放的声音创建了一个播放器,所以我想知道如何只使用播放器的一个实例来播放所有声音。这是我的代码:

import UIKit
import AVFoundation

class ViewController: UIViewController 

    var player1 = AVAudioPlayer()
    var player2 = AVAudioPlayer()
    var player3 = AVAudioPlayer()

    @IBAction func playU3(_ sender: Any) 
        player3.play()
    

    @IBAction func playU2(_ sender: Any) 
        player2.play()
    
    @IBAction func playU1(_ sender: Any) 
        player1.play()
    

    override func viewDidLoad() 
        super.viewDidLoad()

        let audioPath3 = Bundle.main.path(forResource: "sound-3", ofType: "mp3")
        let audioPath2 = Bundle.main.path(forResource: "sound-2", ofType: "mp3")
        let audioPath1 = Bundle.main.path(forResource: "sound-1", ofType: "mp3")


        do 

            try player3 = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath3!))
            try player2 = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath2!))
            try player1 = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath1!))


         catch 
            print(error)
        
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    


提前致谢

【问题讨论】:

【参考方案1】:

请检查一下:

import UIKit
import AVFoundation

class ViewController: UIViewController 

    var player : AVAudioPlayer!

    @IBAction func playU3(_ sender: Any) 
        let audioPath = Bundle.main.path(forResource: "sound-3", ofType: "mp3")
        do 

            try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
            player.play()
         catch 
            print(error)
        
    

    @IBAction func playU2(_ sender: Any) 
        let audioPath = Bundle.main.path(forResource: "sound-2", ofType: "mp3")
        do 

            try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
            player.play()
         catch 
            print(error)
        
    
    @IBAction func playU1(_ sender: Any) 
        let audioPath = Bundle.main.path(forResource: "sound-1", ofType: "mp3")
        do 

            try player = AVAudioPlayer(contentsOf: URL(fileURLWithPath: audioPath!))
            player.play()
         catch 
            print(error)
        
    

    override func viewDidLoad() 
        super.viewDidLoad()

    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    


【讨论】:

感谢您的回答。我对其进行了一些修改,所以我在顶部而不是在每个 playU1 函数等内部创建了 3 个 audioPath 变量。【参考方案2】:

试试这个!

import UIKit
import AVFoundation
class ViewControllerDup: UIViewController   
var audioPlayerArray         = [AVAudioPlayer]()

@IBAction func playU3(_ sender: Any) 
   playSound("sound-3.mp3")


@IBAction func playU2(_ sender: Any) 
    playSound("sound-2.mp3")

@IBAction func playU1(_ sender: Any) 
    playSound("sound-1.mp3")


override func viewDidLoad() 
    super.viewDidLoad()



func playSound(_ soundName: String)


    var audioPlayer        = AVAudioPlayer()
    let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: soundName, ofType: nil)!)

    do
        audioPlayer = try AVAudioPlayer(contentsOf: alertSound )
        audioPlayerArray.append(audioPlayer)

        audioPlayer.prepareToPlay()
        audioPlayer.play()
    
    catch
        print("error")
      
    
 override func didReceiveMemoryWarning() 
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
 

【讨论】:

如何将音频 url 传递给 avaudioplayer 只传递音频文件名,如playSound("sound-1.mp3") 不,我已经写了 let url = Foundation.URL(string: audFile)! as URL do player = try AVAudioPlayer(contentsOf: url) player.prepareToPlay() player.play() catch let error as NSError print(error.description) 我想从 URL site4brandz.com/cphp/26/uploads/KaabilHoon.mp3 播放声音,但它给出了 Error Error Domain=NSOSStatusErrorDomain Code=2003334207 "(null)

以上是关于Swift 3 多个 AVAudioPlayer的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 - 在启动时加载多个 ViewController

Swift 3 - 使用多个自定义单元重用单元的问题

在 UICollectionView 中注册多个单元格(swift 3)

使用 Swift 3 Xcode 8 居中对齐多个标签

从firebase检索到swift 3中的多个按钮

Swift 3:如何在表格视图的多个部分中使用操作按钮?