简体   繁体   English

静音不会添加到音频文件的末尾

[英]Silence is not adding on end of an audio file

I'm working on a demo of how to add extra silence audio at the end of a given audio file.我正在演示如何在给定音频文件的末尾添加额外的静音音频。

here my audio file length is 29 sec.这里我的音频文件长度是 29 秒。 & I'm adding 11-sec silence. & 我正在添加 11 秒的静音。 so, final output audio length will be 40 sec.因此,最终输出音频长度将为 40 秒。

Here is my function,这是我的功能,

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)
            }
        })
    }

above code is working fine & I'm getting my output audio with 40 sec.上面的代码工作正常,我得到了 40 秒的输出音频。

but the Problem is 11-sec silence is adding in starting of an audio file.问题是在音频文件的开头添加了 11 秒的静音。

it should be at the end of an audio file.它应该在音频文件的末尾。

I'm doing something wrong here?我在这里做错了什么?

You basically only need to extend the length of the audio.你基本上只需要延长音频的长度。

So...所以...

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

change to something like更改为类似的东西

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

Disclaimer: It's a while since I've done this免责声明:我已经有一段时间没有这样做了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM