0
我的設置如下: 在我的應用程序的這一部分,用戶可以將視頻放在一行中。 這些影片可以讓他們之間的間隙或在稍後的時間爲0AVMutableVideoComposition不會播放
開始爲了實現這一點,我增加了一個功能,每一個縫隙中增加了全黑的視頻循環播放:
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)
}
說明和細分都是無縫的,並在最後填充整個組成。
但是,每次添加黑色視頻片段時,播放都會失敗。玩家只是在黑色視頻到來的時候停下來。
有沒有人看到這個問題?我無法理解它。
非常感謝!
安德烈亞斯