0

I've created an AVPlayer and set the forwardPlaybackEndTime to make a local video stop at a given timestamp. Sure enough, the video stops at the time I've requested. All good.

Now I want the video to continue when triggered by a user action (touching a button, for example). Unfortunately, I can't seem to make that happen without the video restarting from the beginning.

I'll spare you all of the AVPlayer setup code (which is mostly taken from the AV Foundation Programming Guide), but given these variables:

AVPlayer *avPlayer;
AVPlayerItem *playerItem;

I can set the end time like so:

[playerItem setForwardPlaybackEndTime: CMTimeMake(30, 30)];

To attempt the resume, I've tried this:

[playerItem setForwardPlaybackEndTime: CMTimeMake(30, 30)];
[avPlayer setRate: 1.0];

No dice. I've also tried setting the end time and calling play. No luck. I've tried seekToTime to put the playhead at the place where the video stopped in case that would help. It doesn't.

Can someone please explain how to make this work? Thanks!

Axeva
  • 4,697
  • 6
  • 40
  • 64

1 Answers1

2

Try setting the forwardPlaybackEndTime back to the default value, kCMTimeInvalid then continue to play the video.

[playerItem setForwardPlaybackEndTime: kCMTimeInvalid];
[playerItem seekToTime: CMTimeMake(30, 30) toleranceBefore: kCMTimeZero toleranceAfter: kCMTimeZero];
[avPlayer play];
picciano
  • 22,341
  • 9
  • 69
  • 82
  • Doesn't work. That also restarts the video back to the beginning, then runs to the end. – Axeva Oct 06 '14 at 21:57
  • How about the above edited version? Seek to the point you left off, then continue. – picciano Oct 06 '14 at 22:02
  • Still no good. The video always restarts from the beginning. – Axeva Oct 06 '14 at 22:05
  • I may be on to something, thanks to an off-hand comment made by @darvids0n in this answer [Seek to a certain position in AVPlayer right after the asset was loaded](http://stackoverflow.com/a/12135579/271976). He says "It's possible that if you seek to e.g. 15 seconds, it may start from the beginning of the video instead if that is the location of the nearest keyframe. To force the player to seek to the exact location (note: this will take several seconds to do), do this:" – Axeva Oct 06 '14 at 22:15
  • `[playerItem seekToTime: CMTimeMake(30, 30) toleranceBefore: kCMTimeZero toleranceAfter: kCMTimeZero];` Seems to do the trick! – Axeva Oct 06 '14 at 22:16
  • great, i updated the answer based upon that. Hopefully that will help someone else out too. – picciano Oct 07 '14 at 03:41