Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

audioTrack play() plays only once

Tags:

android

using Android's AudioTrack for the first time. I have created a class AndroidAudioDevice. I init it with this constructor:

 public AndroidAudioDevice( ){  // constructor
           Log.i("Audio", "constructor");
           int minSize =AudioTrack.getMinBufferSize( SAMPLE_RATE, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT );        
           track = new AudioTrack( AudioManager.STREAM_MUSIC, SAMPLE_RATE, 
                                                AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT, 
                                                minSize, AudioTrack.MODE_STATIC);

           createSample();
           track.write( buffer, 0, buffer.length );
       };

(SAMPLE_RATE is set to 44100).

My main activity simply has a button, which calls

public void playSound(){
           track.play();
           Log.i("Audio", "playState: " + track.getPlayState());
       };

this works find BUT ONLY ONCE! If I press the button again no sound anymore. 

BTW: Log.i displays a "3" for the playstate

Any idea why this works only once? Thanks in advance

like image 977
Addi Avatar asked Jul 12 '11 00:07

Addi


1 Answers

State 3 is PLAYSTATE_PLAYING. I would try either calling stop() or reloadStaticData before calling play the second time.

like image 64
Femi Avatar answered Sep 27 '22 21:09

Femi