如何在离开屏幕后停止播放音乐

时间:2018-04-09 22:07:15

标签: android

我有一个包含多个listviews的简单播放列表,其中包含musicAdapter。该应用非常有效,当用户离开屏幕(使用listView播放列表)时,音乐继续播放。我怎么能阻止这种情况发生?我是应用程序开发人员的新手,我在线阅读但我无法找到适用于我案例的内容。请帮忙!谢谢! 我的代码是这样的:



 public class MusicAdapter extends BaseAdapter {
        private Context context;
        private int layout;
        private int currentPosition;
        private ArrayList arrayList;
        private MediaPlayer mediaPlayer;
        private boolean flag = true;
    private ImageView currentPlayingButton;
        public MusicAdapter(Context context, int layout, ArrayList<Music> arrayList) {
            this.context = context;
            this.layout = layout;
            this.arrayList = arrayList;
        }
    
    
        @Override
        public int getCount() {
            return arrayList.size();
        }
    
        @Override
        public Object getItem(int position) {
            return null;
        }
    
        @Override
        public long getItemId(int position) {
            return 0;
        }
        private class Holder{
            TextView textName,textAlbum;
            ImageView play, stop;
        }
    
    
        @Override
        public View getView(final int pos, View convertView, final ViewGroup parent) {
            final Holder holder;
            final int position = pos;
            if (convertView == null){
                holder = new Holder();
                LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(layout,null);
                holder.textName = (TextView) convertView.findViewById(R.id.textName);
                holder.textAlbum = (TextView) convertView.findViewById(R.id.textAlbum);
                holder.play = convertView.findViewById(R.id.play);
                holder.stop = convertView.findViewById(R.id.stop);
                currentPlayingButton= holder.play;
                convertView.setTag(holder);
            }else {
                holder = (Holder) convertView.getTag();
    
            }
            final Music music = (Music) arrayList.get(position);
            holder.textName.setText(music.getName());
            holder.textAlbum.setText(music.getAlbum());
    
            holder.play.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    if(mediaPlayer == null){
    
                        mediaPlayer = MediaPlayer.create(context, music.getSong());
                        mediaPlayer.start();
                        currentPosition = position;
                         holder.play.setImageResource(R.drawable.pause);
    currentPlayingButton= holder.play;
                    } else {
                        if(currentPosition != position){
    
                            mediaPlayer.reset();
                            mediaPlayer.release();
                            currentPlayingButton.setImageResource(R.drawable.ic_play);
    
                            currentPlayingButton= holder.play;
                            currentPosition=position;
    
                            mediaPlayer = MediaPlayer.create(context, music.getSong());
                            mediaPlayer.start();
                            holder.play.setImageResource(R.drawable.pause);
                            currentPosition = position;
                        }else{
                            if (mediaPlayer.isPlaying()){
                                Log.v("Adapter", "mediaPlayer should be playing here" + mediaPlayer.isPlaying());
                                mediaPlayer.pause();
                                holder.play.setImageResource(R.drawable.ic_play);
                                Log.v("Adapter", "user clicked the same row and isPlaying");
                                Log.v("Adapter", "" + currentPosition);
                                Log.v("Adapter", "mediaPlayer shouldn't be playing here" + mediaPlayer.isPlaying());
                            } else {
                                Log.v("Adapter", "mediaPlayer shouldn't be playing here" + mediaPlayer.isPlaying());
                                mediaPlayer.start();
                                holder.play.setImageResource(R.drawable.pause);
                                Log.v("Adapter", "current pos == pos and !isPlaying");
                                Log.v("Adapter", "" + currentPosition);
                                Log.v("Adapter", "mediaPlayer should be playing here" + mediaPlayer.isPlaying());
                            }
                        }
                    }
                }});
    
    
    
    
            holder.stop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mediaPlayer != null){
    
                        mediaPlayer.stop();
                        mediaPlayer.release();
                        mediaPlayer = null;
                        holder.play.setImageResource(R.drawable.ic_play);
    
                    }
                }
    
            });
    
    
            return convertView;
    
        }
    
    }
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

在MusicAdapter类中添加以下功能

    public void stopMusic() {
        if(mediaPlayer != null){
               mediaPlayer.stop();
               mediaPlayer.release();
               mediaPlayer = null;
               holder.play.setImageResource(R.drawable.ic_play);
        }
    }

然后在您调用适配器的Activity中,每当要停止播放器时都必须调用此函数