Susheel
Susheel

Reputation: 784

Android play sound until explicitly stopped

Somebody has already written code for generation a tone with a particular frequency for a given time. The SO link is given here: How to Generate A Particular Sound Frequency?.

What I need is, to play the sound indefinitely and not for 3 seconds. I want the sound to stop playing when a button is clicked or something like that. I was thinking maybe I can use timer function to repeat the same loop over and over. When the user clicks 'stop' button I want to invoke audiotrack.pause or stop. Is this a good way to go about it or is there a better logic for this?

Upvotes: 1

Views: 1264

Answers (2)

Parth Sane
Parth Sane

Reputation: 587

Refer this. http://developer.android.com/reference/android/media/MediaPlayer.html

//Declare a new object

MediaPlayer Mp3=new MediaPlayer();

//Then set path of file
Mp3.setDataSource(“sdcard/filename.mp3”);

//Before playing audio, call prepare
Mp3.prepare();

//For Looping (true = looping; false = no looping)
Mp3.setLooping(true);

//To play audio ,call
Mp3.start();

//To pause, call
Mp3.pause

//To stop audio, call
Mp3.stop(); 

That's it done!

Upvotes: 2

Susheel
Susheel

Reputation: 784

Thanks Boggartfly but I found a much simpler solution. I can simply loop the audiotrack object.

I used the following code.

         audioTrack.setLoopPoints(0, generatedSnd.length/4, -1);

The start frame is zero. The end frame is length/4 for 16bit PCM. The negative 1 here means loop infinite times.

Upvotes: 1

Related Questions