How to check if an audio is playing with Android MediaPlayer? - java

In a situation where I have a listview that in each list item when clicking is an audio and is played when clicked on the desired item.
When you click on multiple items, the audio blends.
I would like to know how I can click on an item and play and if I want another item, when clicking it the previous audio stops and the audio of the clicaco item starts.
I tried to use the code below, but audios do not stop when I click on another item.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mediaPlayer = MediaPlayer.create(MainActivity.this, caminhoAudio[position]);
if ( !(mediaPlayer.isPlaying()) )
{
tocarSom();
}
}
});
}
public void tocarSom() {
if (mediaPlayer != null)
{
mediaPlayer.start();
}
// LIBERAR MEMÓRIA
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
Thank You!!!

Well, if your methode startAudio() automatically do everything needed to start to play a song, you have to call startAudio() after the mediaPlayer.release();

Do something like this.
if(mMediaPlayer!=null)
{
mMediaPlayer.release();
mMediaPlayer=null;
}

if(mMediaPlayer != null) {
//Fist stop the current playing raw file
mMediaPlayer.stop();
mMediaPlayer.release();
mMediaPlayer = null;
//Then Play the selected raw file.
tocarSom();
} else {
//If nothing is playing then Play the selected raw file or audio
tocarSom();
}

Related

MediaPlayer cannot paused in Adapter Recyclerview with once Click Button

please help, I always experience errors here, I create mediaplayer in the audio list using 1 button, I have created the condition and isPlaying function on the mediaplayer in the adapter class, when the audio button is clicked successfully run, but when I click a second time, audio can not be paused, just repeat from the beginning, help me, what is the right solution?
this is my code:
public void onBindViewHolder(#NonNull MurottalAdapter.MurotalHolder holder, int position) {
Item item = itemList.get(position);
holder.titles.setText(item.getTitle());
holder.btnplays.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//I've tried replacing this with holder.itemview.getContext() and I put it outside the onclick method, but the problem is the same
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());
if (mediaPlayer.isPlaying()){
if (mediaPlayer != null) {
mediaPlayer.pause();
holder.btnplays.setImageResource(R.drawable.ic_play_circle_filled_black_24dp);
}
} else {
if (mediaPlayer != null){
mediaPlayer.start();
holder.btnplays.setImageResource(R.drawable.ic_pause_circle_filled_black_24dp);
}
}
}
});
}
Pls try to move following code outside of onClick() method?
mediaPlayer = MediaPlayer.create(view.getContext(),item.getAudio());

Adding a sound to the onClick method

I would like to add a sound whenever the user clicks the apps button, any idea how can I do that?
I have tried to create a "raw" directory on the res/ file with different names, like for example "test.mp3", which did not work...
Playing sound is not difficult.
And as long as these are short sounds during app foreground operations it fine.
You need to use the MediaPlayer.
First, prepare it.
private MediaPlayer mMediaPlayer = null;
private MediaPlayer.OnCompletionListener mOnCompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
}
}
};
Now on click:
public void onItemClick(.........) {
releaseMediaPlayer();
mMediaPlayer = MediaPlayer.create(getActivity(),getSoundFileResID());
mMediaPlayer.setOnCompletionListener(mOnCompletionListener);
mMediaPlayer.start();
}
You need to implement the getSoundFileResID().
For more info read the MediaPlayer OverView

Return MediaController and MediaPlayer to the start of song after finish it

I've created MediaPlayer with MediaControll. It works but I want to return MediaController and MediaPlayer to the start of song after finish it. So when song finished there are pause button and seek bar in the end position are still presented. I want to set seek bar to start position and change button to play(so user can click on it to replay this song) when song finished. I have the same problem with this developer: Android MediaController End Of song . I applied solution of his problem but still can't reset Controller. What's wrong in my code? As I know I should write something in onCompletion method.
mPlayer = new MediaPlayer();
mPlayer.setOnCompletionListener(this);
controller = new MediaController(this){
#Override
public void show(int timeout) {
super.show(0);
}
};
listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mPlayer != null) {
mPlayer.release();
}
mPlayer = MediaPlayer.create(MainActivity.this, arrayList.get(position).getmSongResourceId());
mPlayer.start();
controller.show(0);
}
});
controller.setMediaPlayer(this);
And onCompletion method
#Override
public void onCompletion(MediaPlayer mp) {
mp.reset();
controller.show(0);
}
Thanks for any help.

