Integrating QuickTime with Cocoa
Pages: 1, 2, 3
Step Four: Completing the Project Setup
Returning to Project Builder, the project setup is completed by organizing the source code and resource files.
In addition to the template files that are provided when NSMovie_Example
was created, I have already added files for the button images (Play.tiff
and Pause.tiff) and the controller object (NSMovie_Example_Controller.m
and NSMovie_Example_controller.h). To complete the project, I added
three more files:
- QuickTime.Framework is a Cocoa framework that contains all of the headers and libraries needed to build, link, and execute a QuickTime-enabled application.
- Time_Display_String.m is a C routine, borrowed from an existing
application, that converts a number of seconds into an
NSStringthat represents the movie's time in minutes and seconds. - einsteins_legacy.mov is the movie that
NSMovie_Examplewill play.
Once these were added, I created a couple of new groups within the project, and the result is a "Groups & Files" tab that looks like the following:
|
|
Step Five: Completing the Source Code for NSMovie_Example_Controllers
To finish the coding, I needed to add the logic for the Action methods
in the NSMovie_Example_Controller. In addition to the four Action methods
I had specified in the Interface Builder, I needed to add two more methods
to the controller: -awakeFromNib and -Update_Player_Feedback.
Here's the pseudo-code description for each method:
- (void) awakeFromNib
This method completes the initialization of the controller object. The main tasks are to load the movie einsteins_legacy.mov from the application bundle, make the resulting
NSMovieobject the current movie inMovie_View, initializeTime_Display, and set the parameters ofScrub_Sliderto match the duration of the newly loaded movie.
(IBAction) Scrub_Movie: (id)senderThis method is invoked when someone moves the scrub slider. The main task is to adjust the movie's current time (using
SetMovieTimeValue()) to mirror the value represented by the slider's current position. Additionally,Time_Displaymust be updated to reflect the current movie time.
(IBAction) Set_Volume: (id)senderThis method is invoked when the user moves the volume slider. The main task is to adjust the
Movie_View's volume to reflect the current position ofVolume_Slider.
(IBAction) Start_Movie: (id)senderThis method is invoked after a user clicks the
Control_Button(in its normal state). The main tasks are to start playback in theMovie_Viewand adjust the action of theControl_Buttonso that the next click will stop the movie. This method also creates a repeatingNSTimer(Scrub_Timer) that fires every tenth of a second, sending a message to-Update_Player_Feedback, which in turn animatesScrub_SliderandTime_Display.
(IBAction) Stop_Movie: (id)senderThis method is invoked after a user clicks the
Control_Button(in its alternate state). The main tasks are to stop playback in theMovie_Viewand adjust the action of theControl_Buttonso that the next click will start the movie. This method also stops the repeating time (Scrub_Timer) and resetsScrub_Sliderto its initial position.
(void) Update_Player_Feedback: (NSTimer *) Incoming_TimerThis method is invoked every tenth of second by
Scrub_Timer. The main task is to animateScrub_SliderandTime_Displayto reflect the current playback time ofMovie_View. IfMovie_Viewis done playing, then movie playback is stopped andScrub_SliderandTime_Displayare reset to their initial values.
Step Six: Compile It and Watch My Movie
Special thanks to my friend Wes Vasher. He's responsible for whipping up a short movie for this project.
We've Only Just Begun ...
Hopefully, I've given you enough to get you going on integrating QuickTime into your next Cocoa project.
This QuickTime overview is just the tip of the iceberg. Before you take on your own project, I suggest that you dig deeply into the QuickTime reference materials available at Apple's Developer site. Most notably, check out the QuickTime sample code that uses Cocoa.
The NSMovie_Example project is a place to start, but by no means does
it encompass all the functionality that QuickTime makes available to
you. I would suggest that you take the source code, play with it and
modify it to your own purposes (try adding a movie to your About Box).
That's all for now!
Douglas Welton is the president of Einstein's Legacy that created Cinematics, an innovative new media player that helps you to build and manage playlists of digital movies on your Macintosh.
Return to MacDevCenter.com


