Loading the Audio File

The hardest part of using QuickTime to play audio files is loading the audio file into memory. There are four steps to take to load an audio file on Mac OS X. The first step is to find the main application bundle by calling the function CFBundleGetMainBundle().

CFBundleRef gameBundle = CFBundleGetMainBundle();

The second step is to find the file you want to play in the bundle by calling the function CFBundleCopyResourceURL(). This function takes four arguments. The first argument is the bundle you retrieved by calling CFBundleGetMainBundle(). The second argument is the file’s name. The third argument is the file’s extension. The final argument is a subdirectory the operating system should search for the file. You can pass NULL as the final argument, which tells the operating system to search the entire bundle for the file.

CFBundleRef gameBundle;
CFStringRef filename; 
CFStringRef fileExtension; 
CFStringRef subdirectory; 
CFURLRef fileLocation; 

fileLocation = CFBundleCopyResourceURL(gameBundle, filename, 
        fileExtension, subdirectory);

Suppose you want to load a file named Music.mp3. There are two ways you can call CFBundleCopyResourceURL(). First, you can use Music as the file name and mp3 as the extension. Second, you can use Music.mp3 as the file name and NULL as the extension. Both methods work. The first method is slightly faster if you have lots of files in your application bundle.

The third step is to set up the audio file so QuickTime can load the file into memory. The easiest way to set up the audio file is to call the function QTNewDataReferenceFromCFURL(). This function was introduced in QuickTime 6.4. QuickTime 6.4 shipped with Mac OS X 10.3, which means QTNewDataReferenceFromCFURL() works on Mac OS X 10.3 and later. People running Mac OS X 10.2 have to download and install QuickTime 6.4.

QTNewDataReferenceFromCFURL() takes four arguments. The first argument is the file location you retrieved by calling CFBundleCopyResourceURL(). The second argument is flags, which you should set to 0. The third argument is the data reference that QTNewDataReferenceFromCFURL() returns. The fourth argument is the data type of the data reference that QTNewDataReferenceFromCFURL() returns.

OSErr error; 
Handle dataRef; 
OSType dataRefType; 
CFURLRef fileLocation; 

error = QTNewDataReferenceFromCFURL(fileLocation, 0, 
        &dataRef, &dataRefType);

The final step is to load the audio file by calling NewMovieFromDataRef(). This function takes five arguments. The first argument is the QuickTime movie NewMovieFromDataRef() creates. The second argument is flags, which you can set to 0. The third argument is an ID that specifies the resource containing the movie data. You should pass the value movieInDataForkResID, which means the movie data is in the file’s data fork. The final two arguments are the data reference and data type you created by calling QTNewDataReferenceFromCFURL().

OSErr error; 
Handle dataRef; 
OSType dataRefType; 

short fileID = movieInDataForkResID; 
short flags = 0; 
error = NewMovieFromDataRef(&soundToPlay, flags, 
        &fileID, dataRef, dataRefType);

Next (Playing, pausing, and setting the volume)
Previous (Introduction)