中間鴿了兩天烤芦,產(chǎn)品又給了新的需求扛吞,獲取本地音樂(lè)并添加到視頻中去。08的時(shí)候已經(jīng)獲取到本地音樂(lè)了专酗,但是發(fā)現(xiàn)本地音樂(lè)的assetURL路徑添加到視頻的時(shí)候出現(xiàn)問(wèn)題,需要把它轉(zhuǎn)為本地路徑盗扇。網(wǎng)上查到AVAssetExportSessionexport方法祷肯,由于是OC的沉填,這里把它轉(zhuǎn)為swift.
代碼如下:
let everything = MPMediaQuery()
let itemsFromGenericQuery = everything.items
for song in itemsFromGenericQuery! {
let songTitle = song.value(forProperty: MPMediaItemPropertyTitle) as! String
let url = song.value(forProperty: MPMediaItemPropertyAssetURL)
let songAsset = AVURLAsset.init(url: url as! URL)
let fileManager = FileManager.default
let dirs = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentsDirectoryPath = dirs.first
let exporter = AVAssetExportSession.init(asset: songAsset, presetName: AVAssetExportPresetAppleM4A)
exporter?.outputFileType = "com.apple.m4a-audio"
let exportFile = documentsDirectoryPath?.appending(songTitle + ".m4a")
if fileManager.fileExists(atPath: exportFile!) {
try? fileManager.removeItem(at: URL.init(string: exportFile!)!)
}
let exportURL = URL.init(fileURLWithPath: exportFile!)
exporter?.outputURL = exportURL
exporter?.exportAsynchronously(completionHandler: {
print(exporter!.status)
}
print(path!)
print(exportURL)
}
最后可以查看本地音樂(lè)的assetURL和本地路徑
本地音樂(lè).png