oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Integrating QuickTime with Cocoa
Pages: 1, 2, 3

In most cases, you will use the Interface Builder to create and set up your NSMovieView. The Cocoa Graphic Views palette contains an NSMovieView that you can drag onto the windows of your application.

Figure 2. Cocoa Graphic Views
Figure 2. Cocoa Graphic Views

To create the window in Figure 1, I dragged the NSMovieView from the palette onto my application's window.

Figure 3. Sample Application Window and Inspector
Figure 3. Sample Application Window and Inspector

The Interface Builder Inspector (the right-hand window in Figure 3) is used to set the attributes for your NSMovieView. I wired the necessary outlets and actions and added a small piece of code to initialize the movie, and voila!

Surely There Is More To It Than That!

Instead of going through the various methods for NSMovieView one at a time, I have put together a small example that illustrates a wide variety of NSMovieView's functionality.

Figure 4 illustrates what the final version of the example application will look like.

Figure 4. NSMovie_Example Application
Figure 4. NSMovie_Example Application

So, how do we get there?

I'll assume that you have done some (even a small amount) Cocoa programming and that you are familiar with Apple's Developer Tools — Project Builder (PB) and Interface Builder (IB).

Let's Build Our Own QuickTime Movie Player!

The design of our example will be simple and have the following attributes:

  • Plays a movie file contained in the application bundle.
  • Has a single control (button) used to start and stop movie playback.
  • Has a scrub slider that allows the user to move to any arbitrary time point in the movie.
  • Displays the current playback time for the movie.
  • Has a slider control for the movie's volume.

Here's the step-by-step recipe that I used to make this example. You may wish to download the source code and follow along as we go through the process.

Step One: Create a New Project

Start up Project Builder and select New Project from the File menu. When the Project Builder Assistance displays a list of project types, select "Cocoa Application," then enter the name "NSMovie_Example."

Step Two: Build the User Interface

Double-click the MainMenu.nib file in the Project Builder "Groups & Files" tab and Interface Builder will be started for you. To build my user interface, I have dragged five elements from the IB palette onto my application window: an NSMovieView, an NSSlider (horizontal with tick marks), an NSTextField, an NSButton (borderless), and another NSSlider (horizontal without tick marks).

Next, I used the IB inspector to set the attributes for each element:

  • Set the Application Window to be a "Textured Window".
  • Set the NSMovieView size to 320 by 240 and un-check the "Show Controller" attribute.
  • Borrowing images from another application, I dragged images for "Play" and "Pause" to the Image tab in MainMenu.nib.
  • Set the icon for my Control_Button to the "Play" image, and the alternate icon to the "Pause" image.
  • Set the minimum and maximum values for Volume_Slider to match with the minimum and maximum volumes allowed by QuickTime (0.0 and 1.0, respectively).
  • Set the sizing springs for each UI element.

The result can be seen in Figure 5.

Figure 5. Building the NSMovie_Example user interface
Figure 5. Building the NSMovie_Example user interface
click for larger view

Step Three: Outlets, Actions, and Controllers

The NSMovie_Example application will need a controller to handle the actions initiate by the user interface. From the Interface Builder Classes menu, I created and instantiated an NSObject subclass named NSMovie_Example_Controller.

Figure 6. NSMovie_Example_Controller
Figure 6. NSMovie_Example_Controller

Figure 6 shows the IB inspector after I have created outlets for each of the interface elements. Additionally, I have created four Actions (-Scrub_Movie, -Set_Volume, -Start_Movie, and -Stop_Movie) that will respond to users' actions.

To complete the process, I used the "Create Files..." item on the IB Classes menu to create Objective C source files for my controller and add them to my project.

Pages: 1, 2, 3

Next Pagearrow