Android MediaPlayer - Unwanted new instance created whenever activity is instantiated

286 views Asked by At

I've got a MediaPlayer inside an activity which is called whenever a user clicks on a specific song. Once the MediaPlayer is playing, if the user clicks back, they can select another song.

The problem is that upon selection of a second song, the MediaPlayer is created again, and two songs begin playing at once. I've tried using "if MediaPlayer.isplaying, MediaPlayer.stop" at the beginning of the activity, but then the MediaPlayer stops as soon as the user presses back and scrolls for another song.

The code is shown below. I'd appreciate it if you could keep your answers basic, as I have a limited understanding of java (which I'm attempting to develop!).

Activity containing MediaPlayer:

package music.tabber;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

public class MusicPlayer extends Activity {

    private ImageButton playButton;
    private TextView songTitleLabel;
    private TextView songArtistLabel;
    private MediaPlayer mediaPlayer = new MediaPlayer();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.playing);

        Bundle extras = getIntent().getExtras();
        String songPath = extras.getString("songPath");
        String songTitle = extras.getString("songTitle");
        String songArtist = extras.getString("songArtist");

        playButton = (ImageButton) findViewById(R.id.playButton);
        songTitleLabel = (TextView) findViewById(R.id.songTitleLabel);
        songArtistLabel = (TextView) findViewById(R.id.songArtistLabel);

        playSong(songPath); 

        songArtistLabel.setText(songArtist);
        songTitleLabel.setText(songTitle);


        playButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                if(mediaPlayer.isPlaying()){
                    if(mediaPlayer!=null){
                        mediaPlayer.pause();
                        playButton.setImageResource(R.drawable.ic_play);
                    }

                }else{
                    if(mediaPlayer!=null){
                        mediaPlayer.start();
                        playButton.setImageResource(R.drawable.ic_pause);
                    }
                }
           }

        });

    }

    private void playSong(String songPath) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(songPath);
            mediaPlayer.prepare();
            mediaPlayer.start();

            // Changing Button Image to pause image
            playButton.setImageResource(R.drawable.ic_pause);

        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Thanks for your help.

1

There are 1 answers

2
c0dehunter On BEST ANSWER

Maybe you want to consider Services to have a flexible control over MediaPlayer. It is simple to play/pause from any Activity.