How to play audio user define times in android? - android

Can anyone give me a link of function to play audio file using user define time.. I am using handler but something is missing..
I am also done infinite time audio play but we need user defined times audio play.. Thanks...
Infinite time play audio is done using below function..
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
mMediaPlayer.start();
}
});
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
I just wanted how to play audio x times in android using Thread??
I am updating my function but i was not got 100% on this..so plz help
my updated function is as bellow
public void Repeat_mantra()
{
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
for(count=1;count<5;count++)
{
mMediaPlayer.start();
while(mMediaPlayer.isPlaying());
}
}
});
// mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
But this function is running then other clicks/or user interface are not appear because of "while(mMediaPlayer.isPlaying());" so any help me for how to update my function with Thread??

Related

Android Media player onClick issue

I'm using MediaPlayer for playing sounds onClick. Until the sound is finished the click event is not play the sound again. How can it play the sound again on click, when the sound is currently playing?
final MediaPlayer mistake = MediaPlayer.create(getActivity(), R.raw.mistake);
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mistake.start();
}
});
You must create a new MediaPlayer object to play the sound again like so.
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mistake != null) {
mistake.release();
mistake = MediaPlayer.create(getActivity(), R.raw.mistake);
}
mistake.start();
}
});
You can read more about MediaPlayer in the following links.
Also a quite similar question
MediaPlayer, MediaPlayer Tutorial, MediaPlayer Tutorial From Google
Most of the errors in MediaPlayer comes due to improper handling of different states of its object.
You should release MediaPlayer object after completing playback or before calling start() again.
It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately.
Create a MediaPlayer object as:
Mediaplayer mediaPlayer = null;
And call playMistakeSound() on button click:
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMistakeSound()
}
});
Implement playMistakeSound() as:
void playMistakeSound() {
try {
// releases MediaPlayer object before calling create() again while previous is still playing
if (mediaPlayer != null){
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(getActivity() /*Context*/, R.raw.mistake);
// this will release MediaPlayer as soon as it completes
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mp != null) {
mp.reset();
mp.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
} catch (Exception e) {
// log exception and handle
e.printStackTrace();
}
}

Stopping media player before playing next audio

I am having a listview in my app. Each listview item has button which will play an audio from a url received from web service. But my problem is that if I click play button from the next item then both start playing together. I am having problem in this. I want only one to play at a time. Right now I am creating new Media player object everytime button is clicked, but I also tried creating a single global object but in this case it only plays first time and not after it. What is the possible solution of it.
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Make the mp variable global and remove this:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
because if you take a look at this reference http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram it says:
Once the MediaPlayer object is in the End state, it can no longer be
used and there is no way to bring it back to any other state.
And when you call mp.release(); the media player WILL go to that state.
Then make your onClickListener look something like this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
//... and so on
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
The trick there is to make it back to Idle state so you can set the new data source and start playing again. It's all about the states...

Repeat sound every time a button is pushed

I'm testing Android sound implementation. I have an "imagenButton" and when I push it, a method "musica()" is called. Is really simple:
public void musica(View v) {
img = (ImageView) findViewById(R.id.Migrunido);
if(m.isPlaying()) {
m.stop();
}
else {
m.start();
}
}
It works, but want when I press the button, the sound repeats. I was reading a lot about threads and I have to prepare it before "stop()" but can't fix it.
I have to prepare my method? Or what is the problem?
Thanks
try
public static void playAudio(int id) {
mediaPlayer = MediaPlayer.create(context,id);
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.release();
}
});
}else{
mediaPlayer.stop();
mediaPlayer.release();
}
}
if this doesn't work try mediaPlayer.prepare(); before you start.

Play Sound File from res/raw using MediaPlayer

I want to play Sound Files from raw folder using MediaPlayer but I don't want to use MediaPlayer.create() method, since I want to play it multiple times and want to retain MediaPlayer object. I was successful in compiling the following code but it plays nothing and I can't hear any sound.
public void STARTSOUND(SOUND sound) throws IOException {
switch(sound) {
case SOUND_MENUBG:
Uri uri = Uri.parse("R.raw.mainmenu");
PLAYSOUND(uri);
break;
}
}
public void PLAYSOUND(Uri file) throws IOException {
mPlayerLoopSound.setDataSource(GameManager.getInstance().getCurrentActivity(),file);
mPlayerLoopSound.prepareAsync();
mPlayerLoopSound.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (!mPlayerLoopSound.isPlaying()){
mPlayerLoopSound.setVolume(1.0f, 1.0f);
//start the player
mPlayerLoopSound.start();
}
}
});
}
The onPrepared() method never gets called..
Am I doing anything wrong?
private void playButtonSound(final Context context, final int resourceId)
{
final MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
if (mediaPlayer != null)
{
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mediaPlayer)
{
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
}
}
Call this method like this.
playButtonSound(PopScreenActivity.this, R.raw.soundfilename);
First, hopefully you're initializing your MediaPlayer with:
mPlayerLoopSound = new MediaPlayer();
Next, make sure you call:
mPlayerLoopSound.setAudioStreamType(AudioManager.STREAM_MUSIC);
which:
Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.
Then, in your OnPreparedListener(), when onPrepared() is called, notice the parameter it has: MediaPlayer mp. This is the MediaPlayer that is ready for playback, so you can use it:
#Override
public void onPrepared(MediaPlayer mp) {
if(!mp.isPlaying()) {
mp.setVolume(1.0f, 1.0f);
mp.start();
}
}

How to play audio and wait to finish to countinue to play again?

How to play audio and wait to finish to countinue to play again?? . In this i can play so many time :D
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
if (mMediaPlayer.isPlaying() == false) {
mMediaPlayer.start();
mMediaPlayer
.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
};
});
}
}
});
The code is working fine for every button click. If you wish the audio to be repeated automatically, use
mMediaPlayer.setLooping(true);
If you wish that when Ist sound is completely played then, second one should start on a button click,
try disabling ur button click while the first sound is playing and then enable the click onCompleteListener... Some thing like this:
ivSelect.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Toast.makeText(context, "Hello", 1000).show();
mMediaPlayer = MediaPlayer.create(context, sound);
ivSelect.setClickable(false); //Disabling the button click
mMediaPlayer.start();
}
Enable click on Audio completion
mMediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
ivSelect.setClickable(true);
}
});
Try this
if(mMediaPlayer.isPlaying()){
mMediaPlayer.stop();
mMediaPlayer.release();
}

Categories

Resources