我正在尝试使用拖放方法将 .mp3 文件从 finder 拖到我的应用程序中(使用 mac 催化剂)。到目前为止,我已经成功地将(我猜)mp3 文件中的原始数据放入一个数组中,但我不确定如何进行。我最终想要的是使用 AVAudioPlayerNodes 播放拖放到应用程序中的 .mp3 文件。这是正确的方法,还是我应该尝试获取文件的 URL,然后将其复制到我的应用程序?任何帮助都是极好的。谢谢你。
代码:委托方法
func dropInteraction(_ interaction: UIDropInteraction, sessionDidUpdate session: UIDropSession) -> UIDropProposal {
if session.localDragSession == nil {
print("yes")
return UIDropProposal(operation: .copy)
}
print("no")
return UIDropProposal(operation: .cancel)
}
func dropInteraction(_ interaction: UIDropInteraction, performDrop session: UIDropSession) {
print("somethng happened")
session.loadObjects(ofClass: MP3Class.self) { objects in
for url in objects as! [MP3Class] {
print(url)
print(objects)
}
}
}
func dropInteraction(_ interaction: UIDropInteraction, canHandle session: UIDropSession) -> Bool {
return session.canLoadObjects(ofClass: MP3Class.self)
}
func customEnableDropping(on view: UIView, dropInteractionDelegate: UIDropInteractionDelegate) {
let dropInteraction = UIDropInteraction(delegate: dropInteractionDelegate)
view.addInteraction(dropInteraction)
}
自定义 mp3 类:
class MP3Class: NSObject, NSItemProviderReading {
let data: Data?
required init(mp3Data: Data, typeIdentifier: String) {
data = mp3Data
}
static var readableTypeIdentifiersForItemProvider: [String] {
return [kUTTypeMP3 as String]
}
static func object(withItemProviderData data: Data, typeIdentifier: String) throws -> Self {
return self.init(mp3Data: data, typeIdentifier: typeIdentifier)
}
}