In Cocoa, you can use the MPMoviePlayerController to play .mov videos. The player controller provides you with standard video controls which are fine until you need to build something that is visually suitable for you application. Fortunately, you can hide the controls and build your own from scratch.
Play / Pause buttons
These buttons are pretty easy to make. As MPMoviePlayerController implements the MPMediaPlayback protocol, you can simply call MPMediaPlayback‘s:
- (void)play
and:
- (void)pauseProgress indicator A progress indicator can be built using a simple UISlider. To calculate its current value, you can use formula:
value = currentPlaybackTime / totalVideoTime;
The value needs to be updated every N milliseconds in a method e.g.:
- (void) monitorPlaybackTime
{
self.progressIndicator.value = self.mpMoviePlayerController.currentPlaybackTime / self.totalVideoTime;
//constantly keep checking if at the end of video:
if (self.totalVideoTime != 0 && videoPlayer.currentPlaybackTime >= totalVideoTime - 0.1)
{
//-------- rewind code:
self.mpMoviePlayerController.currentPlaybackTime = 0;
[self.mpMoviePlayerController pause];
}
else
{
[self performSelector:@selector(monitorPlaybackTime) withObject:nil afterDelay:kVideoPlaybackUpdateTime];
}
}
Apart from constantly updating the indicator, this method also solves a problem that a number of people reported: the video would not play for the second time once it got to the end. To solve this problem, you can constantly keep checking whether video is about to finish, in which case you rewind and pause it (the latter being non-compulsory action to do).
Where do you get the self.totalVideoTime value from? The total video time is not available immediately after the video has been instantiated. Therefore, you need to subscribe for the notification:
... [ video instantiating code ] ....
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(handleDurationAvailableNotification)
name:MPMovieDurationAvailableNotification
object:self.mpMoviePlayerController];
And then implement the handler method to save the video time and start playing the video (this method is usually called about a second or less after the video controller has been instantiated:
- (void) handleDurationAvailableNotification
{
self.totalVideoTime = self.mpMoviePlayerController.duration;
[self.mpMoviePlayerController.currentPlaybackTime = 0;
[self.mpMoviePlayerController play];
}
Implementing a timeline control
We can use the same UISlider to be able to jump through the video. The method tied to the value change of the slider bar is really simple:
- (IBAction) onTimeSliderChange: (UISlider*)sender
{
self.mpMoviePlayerController.currentPlaybackTime = totalVideoTime*timeLineSlider.value;
[self monitorPlaybackTime];
}
Using of your own video controls gives you an absolute control of the playback and the UI. Among other things, you can provide a filtered amount of functionality and add effects that are not possible to do with the standard controls. Creating the controls from scratch is not that lengthy or difficult and shows users of your app that you care a bit more about their perfect experience.


Entries:
Comments:
User: