2

I need to play pcm file through AudioTrack, I want know to that it has finished playing or not?

My code:

scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public
              void run() {

              if (audioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED) {
              System.out.println("stop");  } // doSomethingUseful();
              }
             }, 0, 1, TimeUnit.SECONDS);

then

do{
 if (audioTrack.getPlayState() == AudioTrack.PLAYSTATE_STOPPED) {

              // stop(); // myAudioRecorder.release(); // stop(); //
              myAudioRecorder = new MediaRecorder(); // myAudioRecorder.stop();
              System.out.println("stopped......................."); //
              myAudioRecorder.release(); r_start = false; }

              } while (r_start == true);

But none of above is working.

How can I know it has finished playing?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Android
  • 8,995
  • 9
  • 67
  • 108

2 Answers2

3

Refer to this post How to tell when AudioTrack object has finished playing?

This solution using "OnPlaybackPositionUpdateListener" that notifies when playback reached specific mark by marking length of audio length.

Community
  • 1
  • 1
han0ah
  • 229
  • 2
  • 12
0
audioTrack
                    .setPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener() {

                        @Override
                        public void onPeriodicNotification(AudioTrack track) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onMarkerReached(AudioTrack track) {
                            // TODO Auto-generated method stub

                            stop();

                            // messageHandler.sendMessage(messageHandler.obtainMessage(PLAYBACK_END_REACHED));

                        }
                    });
Android
  • 8,995
  • 9
  • 67
  • 108