swift ios xcode 如何播放本地容器中的音频声音文件

How to play audio sound file in local container in swift ios xcode

我需要使用 Swift 像

一样从 iOS 中的用户本地文件系统容器播放音频声音文件

file:///Users/User/Library/Developer/CoreSimulator/Devices/EE8A846B-56D9-4B2B-9B52-FCC5CC16B7CA/data/Containers/Data/Application/C057C9A4-77DB-4615-AA78-C0A256ECD2D2/Documents/Dumont.mp3

试试这个

//give your file path here in form of url
let urlstring = "file:///Users/User/Library/Developer/CoreSimulator/Devices/EE8A846B-56D9-4B2B-9B52-FCC5CC16B7CA/data/Containers/Data/Application/C057C9A4-77DB-4615-AA78-C0A256ECD2D2/Documents/Dumont.mp3"
let url = NSURL(string: urlstring)

do 
{
    self.player = try AVAudioPlayer(contentsOfURL: url)
    player.prepareToPlay()
    player.volume = 1.0
    player.play()
} 
catch let error as NSError 
{
    print(error.localizedDescription)
} 
catch {
    print("AVAudioPlayer init failed")
}