How to make my 'play' button clickable when my MediaPlayer is prepared - android

I'm building a radio application on android, I want know how to make my 'play' button clickable when my MediaPlayer is prepared?

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
// Prepared
}
}

Related

Android MediaPlayer plays sound with lower volume the first time

This is the code I'm using to create a notification sound. However, the media player plays the sound with a lower volume when the function is called for the first time. If this function is immediately called again, it then plays with the desired volume which is the stream volume.
Uri notification = RingtoneManager.getActualDefaultRingtoneUri(context,
RingtoneManager.TYPE_NOTIFICATION);
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setDataSource(context, notification);
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
mMediaPlayer.setOnPreparedListener(new
MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.setVolume(1, 1);
if(!mMediaPlayer.isPlaying())
mMediaPlayer.start();
}
});
mMediaPlayer.setOnCompletionListener(new
MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.release();
}
});
mMediaPlayer.prepareAsync();
How to play the sound with the right volume at all times?

Android Studio- Media Player doesn't play sound On Genymotion?

I am trying to build a small app which plays a sound when we click on the button. But I am not able to play the sound. Don't know what the problem is. Please help me on this. Below is the code.
public class MainActivity extends AppCompatActivity {
private Button button;
private MediaPlayer mediaPlayer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlayer = new MediaPlayer();
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.raw.song);
button = (Button)findViewById(R.id.mediaButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer.start();
}
});
}
}
Note:-Sorry guys,I thought that the problem is with my code but the app is running perfectly fine on my phone,so its the problem with my genymotion emulator.Can anyone please suggest me the solution for this.By the way,I am using Mac OSX.
The MediaPlayer has its own lifecycle. You can't just create the instance and then start to play. First you have to prepare it and then play it.
You can prepare your mediaplayer either sync or asynchronously.
Something along the lines of:
MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});
mediaPlayer.prepareAsync();
Or, if you want to do it synchronously
MediaPlayer mediaPlayer= new MediaPlayer();
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.song);
try {
mediaPlayer.prepare();
} catch (IOException e){
}
mediaPlayer.start();
Just make sure you prepare it before you play it.
Media Player lifecycle: https://developer.android.com/reference/android/media/MediaPlayer.html
You need to make sure the media player is ready before you can play it, so you set the onPreparedListener to handle this for you, like so:
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(getApplicationContext(),R.raw.song);
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mp.start();
}
});
button = (Button)findViewById(R.id.mediaButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.prepareAsync();
}
});
There will be a slight delay from when you press the button to the sound playing this way. Another way to do it could be to disable the button until the media player has prepared and then in the onclick of the button you could just call mp.start(); when the button has been enabled.

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...

How to play a lot of sounds in background of an activity?

For example, I want to play 3 songs. When the 1st song ends, the 2nd song begins, and when the 2nd song ends, the 3rd song begins, and when the 3rd song ends, the 1st song begins again and so on. Is the mp.setOnCompletionListener used here?
You are right. You can do something simple like this:
public class MainActivity extends Activity {
MediaPlayer mp1, mp2, mp3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mp1 = MediaPlayer.create(this, R.raw.music_1);
mp2 = MediaPlayer.create(this, R.raw.music_2);
mp3 = MediaPlayer.create(this, R.raw.music_3);
mp1.start();
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});
}
}
This will play mp1 and onCompletion of it, it will play mp2, and onCompletion of mp2, it will play mp3, and onCompletion of mp3, it will play mp1 again and so on...
First declare the 3 MediaPlayer files:
MediaPlayer song1;
MediaPlayer song2;
MediaPlayer song3;
Then initialize the MediaPlayer objects:
song1 = MediaPlayer.create(this, R.raw.exampleSong1);
song2 = MediaPlayer.create(this, R.raw.exampleSong2);
song3 = MediaPlayer.create(this, R.raw.exampleSong3);
Now start the first Media file with
song1.start();
Now with every instance created you should add to every MediaPlayer object a setOnCompletionListener like this:
song1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song2.start();
}
});
Do the same for the Second and Third MediaPlayer files:
song2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song3.start();
}
});
song3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
song1.start();
}
});

How to play audio user define times in 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??

Categories

Resources