3

I am trying to playback audio using AudioTrack.

I can playback at normal speed. And I can playback slowly.

But I cannot playback fast.

How can I playback fast?

try (InputStream is = getResources().openRawResource(R.raw.sample)) {
    byte[] bytes = new byte[is.available()];

    is.read(bytes);

    final int SAMPLING_RATE = 48000;

    final int size = AudioTrack.getMinBufferSize(
            SAMPLING_RATE,
            AudioFormat.CHANNEL_OUT_MONO,
            AudioFormat.ENCODING_PCM_16BIT);

    AudioTrack audioTrack = new AudioTrack.Builder()
            .setAudioAttributes(new AudioAttributes.Builder()
                    .setUsage(AudioAttributes.USAGE_MEDIA)
                    .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                    .build())
            .setAudioFormat(new AudioFormat.Builder()
                    .setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
                    .setEncoding(AudioFormat.ENCODING_PCM_16BIT)
                    .setSampleRate(SAMPLING_RATE)
                    .build())
            .setBufferSizeInBytes(size)
            .build();

    audioTrack.setPlaybackRate(SAMPLING_RATE);

    PlaybackParams params = audioTrack.getPlaybackParams();
    //params.setSpeed(1.0f);  // available
    //params.setSpeed(0.5f);  // available
    params.setSpeed(1.5f);  // not available
    audioTrack.setPlaybackParams(params);

    audioTrack.play();

    audioTrack.write(bytes, 44, bytes.length - 44);

    audioTrack.stop();

    audioTrack.flush();
} catch (IOException e) {
    Log.e(TAG, e.toString());
}

params.setSpeed(1.5f) is not available.

I got the following error message.

 06-14 09:24:55.714 957-12866/? D/AES: onEndOfErrorDumpThread: data_app_crash Process: com.example.audiotracksample
 Flags: 0x38e8bf46
 Package: com.example.audiotracksample v1 (1.0)
 Foreground: Yes
 Build: COVIA/CP-L45s/CP-L45s:7.0/NRD90M/1502850909:user/release-keys
 java.lang.IllegalArgumentException: arguments out of range
     at android.media.AudioTrack.native_set_playback_params(Native Method)
     at android.media.AudioTrack.setPlaybackParams(AudioTrack.java:1590)
     at com.example.audiotracksample.MainActivity$AudioThread.run(MainActivity.java:69)
     at java.lang.Thread.run(Thread.java:761)
  • compileSdkVersion : 26
  • minSdkVersion : 24
  • targetSdkVersion : 26
pagliarulo
  • 31
  • 4

3 Answers3

1

According to the documentation, the buffer also needs to be large to increase speed.

If you want to increase the speed by 2x, you should use a buffer at least twice the min buffer size

audioTrack = AudioTrack(
        AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_MEDIA)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build(),
        AudioFormat.Builder()
            .setChannelMask(channelConfig)
            .setSampleRate(sampleRateInHz)
            .setEncoding(audioFormat)
            .build(),
        max(minBufferSize, bufferSize) * 2, // see here.
        AudioTrack.MODE_STREAM,
        AudioManager.AUDIO_SESSION_ID_GENERATE
    )

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        val playbackParams = PlaybackParams()
        playbackParams.speed = 2F
        audioTrack.playbackParams = playbackParams
    }
0

You can increase youre parameter SAMPLING_RATE

missing17
  • 79
  • 9
0

AudioTrack: Use of stream types is deprecated for operations other than volume control

Nxtpr
  • 21
  • 1