2016-09-23 35 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)") 
    } 
}