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,
Audi开发者_JAVA百科oFormat.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
State 3 is PLAYSTATE_PLAYING. I would try either calling stop() or reloadStaticData before calling play
the second time.
精彩评论