我正在尝试将声音添加到我正在编写的iOS应用中。我一直收到错误消息
“调用中的参数标签不正确(包含'contentsOfURL:错误:',预期”contentsOfURL:fileTypeHint:')“。
我试过用几种方法修补它,我无法建立它。这是我的代码:
import UIKit import AVFoundation
class ViewController: UIViewController {
var Setup = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("DMSetup", ofType: "mp3")!)
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
audioPlayer = AVAudioPlayer(contentsOfURL: Setup, error: nil)
audioPlayer.prepareToPlay()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func PlayKeySetup(sender: UIButton) {
audioPlayer.play()
}
}
有什么建议吗?我是这里的新手,所以我肯定我错过了一些明显的东西。 xCode 7.3.1,OSX 10.11.4
感谢。
答案 0 :(得分:0)
通常在属性名称中使用大写字母,它被视为不良态度,您应该使用setup
试试这段代码:
let setupUrl = NSBundle.mainBundle().URLForResource("DMSetup", withExtension: "mp3")
if (setupUrl == nil) {
print("Could not find file: DMSetup.mp3")
return
}
do { audioPlayer = try AVAudioPlayer(contentsOfURL: Setup!, fileTypeHint: nil) }
catch let error as NSError { print(error.description) }
if let player = audioPlayer {
player.prepareToPlay()
}
答案 1 :(得分:0)
试试这段代码:
var audioPlayer: AVAudioPlayer!
let path = NSBundle.mainBundle().pathForResource("yourfilename", ofType: "format")
let CREATE_ANY_VARIABLE = NSURL(fileURLWithPath: path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: CREATE_ANY_VARIABLE)
} catch let error as NSError {
print(error.debugDescription)
}
audioPlayer.prepareToPlay()
然后你就可以在这个功能的任何地方玩! 代码中的问题是var的名称与AVFoundation的另一个Setup方法重叠