AudioTrack - how do I know when a sound starts or ends?

I use Audio track to play different sounds in streaming mode. I would like to know if there is a way to find out when each creature sound / finishes the game.

I create an audio track as follows:

AudioTrack tmpAudioTrack = new AudioTrack( STREAM_TYPE, SAMPLE_RATE, CHANNEL_CONFIG_TYPE, AUDIO_FORMAT_TYPE, getOptimalBufferSize(), AUDIO_TRACK_MODE);' 

And run it in the background thread:

 backround_thread = new Thread(new MyRunnable(aTrack)); backround_thread.start(); 

I write every sound like this in runnable:

 byte generatedSnd[] = new byte[2 * beepSamples]; <code for filling the buffer with sound here> int bytesWritten = track.write(generatedSnd, 0, generatedSnd.length); 

Can I use any of the AudioTrack APIs, such as setNotificationMarkerPosition or setLoopPoints, or setPositionNotificationPeriod for this? and how do they work?

Each sound may differ from time to time. I think this is the key word.

I do not fully understand the documentation for these APIs. Does each frame match the pattern? How to specify a marker for each beginning / end of each sound?

Thanks,

+7
source share
1 answer

Here is what I learned.

For me, frames are samples. This is the duration of sound multiplied by the sampling rate.

To use AudioTrack setPositionNotificationPeriod, you pass the number of samples. If you pass 200 samples, a callback will be called every 200 samples that are repeated.

 tmpAudioTrack.setPositionNotificationPeriod(duration * sampleRate); 

To use setNotificationMarkerPosition, you also pass the number of samples. However, this is absolute and not relative, as for the period API. Therefore, if you want to determine when the sound ends, you transmit the number of samples (total duration of the audio track * sampleRate).

 tmpAudioTrack.setNotificationMarkerPosition(duration * sampleRate); 

But if you are already playing in the middle of your audio track and want to add a marker so that you get a callback, say after 3 seconds, then you will need to add the current point of sound playback to the track, for example: (where your duration is 3 seconds)

 tmpAudioTrack.setNotificationMarkerPosition(tmpAudioTrack.getPlaybackHeadPosition() + (duration * sampleRate)); 

And so you register for period and brand notifications.

 tmpAudioTrack.setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener(){ @Override public void onMarkerReached(AudioTrack arg0) { } @Override public void onPeriodicNotification(AudioTrack arg0) { } 

});

+12
source

All Articles