Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 静音不是在音频文件的末尾添加_Ios_Swift_Avassetexportsession_Avmutablecomposition_Avurlasset - Fatal编程技术网

Ios 静音不是在音频文件的末尾添加

Ios 静音不是在音频文件的末尾添加,ios,swift,avassetexportsession,avmutablecomposition,avurlasset,Ios,Swift,Avassetexportsession,Avmutablecomposition,Avurlasset,我正在做一个演示,演示如何在给定的音频文件末尾添加额外的静音音频 这里我的音频文件长度是29秒我增加了11秒的沉默。 因此,最终输出音频长度为40秒 这是我的功能 func addSilenceInAudio(inputFilePath:URL, silenceTime: Int, minimumAudioLength: Int, completionBlock:@escaping ((String?, Error?) -> Void)) { let asset = AV

我正在做一个演示,演示如何在给定的音频文件末尾添加额外的静音音频

这里我的音频文件长度是29秒我增加了11秒的沉默。 因此,最终输出音频长度为40秒

这是我的功能

func addSilenceInAudio(inputFilePath:URL, silenceTime: Int, minimumAudioLength: Int, completionBlock:@escaping ((String?, Error?) -> Void)) {

        let asset = AVURLAsset(url: inputFilePath, options: nil)

        //get an original audio length

        let endAudioTime = CMTimeMake(value: Int64(silenceTime), timescale: 1)

        let composition = AVMutableComposition()

        let insertAt = CMTimeRange(start: CMTime.zero , end: endAudioTime)
        let assetTimeRange = CMTimeRange(start: CMTime.zero, end:asset.duration)

        //here i'm inserting range
        try! composition.insertTimeRange(assetTimeRange, of: asset, at: insertAt.end)

        let exportSessionNew = AVAssetExportSession(asset: composition, presetName: AVAssetExportPresetAppleM4A)
        exportSessionNew?.outputFileType = AVFileType.m4a
        let documentURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        let dateString = (Date().millisecondsSince1970) //added file name here
        let outputURL = documentURL.appendingPathComponent("\(dateString).m4a") //file name must be .m4a
        exportSessionNew?.outputURL = outputURL //output url

        exportSessionNew?.exportAsynchronously(completionHandler: {
            () -> Void in
            print(exportSessionNew as Any)
            if exportSessionNew!.status == AVAssetExportSession.Status.completed  {
                // All is working fine!!
                print(exportSessionNew?.outputURL as Any) //get outputfile
                print("success")
                completionBlock("\(String(describing: exportSessionNew?.outputURL))", nil)
            } else {
                print("failed")
                completionBlock(nil, exportSessionNew?.error)
            }
        })
    }
以上代码运行良好&我用40秒的时间获得了输出音频

但是问题是在音频文件的开头添加了11秒的静音。

它应该位于音频文件的末尾。


我这里做错了什么?

基本上只需要延长音频的长度

所以

let assetTimeRange = CMTimeRange(start: CMTime.zero, end:asset.duration)
换成

let newDuration = asset.duration + silenceTime
let assetTimeRange = CMTimeRange(start: CMTime.zero, end:newDuration)

免责声明:我已经做了一段时间了

您基本上只需要延长音频的长度

所以

let assetTimeRange = CMTimeRange(start: CMTime.zero, end:asset.duration)
换成

let newDuration = asset.duration + silenceTime
let assetTimeRange = CMTimeRange(start: CMTime.zero, end:newDuration)

免责声明:我已经有一段时间没有这么做了

谢谢,@Scriptable感谢您的回复。我已经尝试过上面的逻辑,但不幸的是上面的逻辑不起作用,它仍然给我同样的结果。谢谢,@Scriptable,谢谢你的回复。我已经尝试过上面的逻辑,但不幸的是上面的逻辑不起作用,它仍然给我同样的结果。