Playing a Movie Once

Playing a QTKit movie once is simple. Call the play method.

QTMovie* soundToPlay;
[soundToPlay play];

Playing a Movie Repeatedly

Games like to loop background music to avoid having to supply hours of audio for the game. Looping a movie is a little more complicated than playing it once. It requires two steps: telling QTKit to loop your movie, and using Cocoa’s notification center to notify your program when the movie ends. When the movie ends you tell it to play again.

Telling QTKit to Loop Your Movie

To loop a movie you must tell QTKit to loop it by calling QTKit’s setAttribute method. If you don’t tell QTKit to loop the movie, you will notice a pause when you reach the end of the movie and play it again. You must set the attribute QTMovieLoopsAttribute to the value YES.

QTMovie* soundToPlay;
[soundToPlay setAttribute:[NSNumber numberWithBool:YES] forKey:
	@"QTMovieLoopsAttribute"];

Using the Notification Center

After telling QTKit to loop your movie, you must setup a notification that tells you when the movie stops playing. When you get the notification you tell the movie to play again.

To setup a notification first call the NSNotification class’s defaultCenter method to get access to Cocoa’s notification center.

NSNotificationCenter* notificationCenter = [NSNotificationCenter
        defaultCenter];

After you get the notification center, call NSNotification’s addObserver method. It takes four arguments. The first argument is the observer, which is the object that gets sent the notification. Assuming you have an Objective C class for your sounds, the observer most likely will be self. The second argument is the selector, which is the message the notification center sends to the observer. The message is a method name. You supply the name of the method. The third argument is the name of the notification to look for. The notification QTMovieDidEndNotification tells you when the movie ends.

The final argument is the matching object. If you pass nil for the matching object, all notifications with the notification name you supply are sent to the observer. In the case of looping a movie, passing nil tells the notification center to send a message every time a movie ends. By passing a specific movie as the object, you’re telling the notification center to send a message when that movie stops playing.

The following example tells the notification center to call a method called playAgain when reaching the end of a movie:

[notificationCenter addObserver:self
            selector:@selector(playAgain:)
            name:QTMovieDidEndNotification
            object:self];

The last task to loop movies is to write the method that will be called when a notification occurs. The name of the method must match the selector name you specified when calling addObserver. The method takes a pointer to NSNotification as its argument, but you don’t need to use the argument to loop a movie. The following code shows an example of a playAgain method that tells a movie to play again:

- (void)playAgain:(NSNotification*)note
{
        [self play];
}

Stopping a Movie

Stopping a movie is simple. Call the stop method.

QTMovie* soundToPlay;
[soundToPlay stop];

When you call stop, the sound is paused. If you play the sound again while your application is running, the sound starts playing at the point where you stopped it.

Setting a Movie’s Volume

Call the setVolume method to set the volume for a movie. Supply a floating-point number in the range 0.0 to 1.0, where 1.0 is maximum volume.

QTMovie* soundToPlay;
float desiredVolume;

[soundToPlay setVolume: desiredVolume];

QTKit has the setMuted method that lets you mute a movie. Pass the value true to mute the movie and false to unmute it.

- (void)mute
{
        [soundToPlay setMuted: true];
}

- (void)unmute
{
        [soundToPlay setMuted: false];
}

Conclusion

Included with this article is a simple application for you to download. It uses QTKit to loop an MP3 file. The application comes with full source code that you can use to play audio files in your Cocoa programs.

Previous (Reading the File)