Reading the File

Reading an audio file with QTKit requires three steps. First, you must locate your application’s bundle. Second, you must find the file in the bundle. Third, you must create a movie that you will use to play the audio.

Get the Bundle

Use the NSBundle class’s mainBundle method to get your application’s main bundle.

NSBundle* appBundle = [NSBundle mainBundle];

Find the File

Use NSBundle’s pathForResource method to locate a file in the application bundle. This method takes two arguments. The first argument is the file name, and the second argument is the file’s extension. Suppose you have a file named MusicFile.mp3. You can either supply MusicFile.mp3 as the filename and nil as the extension, or you can supply MusicFile as the filename and mp3 as the extension.

NSBundle* appBundle;
NSString* filename;
NSString* audioFile = [appBundle pathForResource:filename ofType:nil];

Create the Movie

Use the QTMovie class’s initWithFile method to create a movie for the audio file you want to play. This method takes two arguments. The first argument is the path to the file you got when you called pathForResource. The second argument is an optional error argument that tells you what went wrong if the movie couldn’t be created. If you don’t care about the error, pass nil.

NSString* audioFile;
QTMovie* soundToPlay = [[QTMovie alloc] initWithFile:audioFile error:nil];

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