Reputation: 10590
I Suspect Xcode 11 with iOS13 not working Xcode 10.2.1 with iOS12.2 working fine
I trying to merge two audio file into one audio file.
Bellow code working fine Xcode 10.2.1 with iOS 12.2 but now working xcode 11 with iOS13
@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)
})
}
so would you tell me any alternative solution or did i miss anything?.
Upvotes: 1
Views: 1999
Reputation: 6927
I faced the same issue as mentioned by @nazmul.
It's a issue in the Xcode 11.1. Try to run the same code in Xcode 11.2 it will work fine. I have tested the same.
As per release notes of apple for Xcode 11.2 they have resolved this one.
In AVAssetExportSession, the allExportPresets() type method now returns presets in iPhone 11, iPhone 11 Pro, and iPhone 11 Pro Max simulators. (55659811)
Here's the reference link for same. https://developer.apple.com/documentation/xcode_release_notes/xcode_11_2_release_notes
Reference:
Upvotes: 0
Reputation: 23
import AVFoundation
import AVKit
import AssetsLibrary
func getAVMix(audioPath: String, videoPath: String, _ completionHandler: @escaping AVMIXResult) {
let video = Bundle.main.url(forResource: videoPath, withExtension: fileExtension.mp4)! as NSURL
let audio = Bundle.main.url(forResource: audioPath, withExtension: fileExtension.mp3)! as NSURL
//Create the AVmutable composition to add tracks
let composition = AVMutableComposition()
//Create assests url for first video
let video1 = AVURLAsset(url: video as URL, options: nil)
//Create the mutable composition track with video media type. You can also create the tracks depending on your need if you want to merge audio files and other stuffs.
let composedTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: .video, preferredTrackID: kCMPersistentTrackID_Invalid)
//Set the video time ranges of both the videos in composition
if video1.tracks(withMediaType: .video).count > 0 {
do {
try? composedTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, video1.duration), of: video1.tracks (withMediaType: .video)[0], at: kCMTimeZero)
} /*catch let error { print("Error by Developer ", error.localizedDescription) }*/
}
let audioAsset = AVAsset(url: audio as URL)
let audioTrack: AVMutableCompositionTrack? = composition.addMutableTrack(withMediaType: .audio, preferredTrackID: kCMPersistentTrackID_Invalid)
let anIndex = audioAsset.tracks(withMediaType: .audio)[0]
try? audioTrack?.insertTimeRange(CMTimeRangeMake(kCMTimeZero, video1.duration), of: anIndex, at: kCMTimeZero)
// Create a temp path to save the video in the documents dir.
let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(mergeFile)
// Create the export session to merge and save the video
let exporter = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetMediumQuality)
exporter?.outputURL = url
exporter?.outputFileType = AVFileType(exporterFileType)
exporter?.shouldOptimizeForNetworkUse = true
exporter?.exportAsynchronously(completionHandler: {
switch exporter?.status {
case .failed?:
FileManager().clearTmpDirectory()
completionHandler("Merge failed", false) //print("Failed to export video")
case .cancelled?:
FileManager().clearTmpDirectory()
completionHandler("Merge cancelled", false) //print("export cancelled")
case .completed?:
//Here you go you have got the merged video :)
completionHandler(mergeFile, true) //print("Merging completed")
default:
break
}
})
}
Upvotes: 0
Reputation: 36620
Not so much an answer, but it would be difficult to convey all of this as a comment.
Per Apple, there is a function on AVAssetExportSession
you can call named allExportPresets()
that will output all of the available presets.
You can also call exportPresets(compatibleWith:)
with your AVAsset
to find ones compatible with that specific asset.
When I call allExportPresets()
with iOS 12, I get the following output:
["AVAssetExportPreset1920x1080", "AVAssetExportPresetLowQuality", "AVAssetExportPresetAppleM4A", "AVAssetExportPresetHEVCHighestQuality", "AVAssetExportPreset640x480", "AVAssetExportPreset3840x2160", "AVAssetExportPresetHEVC3840x2160", "AVAssetExportPresetHighestQuality", "AVAssetExportPreset1280x720", "AVAssetExportPresetMediumQuality", "AVAssetExportPreset960x540", "AVAssetExportPresetHEVC1920x1080"]
Calling the same function with iOS 13 though, I get:
[]
When I check the support pages for the various presets though, I cannot locate any documentation stating they have been deprecated, etc.
I can attempt to test later to see if I can add to this.
Hope this can at least provide some guidance for now.
Upvotes: 1