暂停正在播放的歌曲并播放新歌曲

时间:2014-08-06 04:52:25

标签: android eclipse

我正在尝试为我的android 4.4.4v(kitkat)构建一个音乐播放器。问题是,每当我试图播放一首新歌时,已播放的旧歌曲都不会停止并继续播放。因此,同时播放多首歌曲。你能帮我解决这个问题。

提前致谢。

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

1 个答案:

答案 0 :(得分:0)

您在暂停按钮的onClick逻辑中犯了错误。

尝试这种方式:

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

    }
});

希望它有所帮助。