Playing Audio Files with QuickTime

Author: Mark Szymczyk
Last Update: September 6, 2005

Games like to provide background music to enhance the gameplay experience. On Mac OS X the easiest way to play background music is to store the music in audio files and use QuickTime to play the files. This article shows you how to use QuickTime to play audio files.

Introduction

One of QuickTime’s major functions is playing movies. The term movie makes you think of video, but QuickTime movies aren’t limited to video files. Audio files can be movies as well. QuickTime can play MP3, AAC (The format the iTunes Music Store uses), AIFF, and Windows WAV files. All you have to do is write code to play a movie, and you’ll be able to play any audio file that QuickTime supports.

This article focuses on using QuickTime for Carbon applications on Mac OS X. I assume you have a set of audio files you want to play that you will be including with your application. Some programs may want to use an Open File dialog box to let the user pick a file to play. Implementing Open File dialog boxes is beyond the scope of this article.

Cocoa applications should use the QTKit framework to play QuickTime movies. I hope to cover the QTKit framework in a future article.

Setting Up Your Xcode Project

To play an audio file with QuickTime in a Carbon application, you must add the QuickTime framework and the audio files you want to play to your Xcode project. Choose Project > Add To Project to add files and frameworks to your project. The QuickTime framework should be in the directory System/Library/Frameworks. In your source code files you must add the header file Movies.h.

#include < QuickTime/Movies.h>

Starting and Ending QuickTime

Call the function EnterMovies() to start QuickTime. You should call EnterMovies() when you initialize your program. When the user quits your program, call ExitMovies() to stop QuickTime.

Next (Loading the audio file)