简体   繁体   中英

To pause the song which is playing and play a new song

I'm trying to build a music player for my android 4.4.4v(kitkat). The problem is, whenever I'm trying to play a new song, the old song which is already playing doesn't stops and it continues playing. Therefore multiple songs are played simultaneously. Can you help me fix this issue.

Thanks in advance.

package com.demo.songs;


public class CustomList extends ArrayAdapter<String> {

    private ArrayList<String> songName;
    private final Activity context;
    ImageButton playButton, pauseButton;
    MediaPlayer mediaPlayer;

    public CustomList(Activity context, ArrayList<String> songName) {
        super(context, R.layout.list_single, songName);
        this.context = context;
        this.songName = songName;

    }

    public View getView(final int position, View view, ViewGroup parent) {
      mediaPlayer = new MediaPlayer();
      playButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          try {
            if (mediaPlayer != null) {
              if (mediaPlayer.isPlaying()) {
                System.out.println("inside the if block");
                mediaPlayer.stop();
              } 
              mediaPlayer.setDataSource(abc);
              mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
              mediaPlayer.prepare();
              mediaPlayer.start();
           }
         } catch (IllegalArgumentException e) {
           e.printStackTrace();
         } catch (SecurityException e) {
           e.printStackTrace();
         } catch (IllegalStateException e) {
           e.printStackTrace();
         } catch (IOException e) {
           e.printStackTrace();
         }
       }
    });
    pauseButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        try {
          if (mediaPlayer.isPlaying()) {
            mediaPlayer.setDataSource(abc);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.stop();
          }
        } catch (IllegalArgumentException e) {
          e.printStackTrace();
        } catch (SecurityException e) {
          e.printStackTrace();
        } catch (IllegalStateException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (mediaPlayer.isPlaying()) {
          mediaPlayer.pause();
        }
      }
    });
    return rowView;
    }
}

You are making mistake in your pause button's onClick in logic.

Try this way:

pauseButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        try {
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.stop();
                mediaPlayer.setDataSource(abc);
                mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                //here you may start new song using mediaPlayer.start()
            }
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
});

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM