Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android AudioTrack() How do you stop a playing stream after it starts

Tags:

android

My target API is 2.2 I create audio snippets on the fly so using soundpool or mediaplayer are out of the question. One item I found that wasnt/isnt well documented is that AudioTrack() will create a set limit of instances. I found it to very between 6 and 12 instances. One thing that was not covered in the docs is that each time initiate a AudioTrack() it creates a new instance. Session ID is not implemented until version 2.3 so GetSessionID() is not available under 2.2. A lot of problems I see with questions about are that each time you do AudioTrack audioTrack = (new) AudioTrack (the various params here); It starts a new process so just doing audioTrack.stop(); Does not work if you are trying to stop a previous stream.

SO my problem is I start an audioTrack playing that may be over minute long. This is done in out of stream process (uh separate routine being passed the parameters) the streams play fine. The program is doing some other user directed task and I want to stop the the audiotrack before it completes its' playback buffer.

I need a way of referencing the audio track that is playing and stopping it. My newbieness and too long a C programmer along with the lack of Java experience is getting in the way. Surely there must be a way to stop audiotrack at any time. Looking for just a way to reference the audiotrack and stop it.
I thought maybe android.media.audiotrack.stop(); might be close but close dont cut it. Help! I've spent 15 hours looking for an example. Tnx

like image 584
user841735 Avatar asked Dec 27 '22 17:12

user841735


1 Answers

Calling these 3 methods worked for me. I also have code to stop the buffering which is in another thread.

audioTrack.flush();
audioTrack.stop();
audioTrack.release();
like image 144
Andrew Thomas Avatar answered May 15 '23 02:05

Andrew Thomas