Playing Audio Files with QTKit
Author: Mark Szymczyk
Last Update: December 5, 2007
This article shows developers how to use Apple’s QTKit framework to play audio files in their Cocoa applications.
QTKit is a framework that lets Cocoa programmers make use of QuickTime through Objective C classes instead of QuickTime’s C API. By using QTKit you’ll write less code than you will using the C API. If you doubt me, compare the amount of code in this article with the amount of code in my QuickTime audio article.
QuickTime is Apple’s multimedia technology. While its main purpose is playing video, you can use QuickTime to display images and play audio. QuickTime can play multiple audio formats, including MP3, AAC (the format the iTunes Music Store uses), AIFF, and WAV files. With QTKit you can use the same set of code to play any audio file whose format QuickTime supports.
Apple introduced QTKit in Mac OS X 10.4. If you want to support earlier versions of Mac OS X, you must either use QuickTime’s C API, which I covered in a previous article, or use the NSMovie class.
Setting Up Your Xcode Project
To use QTKit with Xcode, you must first create a Cocoa application project. Choose File > New Project to create an Xcode project. After creating the project, add the QTKit framework to your project. Select the Frameworks folder in the Groups and Files list and right-click (or control-click if you have a single button mouse) to open a contextual menu. Choose Add > Existing Frameworks from the contextual menu to add the QTKit framework.
Unless you’re planning on having the user choose the files he or she wants to play, you’ll be supplying the audio files for your application to play. In this case you should add the audio files you want to play to your Xcode project so they get added to the application bundle when Xcode builds the project. Select the Resources folder in the Groups and Files list and right-click to open a contextual menu. Choose Add > Existing Files from the contextual menu to add your audio files.
In your source code files that use QTKit you must include QTKit.h, the QTKit header file.
#import < QTKit/QTKit.h >
Next (Reading the File)