我怀疑带有iOS13的Xcode 11无法与带有iOS12.2的Xcode 10.2.1正常工作
我试图将两个音频文件合并为一个音频文件。
下面的代码可以在iOS 12.2上正常运行Xcode 10.2.1,但现在可以在iOS13上正常运行xcode 11
@objc func applyMergeAudio(){
playmerge(audio1: Bundle.main.url(forResource: "audio0", withExtension: "mp3")!,audio2: Bundle.main.url(forResource: "Asteroid_Sound", withExtension: "mp3")!)
}
func playmerge(audio1: URL, audio2: URL)
{
let composition = AVMutableComposition()
let compositionAudioTrack1:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())!
let compositionAudioTrack2:AVMutableCompositionTrack = composition.addMutableTrack(withMediaType: AVMediaType.audio, preferredTrackID: CMPersistentTrackID())!
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileDestinationUrl = documentDirectoryURL.appendingPathComponent("resultmerge.m4a")
let filemanager = FileManager.default
if (!filemanager.fileExists(atPath: fileDestinationUrl.path))
{
do
{
try filemanager.removeItem(at: fileDestinationUrl)
}
catch let error as NSError
{
NSLog("Error: \(error)")
}
}
let avAsset1 = AVURLAsset(url: audio1, options: nil)
let avAsset2 = AVURLAsset(url: audio2, options: nil)
var tracks1 = avAsset1.tracks(withMediaType: AVMediaType.audio)
var tracks2 = avAsset2.tracks(withMediaType: AVMediaType.audio)
let assetTrack1:AVAssetTrack = tracks1[0]
let assetTrack2:AVAssetTrack = tracks2[0]
let duration1: CMTime = assetTrack1.timeRange.duration
let duration2: CMTime = assetTrack2.timeRange.duration
let timeRange1 = CMTimeRangeMake(start: CMTime.zero, duration: duration1)
let timeRange2 = CMTimeRangeMake(start: CMTime.zero, duration: duration2)
do
{
try compositionAudioTrack1.insertTimeRange(timeRange1, of: assetTrack1, at: CMTime.zero)
try compositionAudioTrack2.insertTimeRange(timeRange2, of: assetTrack2, at: CMTime.zero)
}
catch
{
print(error)
}
//below line force unwarp Version xcode 10.2.1 working fine with iOS12.2. but xcode 11 with iOS13 got crush . .
let assetExport = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
assetExport?.outputFileType = AVFileType.m4a
assetExport?.outputURL = fileDestinationUrl
assetExport?.exportAsynchronously(completionHandler:
{
//xcode 11 with iOS13 not fire this block but xcode 10.2.1 with iOS12.2 working fine.
print(fileDestinationUrl)
})
}
所以您能告诉我任何替代解决方案,或者我错过了什么吗?。
不是太多的答案,但是很难将所有这些作为评论来传达。
[Per Apple,AVAssetExportSession
上有一个函数,您可以调用名为allExportPresets()
的函数,该函数将输出所有可用的预设。
您也可以用exportPresets(compatibleWith:)
呼叫AVAsset
以查找与该特定资产兼容的资产。
当我用iOS 12]呼叫allExportPresets()
时,得到以下输出:
[“” AVAssetExportPreset1920x1080“,” AVAssetExportPresetLowQuality“,“ AVAssetExportPresetAppleM4A”,“ AVAssetExportPresetHEVCHighestQuality”,“ AVAssetExportPreset640x480”,“ AVAssetExportPreset3840x2160”,“ AVAssetExportPresetHEVC3840x2160”,“ AVAssetExportPresetHighestQuality”,“ AVAssetExportPreset1280x720”,“ AVAssetExportPresetMediumQuality”,“ AVAssetExportPreset960x540”,“ AVAssetExportPresetHEVC1920x1080”]
虽然通过iOS 13]调用相同的函数,但得到:
[]
但是,当我在支持页面上查看各种预设时,找不到任何说明已弃用的文档,等等。>>
我稍后可以尝试进行测试,看看是否可以添加到此。
希望现在至少可以提供一些指导。