我有2个音频文件的变体,第一个嵌入,第二个我从服务器下载 第一个varian工作得很好。 但是当我从服务器获取音频文件时没有播放并获得错误。
我得到路径:
文件:///var/mobile/Containers/Data/Application/24656EA9-6F1F-4C0F-A6FE-5D457908A80D/Documents/Dumont.mp3
尝试玩
func playMusic(filename: String) {
let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "mp3")
guard let newURL = url else {
print("Could not find file: \(filename)")
return
}
do {
player = try AVAudioPlayer(contentsOfURL: newURL)
player!.numberOfLoops = -1
player!.prepareToPlay()
player!.play()
} catch let error as NSError {
print(error.description)
}
}
如何在swift 2.3中播放此音频文件?
平台iOS> 8.,Swift 2.3
答案 0 :(得分:1)
嘿愚蠢的人为你做出了回答:
if let dir = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
let path = NSURL(fileURLWithPath: dir).URLByAppendingPathComponent("Dumont.mp3")
do {
player = try AVAudioPlayer(contentsOfURL: path)
player!.numberOfLoops = -1
player!.prepareToPlay()
player!.play()
} catch let error as NSError {
print(error.description)
}
此代码播放已下载的歌曲!