Skip to content

Commit

Permalink
set speed, frame/position when beeing ready to play
Browse files Browse the repository at this point in the history
  • Loading branch information
i-n-g-o committed Feb 8, 2019
1 parent 8db8e45 commit b206ddd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libs/openFrameworks/video/ofAVFoundationVideoPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ typedef enum _playerLoopType{
BOOL bSampleAudio; // default to NO
BOOL bIsUnloaded;
BOOL bStream;
int frameBeforeReady;
float positionBeforeReady;

NSLock* asyncLock;
NSCondition* deallocCond;
Expand Down
34 changes: 30 additions & 4 deletions libs/openFrameworks/video/ofAVFoundationVideoPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ - (id)init {
bSeeking = NO;
bSampleVideo = YES;
bIsUnloaded = NO;
frameBeforeReady = 0;
positionBeforeReady = 0.F;

// do not sample audio by default
// we are lacking interfaces for audiodata
Expand Down Expand Up @@ -728,12 +730,29 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

bReady = true;

[self setVolume:volume]; // set volume for current video.
// set volume for current video
[self setVolume:volume];

// set speed for current video
[self setSpeed:speed];

// set start-frame
if (frameBeforeReady > 0) {
[self setFrame:frameBeforeReady];
}

// set start-position
if (positionBeforeReady > 0.F) {
[self setPosition:positionBeforeReady];
}

// auto-play or play if started before beeing ready
if(bAutoPlayOnLoad || bPlayStateBeforeLoad) {
[self play];
}

[self update]; // update as soon is ready so pixels are loaded.
// update as soon is ready so pixels are loaded.
[self update];


} else if ([self.playerItem status] == AVPlayerItemStatusUnknown) {
Expand Down Expand Up @@ -1323,13 +1342,19 @@ - (void)setPosition:(float)position {
if ([self isReady]) {
double time = [self getDurationInSec] * position;
[self seekToTime:CMTimeMakeWithSeconds(time, NSEC_PER_SEC)];
} else {
positionBeforeReady = position;
frameBeforeReady = 0;
}
}

- (void)setFrame:(int)frame {
if ([self isReady]) {
float position = frame / (float)[self getDurationInFrames];
[self setPosition:position];
} else {
frameBeforeReady = frame;
positionBeforeReady = 0.F;
}
}

Expand Down Expand Up @@ -1366,6 +1391,8 @@ - (playerLoopType)getLoop {

- (void)setSpeed:(float)value {

speed = value;

if(![self isReady]) {
return;
}
Expand Down Expand Up @@ -1406,8 +1433,7 @@ - (void)setSpeed:(float)value {
if (value < 0.0) {
bWasPlayingBackwards = YES;
}

speed = value;

[_player setRate:value];
}

Expand Down

0 comments on commit b206ddd

Please sign in to comment.