This is a follow up to this question:
End of playback detection in AudioTrack is not working
AudioTrack initialization and relavent code are in this function:
// function that plays out the audio through the audio hardware
void playSound(){
// final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
// sampleRate, AudioFormat.CHANNEL_OUT_MONO,
// AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
// AudioTrack.MODE_STATIC);
final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
sampleRate, AudioFormat.CHANNEL_OUT_MONO,
AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
AudioTrack.MODE_STREAM);
audioTrack.setNotificationMarkerPosition(generatedSnd.length/2);
// audioTrack.setNotificationMarkerPosition(1);
audioTrack.setPlaybackPositionUpdateListener(new AudioTrack.OnPlaybackPositionUpdateListener() {
@Override
public void onPeriodicNotification(AudioTrack track) {
// nothing to do
}
@Override
public void onMarkerReached(AudioTrack track) {
Log.d("MY LOG", "Audio track end of file reached...");
//bitPatternValTextView.setText("End of play");
// inSituPlaybackCompleted = true;
// idx = 0;
// btnVolumeUp.setEnabled(true);
}
});
audioTrack.write(generatedSnd, 0, generatedSnd.length);
audioTrack.play();
}
This function has been called elsewhere (in the same MainActivity class) elsewhere:
public void btnVolumeUp_click(View view){
textview.setText("Volume Up Button Clicked");
genTone(0.0);
genTone(1.0);
genTone(0.0);
genTone(0.0);
// genTone(1.0);
// genTone(1.0);
// genTone(1.0);
// genTone(1.0);
// Use a new tread as this can take a while
// final Thread thread = new Thread(new Runnable() {
// public void run() {
// // genTone(); // this line is left here for my record only
// handler.post(new Runnable() {
//
// public void run() {
// playSound();
// }
// });
// }
// });
// thread.start();
// btnVolumeUp.setEnabled(false);
playSound(); // TROUBLESHOOTING
}
Currently the playSound() has been called in the same UI thread, but the commented section shows how it had been called in a separate thread as well. (the idea for using the UI thread came from this comment which says try MODE_STREAM if using a separate thread). However, whether using a new thread or the UI thread, the setPlaybackPositionUpdateListener() does not seem to be working, as onMarkerReached() is not being called. Currently I generate my own audio samples (that is, I am not playing a file) and the whole audio (at most around 10 sec long) will have been generated before the playback starts (which makes me think I should be using MODE_STATIC since I am not doing any streaming, and to be honest I don't think I know how to use MODE_STREAM properly at this point anyway). In my code, if I do not change anything else and just change MODE_STATIC to MODE_STREAM in the AudioTrack initialization, the onMarkerReached() function seems to work. However, I would want it to work for MODE_STATIC for the reason stated above.
Why isn't onMarkerReached() being calle dfor AudioTrack MODE_STATIC and how do I fix it? Alternatively, all I really want to do is able to find out when the AudioTrac playback stops. If there are alternate ways to do it then those answers are welcome as well.
Aucun commentaire:
Enregistrer un commentaire