Michal
Michal

Reputation: 11

Best way to use Androids Audiotrack

I'm implementing an Android app to handle PCM sound data (for long sounds i.e. music tracks). I decided to use the AudioTrack class to play the music, but it can't be placed in the same thread as the activity, because is blocking the whole app - and here is the question - should I put AudioTrack operations in a separate thread or in an async task (or any other option)? What would be the best way? I need to play / pause / stop and change music files.

Now I'm trying to manage it by a java thread and its almost ok - playing is ok, pausing almost (its "dropping" a part of the sound), but when I'm changing the music file, the previous isn't stopping and theres a mixed output (I think theres something wrong with my thread implementation).

And - no, I can't use the MediaPlayer in this app (I want to modify the PCM data on the fly).

I was searching for some help in google and here on stackoverflow, but nothing helped me.

Here is my current implementation, if somebody want to take a look.

public class AudioPlayerManager {

private AudioTrack track;
private Thread thread;
private AudioTrackThread trackThread = new AudioTrackThread();

public int getPlayState() {
    return track.getPlayState();
}

public int getAudioSession() {
    return track.getAudioSessionId();
}

class AudioTrackThread implements Runnable {
    private String filePath = "/music/wav/musicfile.wav";

    @Override
    public void run() {
        try {
            playWaveFile();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void playWaveFile() throws FileNotFoundException {       
        int minSize = AudioTrack.getMinBufferSize(44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT);
        track = new AudioTrack(AudioManager.STREAM_MUSIC, 44100,
                AudioFormat.CHANNEL_OUT_STEREO,
                AudioFormat.ENCODING_PCM_16BIT, minSize,
                AudioTrack.MODE_STREAM);

        File file = new File(FileListingUtils.getExternalStorageRootFile(),
                filePath);
        InputStream is = new FileInputStream(file);

        byte[] music = new byte[512];
        track.play();

        try {
            handleSound(is, music);
        } catch (IOException e) {
            e.printStackTrace();
        }

        track.stop();
        track.release();
    }

    private void handleSound(InputStream is, byte[] music)
            throws IOException {
        int i;
        while ((i = is.read(music)) != -1) {
            track.write(music, 0, i);
        }
    }
}

public void playNew() {
    if (track != null) {
        track.stop();
        //track.flush();
        track.release();
    }

    thread = new Thread(trackThread);
    thread.start();
}

public void playOrResume() {
    this.track.play();
}

public void pause() {
    this.track.pause();
}

}

Thanks for every help!

M.

Upvotes: 1

Views: 3227

Answers (1)

Mehdi Fanai
Mehdi Fanai

Reputation: 4059

Your thread life cycle doesnt seem to be right. Firstly You will have to close(interrupt)your current thread every time playback is stopped or finished and create a new instance of the thread and close the old thread.

Secondly, you are calling track.stop()from outside your thread. You must implement a public stop method inside your thread and then insert finalisation code there and then call yourthread.stop() in your playNew() method.

so here is the pseudocode:

public void playNew() {

//the following if clause should go to thread's stop() method.
  // if (track != null) {
  //  track.stop();
 //track.flush();
 //    track.release();
// }
  //create a new thread if its null
if(thread ==null){
thread = new Thread(new AudioTrackThread());
thread.start();}
//stop the current thread
else{
thread.stop();
thread=null;}
}

about your implementation, somewhere i read that to play music, a service and a thread will be a better option.Read about services and threads in android documentation.

Upvotes: 1

Related Questions