Android AudioTrack slow playback

别来无恙 提交于 2019-12-08 05:54:05

问题


I'm trying to stream music in using AudioTrack. The track plays, but the audio plays at half the rate. It's like the song has become slow motion.

try{
    AudioTrack track= new AudioTrack( AudioManager.STREAM_MUSIC,
        44100,
        android.media.AudioFormat.CHANNEL_CONFIGURATION_MONO,
        android.media.AudioFormat.ENCODING_PCM_16BIT,

        android.media.AudioTrack.getMinBufferSize( 44100,
                android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                android.media.AudioFormat.ENCODING_PCM_16BIT ),
        AudioTrack.MODE_STREAM );

System.out.println("Min buffer: " + android.media.AudioTrack.getMinBufferSize( 44100,
                android.media.AudioFormat.CHANNEL_CONFIGURATION_STEREO,
                android.media.AudioFormat.ENCODING_PCM_16BIT ));


  int cnt;
  int totalWrite = 0;
  boolean play = true;
  byte buff[]=new byte[16384];
  while((cnt=CircularByteBuffer.getInstance().getInputStream().read(buff))>0){

      totalWrite += cnt;
      System.out.println("Writing: " + cnt);
      track.write(buff, 0, cnt);

      if ( totalWrite > 60000 && play ){

          track.play();
          play = false;
      }
  }



      }catch (Exception e)
      {
}//end catch    

In the CircularByteBuffer, the bytes are being written on another thread and read on this one. The song plays consistently without any pauses, but it just plays at a slow rate. I have no idea what it could be. Any ideas?


回答1:


Assuming this is in fact a stereo stream, why are you creating the AudioTrack with CHANNEL_CONFIGURATION_MONO?




回答2:


So it turns out that all the settings were fine. I ended up using the "CHANNEL_CONFIGURATION_STEREO" setting but I tried to run the application on an actual device. When I streamed it from the device, the music was perfectly fine. But the simulator was causing all the problems for the song.

I hope that is the reason and the simulator is just a memory hog and runs inefficiently.




回答3:


try with

track.setPlaybackRate(88200);
track.play();

It should play in normal speed



来源:https://stackoverflow.com/questions/12940603/android-audiotrack-slow-playback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!