I have a problem with AudioTrack, this Android API is killing me. I do not proceed from previous experience with Android or Java, but I am a very experienced coder (asm, C ++, etc. On many platforms), and I never thought that I was especially dumb, like Android.
What am I doing wrong? Apparently nothing:
audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, minbufsizbytes*64, AudioTrack.MODE_STATIC); audioTrack.write(Buffer, 0, numSamples); audioTrack.play();
Actually, sampleRate = 8000 and minbufsizbytes = 742, which (742 * 64 = 47488) is significantly larger than the buffer that I write (16000 16-bit samples).
The sound is played for the first time. But ... how can I play it more than once? (for example, in response to an event, for example, for example, pressing a piano key). If I call play () again, there will be no new sound. So after a few days of frustration, this is what I came up with:
for (i=0;;i++) { SystemClock.sleep(3000L);
So, it plays the sound of the second OR (!) For the third time .. then NO AUDIO !! And the log receives an error message:
05-18 13:03:16.785: ERROR/AudioFlinger(345): TrackBase::getBuffer buffer out of range: 05-18 13:03:16.785: ERROR/AudioFlinger(345): start: 0x404fb680, end 0x404fb7f2 , mBuffer 0x40507000 mBufferEnd 0x40512980 05-18 13:03:16.785: ERROR/AudioFlinger(345): server 0, serverBase 23744, user 47488, userBase 47488, channels 1
Then I have to restart the phone (emulated or real), otherwise the log stream will not stop.
(mis) It behaves on my Galaxy 2.2.1, on my IDEOS 2.1 and on the emulator (various versions) .. so this is not a problem with the phone error.
If I make the internal buffer larger (the 5th parameter in AudioTrack), it will play more times before it stops making sounds and starts to fill the log, so I think that if the internal buffer was overloaded
PS: Do you know if getMinBufferSize returns samples or bytes (even for PCM_16BIT), as some of them have reported?