gpt4 book ai didi

ios - AVMutableVideoComposition 无法播放

转载 作者:行者123 更新时间:2023-11-30 13:35:23 24 4
gpt4 key购买 nike

我的设置如下:在我的应用程序的这一部分中,用户可以将视频排成一行。这些视频之间可能有间隙,或者稍后从 0 开始。

为了实现这一目标,我添加了一个功能,该功能添加全黑视频并在每个间隙期间循环播放:

func getBlackVideoSegments(timerange: CMTimeRange) -> [AVCompositionTrackSegment] {
var compositionTrackSegments = [AVCompositionTrackSegment]()
if CMTimeCompare(timerange.duration, RTGlobals.blackVideo.duration) == -1 {
let compositionTrackSegment = AVCompositionTrackSegment(URL: RTGlobals.blackVideoURL, trackID: RTGlobals.blackVideoAssetTrack.trackID, sourceTimeRange: CMTimeRangeMake(kCMTimeZero, timerange.duration), targetTimeRange: timerange)
compositionTrackSegments.append(compositionTrackSegment)
addVideoCompositionInstruction(RTGlobals.blackVideoAssetTrack, timeRange: timerange, assetTrack: videoCompositionTrack)
} else {
var currentStart = timerange.start
var filledDuration = kCMTimeZero
while CMTimeCompare(filledDuration, timerange.duration) == -1 {
let durationToFill = CMTimeSubtract(timerange.duration, filledDuration)
var durationAdded = RTGlobals.blackVideo.duration
if CMTimeCompare(durationAdded, durationToFill) == 1 {
durationAdded = durationToFill
}

let compositionTrackSegment = AVCompositionTrackSegment(URL: RTGlobals.blackVideoURL, trackID: RTGlobals.blackVideoAssetTrack.trackID, sourceTimeRange: CMTimeRangeMake(kCMTimeZero, durationAdded), targetTimeRange: CMTimeRangeMake(currentStart, durationAdded))
compositionTrackSegments.append(compositionTrackSegment)
addVideoCompositionInstruction(RTGlobals.blackVideoAssetTrack, timeRange: CMTimeRangeMake(currentStart, durationAdded), assetTrack: videoCompositionTrack)

currentStart = CMTimeAdd(currentStart, durationAdded)
filledDuration = CMTimeAdd(filledDuration, durationAdded)
}
}
return compositionTrackSegments
}

这是我设置指令的方法:

func addVideoCompositionInstruction(videoAssetTrack: AVAssetTrack, timeRange: CMTimeRange, assetTrack: AVAssetTrack) {
let instruction = AVMutableVideoCompositionInstruction()
instruction.timeRange = timeRange
let layerInstruction = AVMutableVideoCompositionLayerInstruction(assetTrack: assetTrack)

let videoSize = CGSizeMake(1920.0, 1080.0)

var transform = videoAssetTrack.preferredTransform
var scale:CGFloat = 1.0
var offset:CGFloat = 0.0

if fabs(videoAssetTrack.preferredTransform.b) == 1.0 && fabs(videoAssetTrack.preferredTransform.c) == 1.0 {
scale = videoSize.height / videoAssetTrack.naturalSize.width
offset = (videoSize.width - (videoAssetTrack.naturalSize.height * scale)) / 2.0
} else {
scale = videoSize.height / videoAssetTrack.naturalSize.height
}
transform = CGAffineTransformConcat(CGAffineTransformConcat(transform, CGAffineTransformMakeScale(scale, scale)), CGAffineTransformMakeTranslation(offset, 0))

layerInstruction.setTransform(transform, atTime: timeRange.start)

instruction.layerInstructions = [layerInstruction]

instructions.append(instruction)
}

说明和片段都是无缝的,并在最后填充整个构图。

但是,每次我添加这些黑色视频片段时,播放都会失败。播放器只是在出现黑色视频时停止。

有人看出问题了吗?我就是无法理解它。

非常感谢!

安德烈亚斯

最佳答案

我的问题的答案很简单:只需更新到 iOS 9.3苹果目前的框架显然已经被打破。仍然存在一些情况,它不能完美地工作,但没关系。我猜它会在 9.4 中修复

再见!安德烈亚斯

关于ios - AVMutableVideoComposition 无法播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36134049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com