2016-09-23 36 views
0

當我試圖按按鈕時添加音頻,但我發現了以下錯誤斯威夫特3:使用AVAudioPlayer

「不匹配任何重載音頻」錯誤「的說法標籤contentsOfURL,錯誤不匹配任何重載音頻」

var ButtonAudioURL = NSURL(fileURLWithPath: Bundle.main.path(forResource: "Slag", ofType: "m4a")!) 
var clickAudioPlayer = AVAudioPlayer() 

class UIViewController: UIViewController { 
clickAudioPlayer = AVAudioPlayer(contensOfURL: ButtonAudioURL, error: nil) //This is in this line i get the error 

} 
@IBAction func ASButton(_ sender: UIButton) { 
} 

錯誤發生在

clickAudioPlayer = AVAudioPlayer(contensOfURL: ButtonAudioURL, error: nil) 
+0

這裏呢?這仍然開放?我有一個運行音頻的代碼 –

回答

0

Swift 3.0,編譯成功,這裏是我的代碼:

import UIKit 
import AVFoundation 

class ViewController: UIViewController { 

    var audioPlayer: AVAudioPlayer? 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    let audioURL = URL(fileURLWithPath: Bundle.main.path(forResource: "test", ofType: "mp3")!) 
    audioPlayer = try? AVAudioPlayer(contentsOf: audioURL) 

    print("test: \(audioURL), \(audioPlayer)") 
    } 
}