我的设置如下: 在我的应用程序的这一部分中,用户可以将视频连续放置。 这些视频可能在它们之间存在差距,或者在稍后的时间开始为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)
}
说明和细分都是无缝的,最后填写整个构图。
但是,每次添加黑色视频片段时,播放都会失败。当时播放器只是停在那里,黑色视频来了。
有人看到了这个问题吗?我无法理解它。
非常感谢!
安德烈亚斯
答案 0 :(得分:0)
我的问题的答案很简单:只需更新到iOS 9.3 苹果显然目前有一个破碎的框架。 还有一些案例,它并没有完美地运作,但它没有问题。 我想它将在9.4中修复
再见! 安德烈亚斯