Is it possible to have MediaPlayer play one audio file and when it finishes play the next?

I want MediaPlayer to take in an arraylist of songTitles and have it check which song title came in, then play that song. When it finishes, I then want it to go to the next song title in the loop and play that one. However, my code only plays the last song.
public void play(Context c, ArrayList<String> songTitles) {
stop();
for (String song: songTitles){
if (song.equalsIgnoreCase("shakeItOff")){
mSongPlayer = MediaPlayer.create(c, R.raw.shaketoff);
} else if (song.equalsIgnoreCase("dropItLow")){
mSongPlayer = MediaPlayer.create(c, R.raw.dropitlow);
} else if (song.equalsIgnoreCase("chachaslide")){
mSongPlayer = MediaPlayer.create(c, R.raw.chachaslide);
}
mSongPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (mp == mSongPlayer) {
mSongPlayer.start();
}
}
});
mSongPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
}
});
}
}
You need to use the MediaPlayer.OnCompletionListener to start the new track when the previous one finishes, so you could do something more like the following (I haven't compiled it, so there may be some syntax errors):
public void play(final Context c, ArrayList<String> songTitles) {
stop();
if (songTitles != null && songTitles.size > 0) {
final List<String> playList = new ArrayList<String>(songTitles);
String song = playList.get(0);
playList.remove(0);
mSongPlayer = MediaPlayer.create(c, getSongResourceId(song));
mSongPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (mp == mSongPlayer) {
mSongPlayer.start();
}
}
});
mSongPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
stop();
// Recursively call the play() method with one less
// track in the list.
play(c, playList);
}
});
}
}
public int getSongResourceId(String songTitle) {
if (song.equalsIgnoreCase("shakeItOff")){
return R.raw.shaketoff;
} else if (song.equalsIgnoreCase("dropItLow")){
return R.raw.dropitlow;
} else if (song.equalsIgnoreCase("chachaslide")){
return R.raw.chachaslide;
}
}
The first time through, this plays the first track in the list, then in the MediaPlayer.OnCompletionListener, it recursively calls the play() method with a copy of the list that has had the first track removed from it. This means each time play() is called, the list is shorter until we reach the end of the list.
I'm not sure if there is a way to do it only using a single MediaPlayer.
But, I'm sure that your are only playing the last song due to the follow:
Every time you create the MediaPlayer, you are overriding the previously one, so if your first MediaPlayer is for "shakeItOff", the next is for "dropItDown", when you reach your last one, the "chachaslide", you end up referencing a MediaPlayer that will play chachaslide.
Every time you go through the loop you are overriding the last OnCompletionListener that you attached before. So, you end with the last listener that you attached (your last song)
You are asserting the prepared MediaPlayer to the current MediaPlayer, so even if you manage to prepare every MediaPlayer to play, you will only play the currentOne, as for 1 is "chachaslide".
You can check the lifecycle of the MediaPlayer here: http://developer.android.com/reference/android/media/MediaPlayer.html
So, the only way you can reuse the same MediaPlayer reference is to manage it for yourself, other way you end with at least 3 instances at memory of the MediaPlayer

audio player app for android

hi i have created a new mp3 player app for my owm. In that i am playing songs from a folder where its been stored, the problem is when one song is in play and when i click the next song both gets played, i want the other one to be stopped. the following is my code
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
{
Log.e("", "aaaaa "+arg2);
if(arg2==0)
{
mplayer = MediaPlayer.create(aply.this, R.raw.a);
mplayer.start();
}
else
{
mplayer = MediaPlayer.create(aply.this, R.raw.download);
mplayer.start();
}
mplayer.start();
}
});
pls help me in this....
you can check the playing status, if it is playing you have to stop the player and reset the contents and issue the command Play.
if (mplayer.isPlaying()) {
mp.stop();
}
mplayer.reset();
mplayer.setDataSource(abs_filepath);//set the path
mplayer.prepare();
mplayer.start();
above a sample code
Don't you want a check like this:
if (mplayer != null && mplayer.isPlaying()) {
mplayer.stop();
}
at some point before you start your next song?

Categories

Resources