Android: Mixing multiple AudioTrack instances?
Asked Answered
P

1

9

I need to run two instances of AudioTrack at the same time. They must run separately because I'm playing them at different, variable sample rates. I found that if I run them in the same thread, they "take turns". I'm running them each in their own thread, but the audio is stuttering.

Any ideas on making two instances play nice? If not, any tips on mixing two short buffers into one, even if I want to play them at different sample rates.

Pentaprism answered 13/12, 2010 at 3:7 Comment(0)
L
12

I have 4 audioTracks playing at once and they seem to play fine. Testing on HTC Desire 1.1ghz OC. I get glitches with the threading sometimes though. Occasionally if all four are playing one will not stop when I try to join the thread. Need to do more testing. Here is my class for playing back a wav file recorded at a given path

    package com.ron.audio.functions;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;

public class AudioPlayManager implements Runnable {

private File fileName;
private volatile boolean playing;

public AudioPlayManager() {
    super();
    setPlaying(false);
}

public void run(){
      // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
      // and create a short array to store the recorded audio.
      int musicLength = (int)(fileName.length()/2);
      short[] music = new short[musicLength];

      try {
        // Create a DataInputStream to read the audio data back from the saved file.
        InputStream is = new FileInputStream(fileName);
        BufferedInputStream bis = new BufferedInputStream(is);
        DataInputStream dis = new DataInputStream(bis);

        // Read the file into the music array.
        int i = 0;
        while (dis.available() > 0) {
          music[i] = dis.readShort();
          i++;
        }

        // Close the input streams.
        dis.close();     

        // Create a new AudioTrack object using the same parameters as the AudioRecord
        // object used to create the file.
        AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 
                                                11025, 
                                               AudioFormat.CHANNEL_CONFIGURATION_MONO,
                                               AudioFormat.ENCODING_PCM_16BIT, 
                                               musicLength, 
                                               AudioTrack.MODE_STREAM);
        // Start playback
        audioTrack.play();

        // Write the music buffer to the AudioTrack object
        while(playing){
            audioTrack.write(music, 0, musicLength);
        }

      }
      catch(Exception e){
          e.printStackTrace();
      }

}


public void setFileName(File fileName) {
    this.fileName = fileName;
}

public File getFileName() {
    return fileName;
}

public void setPlaying(boolean playing) {
    this.playing = playing;
}

public boolean isPlaying() {
    return playing;
}

}

Lasko answered 26/1, 2011 at 23:16 Comment(6)
Thanks! Will try this. I'm not creating my AudioTrack in the thread callback like you are. I'm guessing that's the cure (the rest of your code is nearly identical to what I have). That would explain why the two AudioTrack's "toggle" -- they are blocking each other.Pentaprism
Let me know if this works for you. I also have all this wrapped in another class which will do all the thread management for me. I can then call the thread manager and not have to worry about the threading for recording or playing back the streams.Lasko
There was a little more to it than that, but the real key was creating the AudioTrack inside the thread. I had a separate sort of "Device" class so I could control tempo, volume, etc on it. Combining it into the class that reads the file and doing it all in one thread got them to play nice. Am now DJing tracks on my Optimus (600mHz). :)Pentaprism
Can you provide the sample project for that it will very appreciated .Stream
I could try and add the example project but it was not in a working state last time I checked. We are going back like 3 years now ;)Lasko
@Lasko three years you say?Pastille

© 2022 - 2024 — McMap. All rights reserved.