如何找到audioTrack已经完成/停止播放?

时间:2014-09-30 08:00:12

标签: android playback audiotrack

我需要通过AudioTrack播放pcm文件,我想知道它已经播放完毕了吗?

我的代码:

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

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

然后

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);

但上述情况均无效。

我怎么知道它已经播放完毕?

2 个答案:

答案 0 :(得分:3)

参考这篇文章 How to tell when AudioTrack object has finished playing?

此解决方案使用“OnPlaybackPositionUpdateListener”通过标记音频长度来通知播放何时达到特定标记。

答案 1 :(得分: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));

                        }
                    });