我正在使用下面的代码,但是听不到声音。
var audioPlayer:AVAudioPlayer?
override func viewDidLoad() {
super.viewDidLoad()
let path = NSBundle.mainBundle().pathForResource("SecondBeep", ofType: "wav")
let url = NSURL.fileURLWithPath(path!)
do {
try audioPlayer = AVAudioPlayer(contentsOfURL: url)
} catch {
print("Player not available")
}
audioPlayer?.prepareToPlay()
audioPlayer?.play()
}
AVFoundation已正确导入,并且声音文件已添加到项目中。检查了其他stackoverflow主题,但没有帮助。
试图更改为,URLForResource
但也无济于事。
let path = NSBundle.mainBundle().URLForResource("SecondBeep", withExtension: "wav")
let url = NSURL.fileURLWithPath(path!.path!)
我该如何解决该问题?
import UIKit
import AVFoundation
class ViewController: UIViewController {
var audioPlayer:AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
if let myAudioUrl = NSBundle.mainBundle().URLForResource("SecondBeep", withExtension: "wav") {
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: myAudioUrl)
audioPlayer.prepareToPlay()
audioPlayer.play()
} catch let error as NSError {
print(error.localizedDescription)
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句