Game Development Reference
In-Depth Information
12. To avoid handling the complete RIFF WAVE ile format, we prepare a speciic ile
containing a single block of uncompressed audio data; the format of this data is a 22
kHz monophonic 16-bit sound. We pass
Data+sizeof(sWAVHeader)
as the audio
data, and the size of the audio data is obviously
DataSize-sizeof(sWAVHeader)
:
PlayBuffer( Data + sizeof( sWAVHeader ),
DataSize - sizeof( sWAVHeader ));
13. Then we call the
IsPlaying()
function in a spin loop to detect when OpenAL stops
playing the sound:
while ( IsPlaying() ) {}
14. Once the sound playback is complete, we delete all the objects we have created:
alSourceStop( FSourceID );
alDeleteSources( 1, &FSourceID );
alDeleteBuffers( 1, &FBufferID );
alcDestroyContext( FContext );
alcCloseDevice( FDevice );
15. Finally, we unload the OpenAL library on Windows:
UnloadAL();
16. On Android, it is very important to free the allocated resource and release the audio
device. Otherwise, audio will keep playing in the background. To avoid writing Java
code in this small example, we just terminate our native activity with the
exit()
call:
exit( 0 );
}
17. The code above uses the function
IsPlaying()
to check if the audio source is busy:
bool IsPlaying()
{
int State;
alGetSourcei( FSourceID, AL_SOURCE_STATE, &State );
return State == AL_PLAYING;
}
18. The function
PlayBuffer()
feeds the audio data to the audio source:
void PlayBuffer(const unsigned char* Data, int DataSize)
{
alGenBuffers( 1, &FBufferID );
alBufferData( FBufferID, AL_FORMAT_MONO16,
Data, DataSize, 22050 );
alSourcei( FSourceID, AL_BUFFER, FBufferID );
alSourcePlay( FSourceID );
}
};