xcode 如何在 Swift 应用程序中访问声音文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29809854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access sound files in Swift app
提问by t33j4y
So I am creating an iPhone app that downloads sound files in Swift and I had a question. Using Xcode and the iOS simulator, I'm able to successfully download the audio files that I need onto the simulator. I found that the files being downloaded are in this directory on my OSx machine:
所以我正在创建一个在 Swift 中下载声音文件的 iPhone 应用程序,我有一个问题。使用 Xcode 和 iOS 模拟器,我能够成功地将我需要的音频文件下载到模拟器上。我发现正在下载的文件在我的 OSx 机器上的这个目录中:
OS X -> Users -> "My name" -> Library -> Developer -> CoreSimulator -> Devices -> 96CDD... -> data -> 6B742... -> Documents
OS X -> 用户 -> “我的名字” -> 库 -> 开发者 -> CoreSimulator -> 设备 -> 96CDD... -> 数据 -> 6B742... -> 文档
Now my question is this, I know I have the audio files on the device inside of the app, but how do I access them and then play them from the app itself? I'm trying to just use AVAudioPlayer, but I've been unsuccessful in accessing the file. Here is the code I'm trying to run which is inside of the viewDidLoad function:
现在我的问题是这样的,我知道我在应用程序内的设备上有音频文件,但是我如何访问它们然后从应用程序本身播放它们?我正在尝试只使用 AVAudioPlayer,但我一直没有成功访问该文件。这是我尝试运行的代码,它位于 viewDidLoad 函数内:
var playYoda = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Documents/do_or_do_not", ofType: "wav")!)
println(playYoda)
AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)
var error:NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: playYoda, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()
All of my code compiles, but when I start the simulator, I'm getting this error: fatal error: unexpectedly found nil while unwrapping an Optional valueright at the first line of code that I linked (var playYoda = ...).
我所有的代码都会编译,但是当我启动模拟器时,我收到这个错误:致命错误:在我链接的第一行代码(var playYoda = ...)处解包一个可选值时意外发现 nil。
I don't know whether this is because my path is wrong to the audio file, or if I'm doing something wrong with AVAudioPlayer. Any help would be appreciated.
我不知道这是因为我的音频文件路径错误,还是我在使用 AVAudioPlayer 时做错了。任何帮助,将不胜感激。
采纳答案by Michael Dautermann
How about something like:
怎么样:
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
if let documentDirectoryURL: NSURL = urls.first as? NSURL {
let playYoda = documentDirectoryURL.URLByAppendingPathComponent("do_or_do_not.wav")
println("playYoda is \(playYoda)")
}
回答by nepete
Here is what else you can do:
您还可以执行以下操作:
Open your Xcode project, select your audio files in the project navigator, and hit delete (make sure to move them to trash and not just remove references).
打开您的 Xcode 项目,在项目导航器中选择您的音频文件,然后点击删除(确保将它们移至垃圾箱,而不仅仅是删除引用)。
Then go File > Add files to "Your app".
然后转到“文件”>“将文件添加到“您的应用程序”。
Select your audio files you want to import and make sure that the defaults ("Copy items if needed", "Create groups", and "Add files to target: Your App") are checked.
选择您要导入的音频文件,并确保选中默认设置(“根据需要复制项目”、“创建组”和“将文件添加到目标:您的应用程序”)。
For the file path write:
对于文件路径写入:
var playYoda = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("do_or_do_not", ofType: "wav")!)