Playing the File

To play an audio file, call the function StartMovie() and supply the movie you want to play.

Movie soundToPlay; 
StartMovie(soundToPlay);

One line of code is all it takes to play any movie, even movies with video. If you want to play a movie with video, you must provide a destination for QuickTime to show the video. Interface Builder provides movie views to help those of you interested in playing video. I leave playing video as an exercise for you.

A lot of games like to loop sounds (play them repeatedly) to provide continuous background music. To loop a sound you must call the function IsMovieDone() to see if the movie finished playing. If IsMovieDone() returns a value of true, call the functions GoToBeginningOfMovie() and StartMovie() to play the sound again.

Movie soundToPlay; 

if (IsMovieDone(soundToPlay)) { 
	GoToBeginningOfMovie(soundToPlay); 
	StartMovie(soundToPlay);
}

Pausing Playback

When you want to pause a sound you’re playing, call the function StopMovie() and supply the movie you want to pause. Call StartMovie() to resume playing the movie.

Movie soundToPlay; 
StopMovie(soundToPlay);

Giving QuickTime CPU Time

When you play an audio file with QuickTime, you must periodically give QuickTime CPU time to service the movie it’s playing. Servicing the movie keeps it playing smoothly. Call the function MoviesTask() to give QuickTime CPU time. This function takes two arguments. The first argument is the movie you’re playing. The second argument is the amount of time (in milliseconds) to give QuickTime. If you supply a value of 0, QuickTime services every movie once. Unless you’re playing a lot of movies, you can normally get away with supplying a value of 0.

Movie soundToPlay; 
MoviesTask(soundToPlay, 0);

You should call MoviesTask() as often as you can to keep the movie playing smoothly. In the application that accompanies this article, I use an event timer. The timer calls a function, where I call MoviesTask(). Each time the timer fires, MoviesTask() gets called, giving QuickTime enough time to keep the movie playing.

Setting a Sound’s Volume

To set a sound’s volume, call the function SetMovieVolume(). This function takes two arguments. The first argument is the movie whose volume you want to set. The second argument is the volume.

Movie soundToPlay; 
short volume;

SetMovieVolume(soundToPlay, volume);

The range of acceptable volumes is –256 to 256. Negative and zero volumes do not play the sound. What is the point of negative volumes? Negative volumes preserve the volume’s absolute value, which makes them useful for muting sounds. Multiply the current volume by –1 to mute the sound, and multiply by –1 a second time to restore the sound’s volume.

Movie soundToPlay; 
short currentVolume = GetCurrentVolume(soundToPlay);

SetMovieVolume(currentVolume * -1);

Conclusion

Included with this article is an application for you to download. It loops an MP3 file using QuickTime. It comes with full source code you can use to play audio files in your programs. If you have any questions or comments about this article, send them to mark-AT-meandmark-DOT-com.

Previous (Loading the audio file)