1

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?

Cristiano Fontes
  • 4,920
  • 6
  • 43
  • 76
cmakthat
  • 201
  • 4
  • 7

3 Answers3

1

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

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Honestly, I have no idea. I was using examples online and I was just trying different combination settings. When I do use "CHANNEL_CONFIGURATION_STEREO", it plays a bit faster without sounding as much like it's in slow motion but ends up becoming choppier. – cmakthat Oct 17 '12 at 19:38
  • You may want to go through all of your code to make sure everything(including the other thread that's writing to the buffer) matches up with what the audio format actually is, instead of assuming all your streams will be the same and hard-coding the formats. – Geobits Oct 17 '12 at 19:44
1

try with

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

It should play in normal speed

osum
  • 805
  • 2
  • 10
  • 32
0

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.

cmakthat
  • 201
  • 4
  • 7