mercredi 3 décembre 2014

AVPlayer playing just 5 seconds of video, audio ok

I have an AVPlayer object that plays videos perfectly fine. But I am trying to make that player to fade in when the video starts and fade out when it ends.


As soon as I add the code to do this, the videos start to play fine, audio and video and after about 5 seconds the video freezes and the audio continues.


This is the code for the fade in, fade out of one second.



// duration is the video duration

CMTime fadeDuration = CMTimeMakeWithSeconds(1.0, duration.timescale);
CMTime fadeOutStartTime = CMTimeSubtract(duration, fadeDuration);

CMTimeRange fadeInRange = CMTimeRangeMake(kCMTimeZero, fadeDuration);
CMTimeRange fadeOutRange = CMTimeRangeMake(fadeOutStartTime, fadeDuration);

AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] firstObject];;
AVMutableVideoCompositionLayerInstruction *instructionLayerFadeInOut = [AVMutableVideoCompositionLayerInstruction videoCompositionLayerInstructionWithAssetTrack:videoTrack];

[instructionLayerFadeInOut setOpacityRampFromStartOpacity:0.0f
toEndOpacity:1.0f
timeRange:fadeInRange];


[instructionLayerFadeInOut setOpacityRampFromStartOpacity:1.0f
toEndOpacity:0.0f
timeRange:fadeOutRange];

AVMutableVideoCompositionInstruction *fadeInOut = [AVMutableVideoCompositionInstruction videoCompositionInstruction];
fadeInOut.timeRange = CMTimeRangeMake(kCMTimeZero, duration);
fadeInOut.layerInstructions = @[instructionLayerFadeInOut];

AVMutableVideoComposition *videoComposition = [AVMutableVideoComposition videoComposition];
videoComposition.instructions = @[fadeIn];
videoComposition.frameDuration = CMTimeMake(1.0, self.frameRate);

CGSize videoSize = [videoTrack naturalSize];
videoComposition.renderSize = videoSize;

[playerItem setVideoComposition:videoComposition];


removing the last line makes the video play fine again.




Aucun commentaire:

Enregistrer un commentaire