我正在编写一个录音应用程序,使用户可以修剪以前的录音部分并将它们连接成一个新的录音。

我的问题是:假设我录制了一个小时的曲目,并且想修剪该曲目的前2分钟。当我导出这2分钟时,此曲目的创建日期将为“现在”,并且我需要它与这2分钟实际发生的日期相匹配。

因此,基本上我正在尝试修改轨道“URL资源值”,但是我只想更改创建日期。

有没有办法做到这一点?还是有办法添加新的资源值键?或将所需日期附加到网址的方法?

func trimStatringPoint(_ from: Date, startOffSet: TimeInterval, duration: TimeInterval, fileName: String, file: URL, completion: fileExportaionBlock?) {

    if let asset = AVURLAsset(url: file) as AVAsset? {
        var trimmedFileUrl = documentsDirectory().appendingPathComponent(fileName)
        let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetAppleM4A)
        exporter?.outputFileType = AVFileTypeAppleM4A
        exporter?.outputURL = trimmedFileUrl
        let start = CMTimeMake(Int64(startOffSet), 1)
        let end = CMTimeMake(Int64(startOffSet + duration), 1)
        exporter?.timeRange = CMTimeRangeFromTimeToTime(start, end)

        exporter?.exportAsynchronously { handler in

            if exporter?.status != AVAssetExportSessionStatus.completed {
                print("Error while exporting \(exporter?.error?.localizedDescription ?? "unknown")")
                completion?(nil)
                return
            }
        }
        //------------------------------------------------------
        // this code needs to be replaced
        do {
                var resourceValus = URLResourceValues()
                resourceValus.creationDate = from
                try trimmedFileUrl.setResourceValues(resourceValus)
        } catch {
            deleteFile(atPath: trimmedFileUrl)
            print("Error while setting date - \(error.localizedDescription)")
            completion?(nil)
            return
        }
        //------------------------------------------------------
        completion?(trimmedFileUrl)
    }

最佳答案

您是否尝试过修改导出记录的元数据?
https://developer.apple.com/documentation/avfoundation/avmetadatacommonkeycreationdate

AVMutableMetadataItem *item = [AVMutableMetadataItem metadataItem];
metaItem.key = AVMetadataCommonKeyCreationDate;
metaItem.keySpace = AVMetadataKeySpaceCommon;
metaItem.value = [NSDate date];
NSArray *metadata = @{ metaItem };
AVAssetExportSession *exportSession = [AVAssetExportSession exportSessionWithAsset:composition presetName:AVAssetExportPresetMediumQuality];
    exportSession.metadata = metadata;

关于ios - 如何更改CreationDate资源值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45793480/

10-11 14:04