ADC Home > Reference Library > Technical Q&As > QuickTime > Audio >

Playing memory-resident WAVE data using QuickTime 4


Q: How do I play memory-resident WAVE data using QuickTime 4?

A: QuickTime 4 has a built-in WAV file importer. You can open and play a WAV file quite easily by opening the file as if it were a QuickTime movie file and playing the "movie" (WAV file). For example, here's some QuickTime code which you can use to play any WAV file (simply select your WAV file at the file prompt and QuickTime will play it as a sound-only movie):

OSErr PlayWAVfile ()
{
    SFTypeList        myTypeList;
    StandardFileReply myReply;
    OSErr             err = noErr;
    short             movieRefNum, resID = 0;
    Movie             movie;

        StandardGetFilePreview(NULL, -1, myTypeList, &myReply);
        if (!myReply.sfGood)
        {
            err = userCanceledErr;
            return err;
        }
        else
        {
            err = OpenMovieFile(&myReply.sfFile, &movieRefNum, fsRdPerm);
            if (!err)
            {
                err = NewMovieFromFile(&movie,
                                       movieRefNum,
                                       &resID,
                                       NULL,
                                       newMovieActive,
                                       NULL);
            }

            if (err)
            {
                if (movie)
                {
                    DisposeMovie(movie);
                }
            }
            else
            {
                SetMovieVolume(movie, kFullVolume);
                GoToBeginningOfMovie(movie);
                StartMovie(movie);
                while (!IsMovieDone(movie))
                {
                    MoviesTask(movie, 0);
                    err = GetMoviesError();
                }
            }
        }

        return err;
}

It's a bit more work to play a WAV sound using QuickTime if your WAV data is in memory instead of in a file. In this case, one option is to create a new movie and add the WAV data as a separate sound track (since the QuickTime WAV importer only works with files). Inside Macintosh: QuickTime chapter 2 contains sample code which shows how to create a movie from scratch and add a sound track to the movie. Simply add the WAV data to the sound track of your movie as you would normally then play the movie.

You can also use the QuickTime WAV movie import component along with the MovieImportDataRef function to add memory-resident WAV file data to a movie. The MovieImportDataRef function lets you specify the data reference to use for the import operation (in this case, a handle data reference for the WAV file data). Note that this does not mean you actually pass the handle to your data in the dataRef parameter; instead, the dataRef is a handle that starts with the handle to your data (see code below):

void ImportWAVDataFromMemory(Ptr waveDataPtr, long waveDataSize)
{
    Handle                  myHandle, dataRef = nil;
    Movie                   movie;
    MovieImportComponent    miComponent;
    Track                   targetTrack = nil;
    TimeValue               addedDuration = 0;
    long                    outFlags = 0;
    OSErr                   err;
    ComponentResult         result;

        myHandle = NewHandleClear((Size)waveDataSize);
        BlockMove(waveDataPtr,
                    *myHandle,
                    waveDataSize);

        err = PtrToHand(&myHandle,
                        &dataRef,
                        sizeof(Handle));

        miComponent = OpenDefaultComponent(MovieImportType,
                                            kQTFileTypeWave);
        movie = NewMovie(0);

        result = MovieImportDataRef(miComponent,
                                    dataRef,
                                    HandleDataHandlerSubType,
                                    movie,
                                    nil,
                                    &targetTrack,
                                    nil,
                                    &addedDuration,
                                    movieImportCreateTrack,
                                    &outFlags);

        SetMovieVolume(movie, kFullVolume);
        GoToBeginningOfMovie(movie);
        StartMovie(movie);
        while (!IsMovieDone(movie))
        {
            MoviesTask(movie, 0);
            err = GetMoviesError();
        }
}

[Jul 21 1999]


Did this document help you?
Yes: Tell us what works for you.
It’s good, but: Report typos, inaccuracies, and so forth.
It wasn’t helpful: Tell us what would have helped.