在我的 Xcode 项目中,我设置了以下代码(简化代码):
import Cocoa
import AVKit
class ViewController: NSViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
do {
guard let filePath = Bundle.main.path(forResource: "sample", ofType: "mp3") else {
print("ERROR: Failed to retrieve music file Path")
return
}
let fileURL = URL.init(fileURLWithPath: filePath)
print(" PATH: \(filePath)")
print(" URL: \(fileURL)")
try audioPlayer = AVAudioPlayer.init(contentsOf: fileURL)
} catch {
print("ERROR: Failed to retrieve music file URL")
}
audioPlayer.play()
}
}
//CONSOLE PRINTS ----------:
// PATH: /Users/vakho/Library/Developer/Xcode/DerivedData/MusicPlayer-foqzsckozmcjnofvlvhuwabfssqi/Build/Products/Debug/MusicPlayer.app/Contents/Resources/sample.mp3
// URL: file:///Users/vakho/Library/Developer/Xcode/DerivedData/MusicPlayer-foqzsckozmcjnofvlvhuwabfssqi/Build/Products/Debug/MusicPlayer.app/Contents/Resources/sample.mp3
通过首先转换为 URL,我成功地将sample.mp3(包含在应用程序包中)的文件路径传递给audioPlayer。调用play()函数播放音频文件。
但是,由于我正在创建一个音乐播放器应用程序,我希望能够播放驻留在目录文件夹中的音频文件,例如桌面、下载文件夹等......但是当我尝试传递应用程序包外部音频文件的文件路径,代码中断:
import Cocoa
import AVKit
class ViewController: NSViewController {
var audioPlayer = AVAudioPlayer()
override func viewDidLoad() {
super.viewDidLoad()
do {
let filePathOptional: String? = "/Users/vakho/Downloads/sample.mp3"
guard let filePath = filePathOptional else {
print("ERROR: Failed to retrieve music file Path")
return
}
let fileURL = URL.init(fileURLWithPath: filePath)
print(" PATH: \(filePath)")
print(" URL: \(fileURL)")
try audioPlayer = AVAudioPlayer.init(contentsOf: fileURL)
} catch {
print("ERROR: Failed to retrieve music file URL")
}
audioPlayer.play()
}
}
//CONSOLE PRINTS ----------:
// PATH: /Users/vakho/Downloads/sample.mp3
// URL: file:///Users/vakho/Downloads/sample.mp3
//ERROR: Failed to retrieve music file URL
//ERRORS ----------:
// (lldb)
// Thread 1: EXC_BAD_ACCESS (code=1, address=0x38)
据我所知,AVAudioPlayer 和 AVKit 库旨在访问应用程序 Assets 和捆绑媒体文件以及来自互联网的流媒体。但无法播放目录中的媒体。
我发现了几个有关该问题的线索,但全部不完整或未得到答复,例如:https://forums.developer.apple.com/thread/18272
所以,如果 AVKit 不能做我认为它应该做的事情,我可以使用什么库或方法来访问目录文件? OSX 的音乐播放器应用程序当然能够提示用户扫描目录并访问音乐文件。他们是如何做到的?
最佳答案
看来问题就在这里:
guard let filePath = filePathOptional else {
print("ERROR: Failed to retrieve music file Path")
return
}
请将打印更改为:
print("ERROR: Failed to retrieve music file Path \(error.localizedDescription)")
我想您会发现问题是您无权访问该文件。 默认情况下,您只能访问应用程序和桌面文件夹的沙箱。
另请尝试将文件放入桌面并播放。
关于Swift AVAudioPlayer (Xcode) - 在应用程序包外部播放音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56540569/