I'm going to start video slow motion using AVPlayer in cocoa application. Example: I can select start position and end position for that video. if i play again that video want to slow motion for start and end position only . Any one please tech me how to implement that. How to select start position and end position and slow motion using AVPLAYER.
3 Answers
It's better to use scaleTimeRange:toDuration:
method of AVMutableComposition
to change speed of tracks.
No more magic with changing AVPlayer
's speed.
Just create another composition (AVMutableComposition
is subclass of AVAsset
), make AVPlayerItem
with it and set it to your AVPlayer
instance.

- 325
- 4
- 12
You'll need to add time observer for the player - addPeriodicTimeObserverForInterval:queue:usingBlock:
. This will be called on regular intervals that you specify. You'll use this to know when the player reaches start or end positions.
For start and end positions you have to store them somewhere in your code and compare them in the block of that method. Take in mind that the observer needs CMTime
and many people are a bit confused about it. Here are some answers:
I personally prefer to work with CMTime
instead of seconds but it's up to you.
Then for the slow motion you just need to set the rate
property of the player to whatever you need - 0.5 will make it twice slower than the original speed, 0.25 will make it four times slower etc. Value of 0 will pause the playback and value of 1 will play it normally. Negative values will do the same with the difference that the playback will go reversed (towards the begging) and values higher than 1 (or -1) will set playback faster than normal.
So... while observing the playback once the currentTime
property of the player (or time
parameter from the block) is between your start and end position you set the rate to the desired one. Then when the currentTime is out of your range set rate back to 1 and the video plays normal speed.
This is, I think, the easiest way. You can also use compositions and then scale time ranges but it's more complicated and if you just play the video and don't need to process it afterwards I think it's better to stick with the simpler solution.
Hope it helps however I'd suggest to read more deeply the docs because AVFoundation is a great framework with almost unlimited possibilities but there are some important things you shouldn't miss when you are dealing with it.
NSObject *time_observer;
CMTime interval = CMTimeMakeWithSeconds(1.0, NSEC_PER_SEC); // 1 second
__unsafe_unretained typeof(self) weakSelf = self;//necessary to calm compiler
time_observer = [video_player addPeriodicTimeObserverForInterval:interval
queue:NULL usingBlock:^(CMTime time) {
int cur_vid_time = CMTimeGetSeconds([weakSelf.video_player currentTime]);
NSLog(@"vid player time: %d",cur_vid_time);
//vid will play in slow-mo
//beginning at the second second (lol) of the video
if(cur_vid_time >= 2)
[weakSelf.video_player setRate:0.0001];
else
[weakSelf.video_player setRate:1.0];
}];

- 550
- 5
- 13