我正在为iOS上的GarageBand构建一个动作扩展,它可以转换和上传音频,但无论我尝试什么,我都无法访问导出的文件。
让我们考虑以下代码 - 它应该:
extensionContext
如果我在Voice Memos.app中运行扩展程序,它会起作用 - 该文件的URL如下所示:file:///tmp/com.apple.VoiceMemos/New%20Recording%202.m4a
现在,如果我在GarageBand.app中运行代码,则url指向(我猜想的)是GarageBand的app容器,因为url看起来像/var/…/Containers/…/Project.band/audio/Project.m4a
,并且音频将无法加载且无法加载因此可以以任何方式操纵。
//编辑:如果我尝试加载音频文件的内容,看起来数据只包含aac标题(?),但文件的其余部分为空
有趣的是:扩展程序呈现react-native
视图,如果我将视图传递给fileUrl(/var/…Project.band/audio/Project.m4a
),然后将其传递给XMLHTTPRequest
,则文件会上传。所以看起来文件可以通过某种方式访问?
我是Swift / iOS开发的新手,所以这对我来说有点令人沮丧,我觉得我几乎尝试过所有事情。
override func viewDidLoad() {
super.viewDidLoad()
var audioFound :Bool = false
for inputItem: Any in self.extensionContext!.inputItems {
let extensionItem = inputItem as! NSExtensionItem
for attachment: Any in extensionItem.attachments! {
print("attachment = \(attachment)")
let itemProvider = attachment as! NSItemProvider
if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeAudio as String) {
itemProvider.loadItem(forTypeIdentifier: kUTTypeAudio as String,
options: nil, completionHandler: { (audioURL, error) in
OperationQueue.main.addOperation {
if let audioURL = audioURL as? NSURL {
print("audioUrl = \(audioURL)")
// in our sample code we just present and play the audio in our app extension
let theAVPlayer :AVPlayer = AVPlayer(url: audioURL as URL)
let theAVPlayerViewController :AVPlayerViewController = AVPlayerViewController()
theAVPlayerViewController.player = theAVPlayer
self.present(theAVPlayerViewController, animated: true) {
theAVPlayerViewController.player!.play()
}
}
}
})
audioFound = true
break
}
}
if (audioFound) {
break
}
}
}