onStop()片段不会停止mediaplayer

时间:2018-11-30 23:27:57

标签: android android-fragments android-mediaplayer

当我使用onStop()从Fragmant A转到Fragment B时,我试图停止播放歌曲,但是我无法使其正常工作。

这是调试结果: enter image description here

我的mediaplayer变量正在获取歌曲属性,但跳过了-> mediaplayer.isPlaying语句。

当我将其插入片段B时,它仍会继续播放歌曲。

我在做什么错了?

注意:我不播放Fragment A中的音乐(我只是在此处添加歌曲)。我从自定义适配器播放歌曲。不知道是否可能是这种情况,但如果是->那么我无法在适配器内调用onStop()

已编辑: 这是我的适配器类:

public class ListViewAdapter extends BaseAdapter {

//Create variables
MediaPlayer mediaPlayer;
int layout;
Song currentSong;
ArrayList<Song> arrayList;
Context context;

//Constructor
public ListViewAdapter(int layout, ArrayList<Song> arrayList, Context context) {
    this.layout = layout;
    this.arrayList = arrayList;
    this.context = context;
}

//ViewHolder class holding my views
private class Viewholder {
    TextView artistTxt, songNameTxt;
    ImageView playB, stopB;
    CircleImageView artistImg;
}

@Override
public int getCount() {
    return arrayList.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View view, ViewGroup parent) {

    //Create viewholder variable
    final Viewholder viewholder;

    //Check if view is null
    if (view == null) {

        //Create new ViewHolder object
        viewholder = new Viewholder();

        //Inflate my view
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.listview_customlayout, null);

        //Find my view id's
        viewholder.artistImg = (CircleImageView) view.findViewById(R.id.artistImgBackgroundDetail);
        viewholder.artistTxt = (TextView) view.findViewById(R.id.artistTxt);
        viewholder.songNameTxt = (TextView) view.findViewById(R.id.songNameTxt);
        viewholder.playB = (ImageView) view.findViewById(R.id.playB);
        viewholder.stopB = (ImageView) view.findViewById(R.id.stopB);

        //Set my view to viewholder
        view.setTag(viewholder);
    } else {
        viewholder = (Viewholder) view.getTag();
    }

    //Assign song to my arraylist
    final Song song = arrayList.get(position);

    //Set my views to their resources
    viewholder.artistImg.setImageResource(song.getArtistImg());
    viewholder.artistTxt.setText(song.getArtist());
    viewholder.songNameTxt.setText(song.getSongName());

    //get all songs
    mediaPlayer = MediaPlayer.create(context, song.getSong());

    //Play button click performed
    viewholder.playB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            //Checks if my current song is null and creates a new song
            if (currentSong == null) {
                mediaPlayer = MediaPlayer.create(context, song.getSong());

            }

            //if mediaplayer is not null and my current song is not equal to the new song i clicked on
            if (mediaPlayer != null && currentSong != song) {

                //resets the mediaplayer and creates a new song from the position in the list
                mediaPlayer.reset();

                mediaPlayer = MediaPlayer.create(context, song.getSong());
                viewholder.playB.setImageResource(R.drawable.play_black);

                mediaPlayer.start();
                viewholder.playB.setImageResource(R.drawable.pause_black);
            } else {
                mediaPlayer.pause();
                viewholder.playB.setImageResource(R.drawable.play_black);
            }

            //check if current song is null or the newly clicked song is equal to my current song
            //if true then assign the newly clicked song as my CURRENT one
            //--so it doesnt play the same song for every single one
            if (currentSong == null || song != currentSong) {
                currentSong = song;
            }
        }
    });

    //Stop song when click performed
    viewholder.stopB.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //stops my current song and make it null
            if (currentSong != null) {
                mediaPlayer.stop();
                mediaPlayer.release();

                currentSong = null;
                viewholder.playB.setImageResource(R.drawable.play_black);
            }
        }
    });

    return view;
}

}

2 个答案:

答案 0 :(得分:1)

mediaPlayer = MediaPlayer.create(...)
.
.
.
mediaPlayer.stop()

您创建一个新播放器,然后将其停止。您永远不会停止已经在玩的游戏。

答案 1 :(得分:1)

您可以尝试添加一种方法来停止适配器中的媒体播放器:

public void stop() {
    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
        mediaPlayer.release();
        mediaPlayer = null
    }
}

在片段停止时调用该方法:

@Override
public void onStop() {
    super.onStop();
    adapter.stop();
}