MediaPlayer cannot paused in Adapter Recyclerview with once Click Button - java

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());

Related

Adding music in android studio (java, MediaPlayer)

buttonMusic = findViewById(R.id.buttonMus);
musicSound = MediaPlayer.create(this, R.raw.music);
buttonClick();
}
private Button buttonMusic;
private MediaPlayer musicSound;
public void buttonClick() {
buttonMusic.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
soundPlay(musicSound);
}
}
);
}
public void soundPlay(MediaPlayer sound) {
if (sound.isPlaying()) {
sound.stop();
}else {
sound.start();
sound.setLooping(true);
} }
Hello.
The code launches the music, is able to stop it, but it wont play again after pressing the play button, after pausing the song that is.
you aren't pausing song, you are stop()ing it. use sound.pause() for pausing :)
after stop() MediaPlayer you have to prepare it again, use prepare() or prepareAsync(). MediaPlayer.create( makes that for you at the beginning, also if you would create MediaPlayer using its constructor you would also want to prepare()/preapreAsync() before calling start() in onClick
check out state diagram of MediaPlayer

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.

How to stop the sound/audio when you press the back button in the phone

how can i stop the audio from playing when i press the back button in my phone. when i go to another activity the audio is still playing unless i close the app. here's my code:
FloatingActionButton fab7 = (FloatingActionButton) this.findViewById(R.id.fab7);
final MediaPlayer mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
});
1.) declare MediaPlayer mp; outside onCreate
2.) override onStop and apply the same logic of click listener inside onStop
why onStop not onBackPress?
Because you want to pause the player when you go outside app as well as when you go to another activity
MediaPlayer mp;
onCreate(..){
mp = MediaPlayer.create( this, R.raw.cebu);
fab7.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {;
pausePlayer();
}
});
}
#Override
public void onStop(){
super.onStop()
pausePlayer();
}
void pausePlayer(){
if(mp.isPlaying()){
mp.pause();
} else {
mp.start();
}
}
#Override
public void onBackPress() {
pausePlayer();
mp.stop();
}
Use this to detect and pause the Mediaplayer
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed Song paused");
mp.pause();
}
return super.onKeyDown(keyCode, event);
}
write mediaplayer.stop();
on start of back activity when u gone
Override Activity#onBackPress() method to stop your player when pressing Back button:
#Override
public void onBackPress() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
If you want to stop your player when your Activity finish, and don't care why (may be Back press, another Activity started, app crashed,..), you can override Activity#onStop() so you will make sure your music is immediately stop whenever your Activity gone!
#Override
public void onStop() {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
super.onStop();
}
Btw, note that you should use mp.stop() instead of mp.pause() then mp.release() to avoid memory/resources leak!

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

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();
}

Android: button is not changing to pause state when i click

i have coded for media player and it is playing song .In the front end only image button is used and hardcoded as play button
when i click again it has to change image to pause state but the button which i have used is playing the song two times simultaneously whenever i click on it which is wrong
but i need the song to be paused so whereever i used
mp.start();
function which will be start to play the music from the url i added the code as
if(!mp.isPlaying()){
mp.start();
buttonPlayPause.setBackgroundResource(R.drawable.pause);
}else {
mp.pause();
buttonPlayPause.setBackgroundResource(R.drawable.play);}
but when i click the play button it starts playing and again if i click the button again it starts playing two times simultaneously
please help me how should i start and pause the mp3 file which i have displayed it in url it is not going to else block itself should i use different function
the full code of the project is
public class MainActivity5 extends Activity implements OnClickListener,
OnPreparedListener, OnErrorListener, OnCompletionListener {
MediaPlayer mp;
ProgressDialog pd;
Button bt;
ImageButton iv;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main5);
iv = (ImageButton)findViewById(R.id.play);
iv.setOnClickListener(this);}
#Override
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
//mp.start();
/*if(mp.isPlaying()== true)
{
mp.start();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.pause();
mp.release();
iv.setImageResource(R.drawable.play);
}*/
}
#Override
public void onClick(View v) {
try
{
pd = new ProgressDialog(this);
pd.setMessage("Buffering.....");
//pd.show();
mp = new MediaPlayer();
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnPreparedListener(this);
mp.setOnErrorListener(this);
mp.setDataSource("http://192.168.1.138/Android/music/vande.mp3");
mp.prepareAsync();
mp.setOnCompletionListener(this);
}
catch(Exception e)
{
Log.e("StreamAudioDemo", e.getMessage());
}
}
#Override
public boolean onError(MediaPlayer mp, int what, int extra) {
pd.dismiss();
return false;
}
#Override
public void onCompletion(MediaPlayer mp) {
pd.dismiss();
Toast.makeText(getApplicationContext(), "Completed", Toast.LENGTH_LONG).show();
}}
thanks for your help in advance
i have commented some code above i have to re correct it
you can have a try at your onclickListener,when this player is playing,if you want to pause it ,you should to call MediaPlayer.pause() ,if you want to release it data,you can call MediaPlayer.release(),then ,you can play next music.
use this
public void onPrepared(MediaPlayer mp)
{
Log.i("StreamAudioDemo", "prepare finished");
//pd.setMessage("Playing.....");
// mp.start(); - this should be removed
if(mp.isPlaying()== true)
{
mp.pause();
iv.setImageResource(R.drawable.pause);
}
else
{
mp.start();
mp.release();
iv.setImageResource(R.drawable.play);
}
}

Categories

Resources