cmakthat
cmakthat

Reputation: 201

Android AudioTrack slow playback

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?

Upvotes: 1

Views: 3878

Answers (3)

osum
osum

Reputation: 815

try with

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

It should play in normal speed

Upvotes: 1

cmakthat
cmakthat

Reputation: 201

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.

Upvotes: 0

Geobits
Geobits

Reputation: 22342

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

Upvotes: 1

Related Questions