1

Im a newbie to android studio (programming at general) but i want to build a mediaplayer for learning purposes.

I have a a list of local music in a listView that contains 2 images 'play, pause and stop button'.

This is how my app works:

  • Click on playbutton -> start music
  • Click on pausebutton -> pause music
  • Click on stopbutton -> stop music

Very simple. BUT! the thing is -> when i pause a song and want to play another song in my list then it just resume the first song i clicked on.

I want it to release the first song and start the other one i click on.

This is my code:

// play music
    viewHolder.playB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (flag) {
                //get song you clicked on
                mediaPlayer = MediaPlayer.create(context, song.getSong());

                //set boolean to false so it can get ANOTHER song when 
                //clicked
                flag = false;
                Toast.makeText(context, "Playing" + song.getName(), 
                Toast.LENGTH_LONG).show();
            }
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                viewHolder.playB.setImageResource(R.drawable.play);
            } else {
                mediaPlayer.start();
                viewHolder.playB.setImageResource(R.drawable.pause);
            }
        }
    });

    // stop
    viewHolder.stopB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!flag) {
                mediaPlayer.stop();
                mediaPlayer.release();
                flag = true;
            }
            viewHolder.playB.setImageResource(R.drawable.play);
        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Delice
  • 743
  • 9
  • 20

1 Answers1

0

Your logic is a little bit wrong. When you create the MediaPlayer you set flag to false and as long as you do not stop playback flag doesn't change. But your MediaPlayer creation depends on it.

For future improvements (maybe when you're more confident in working with MediaPlayer and Android) you should take a look at a more "self-made" approach instead of MediaPlayer.create(...) cause this method is calling MediaPlayer's prepare() method that is going to eventually make your app extremely slow or crash it when loading big files.

Example according to comments
I assumed songis going to be the class object.

// class variable will hold the currently loaded song
private Song mCurrentSong;

[...]

viewHolder.playB.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // MediaPlayer has not been initialized OR clicked song is not the currently loaded song
        if (mCurrentSong == null || song != mCurrentSong) {

            // Sets the currently loaded song
            mCurrentSong = song;
            mediaPlayer = MediaPlayer.create(context, song.getSong());

            Toast.makeText(context, "Playing" + song.getName(), 
            Toast.LENGTH_LONG).show();
        }

        if (mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            viewHolder.playB.setImageResource(R.drawable.play);
        } else {
            mediaPlayer.start();
            viewHolder.playB.setImageResource(R.drawable.pause);
        }
    }
});

viewHolder.stopB.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // If currently loaded song is set the MediaPlayer must be initialized
        if (mCurrentSong != null) {
            mediaPlayer.stop();
            mediaPlayer.release();
            mCurrentSong = null; // Set back currently loaded song
        }
        viewHolder.playB.setImageResource(R.drawable.play);
    }
});
CodeRed
  • 481
  • 4
  • 17
  • I found a tutorial from another person creating this mediaplayer but the logic is a bit confusing for me. I really dont understand the `flag` part. Can you elaborate for me please. – Delice Nov 08 '18 at 18:47
  • Hm. For me it seems like this `flag`s purpose is really only to help you with play and pause. It is like a initialization flag. Problem here is that as long as you don't stop the player it will always be initialized. So a better approach would be to check if the song to play equals the song that is already initialized. I'll edit my answer with an example. Could you tell me what getSong() returns? – CodeRed Nov 08 '18 at 19:04
  • getSong() returns my Song class with 3 properties: string Name, string Singer, int Song .. in other words: it returns the song i click on in my listview – Delice Nov 08 '18 at 19:14
  • Added an example. Please ask if you have any questions :) – CodeRed Nov 08 '18 at 19:26
  • Thank you! it worked perfectly. Im trying to completely understand the logic right now. So you basically checked if the current song is equal to the newly clicked one and if yes -> create a new instance of the song. Did i understand it correctly? – Delice Nov 08 '18 at 19:52
  • Not exactly. If the current song equals the newly clicked one I don't create a new instance. You only need a new instance if the songs are different. – CodeRed Nov 08 '18 at 19:58