2017-04-02 53 views
0

我有2個音頻文件的變體,第一個嵌入,第二個是我從服務器上下載的。
第一變異是工作良好。 但是,當我從服務器獲取音頻文件不播放,並獲得錯誤。如何播放下載的音頻文件swift

我得到的路徑:

文件:///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,雨燕2.3

回答

1

嘿愚蠢的傢伙誰downvote爲你解答:

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) 
     } 

此代碼播放下載的歌曲!