Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a listview playlist with mediaplayer in musicAdapter.every item from list have a play/pause and stop btn.whe you click play it changes to pause and other way.stop btn stops the music and changes the pause button to play.
This is my musicAdapter:
<pre lang="java">package com.example.android.musicapp;

import android.content.Context;
import android.media.MediaPlayer;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

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;


                        mediaPlayer = MediaPlayer.create(context, music.getSong());
                        mediaPlayer.start();
                        currentPlayingButton.setImageResource(R.drawable.pause);
                        currentPlayingButton = holder.play;
                        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;


    }
   
    {

}}


i have 2 problems:1) i set my play/pause button to also change when another play btn from another position is clicked.It works onlyu that besides position 6,7 and 8,no matter on witch position you click play another play btn from another position appear with the pause drawable though the music remains the one from the position we clicked, and 2) i seem to not understand how to stop music from playing when the back button or home is clicked or is changing screens.

What I have tried:

for the music stoping when changing screens etc. i tried:
public void stopMusic() {
        if(mediaPlayer != null){
               mediaPlayer.stop();
               mediaPlayer.release();
               mediaPlayer = null;
               holder.play.setImageResource(R.drawable.ic_play);
        }
    }


i put it in my holder.stop.setOnClickListener(...)
Posted
Updated 10-Apr-18 21:16pm
Comments
David Crow 11-Apr-18 15:46pm    
For your first problem, something you could try is:
// in the getView() method:
holder.play.setTag(Integer.valueOf(position));

// in the onClick() method:
Integer pos = (Integer) v.getTag();
Music music = arrayList.get(pos.intValue());
Another option might be:
// in the getView() method:
holder.play.setTag(music);

// in the onClick() method:
Music music = (Music) v.getTag();
"...i seem to not understand how to stop music from playing when the back button or home is clicked or is changing screens."

Are you calling mediaPlayer.stop() in an activity method such as onPause(), onStop(), or onDestroy()?
Member 13772817 11-Apr-18 16:38pm    
i does not work in both cases is the "Incompatible types"error
Member 13772817 11-Apr-18 16:29pm    
i tried this*
public void stopMusic() {
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
holder.play.setImageResource(R.drawable.ic_play);
}
}
*in holder.stop.setOnClickListener does not work and this*
public void onStop() {
if(mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;

}
}
*last in my code didn;t work.i read almoust everyting on this.and looked other answers,but all that i try is not working

1 solution

You need to capture the Pause or Stop events of your application. You can then stop the player before your app gets put in the background. see The Activity Lifecycle | Android Developers[^].
 
Share this answer
 
Comments
Member 13772817 11-Apr-18 4:07am    
thank you! do you know why my first problem occurs?
Richard MacCutchan 11-Apr-18 4:16am    
I don't quite understand the other issue. You mention multiple play buttons - where do they come from?
Member 13772817 11-Apr-18 4:37am    
I have a listview of song,each of them have a play and a stop button.when you click play,the button changes to pause and the music starts but also another play btn,from another position is changing to pause (but the music doesn't start)when i click the position i want.i think something is wrong in currentPosition!=position
Richard MacCutchan 11-Apr-18 5:04am    
It sounds like you are accessing an incorrect item when the button is pressed. You need to do some debugging to find the actual values that are being used to access the items.
Member 13772817 11-Apr-18 13:51pm    
@Richard thise is my logcat.it doesn;t say much
Reconstruct Branch : NOTHING
04-11 20:48:13.308 12675-12675/com.example.android.musicapp D/MediaPlayer: ANDROID_HTC_INVOKE_GET_CALLING_PROCESS packageName: com.example.android.musicapp
04-11 20:48:13.318 12675-12675/com.example.android.musicapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-11 20:48:13.368 12675-12675/com.example.android.musicapp D/MediaPlayer: Is using offload now: true
04-11 20:48:22.158 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() in
04-11 20:48:22.288 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() out
04-11 20:48:22.298 12675-12675/com.example.android.musicapp I/MediaPlayer: release() in
release() out
04-11 20:48:22.298 12675-12675/com.example.android.musicapp D/MediaPlayer: ANDROID_HTC_INVOKE_GET_CALLING_PROCESS packageName: com.example.android.musicapp
04-11 20:48:22.308 12675-12675/com.example.android.musicapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-11 20:48:22.338 12675-12675/com.example.android.musicapp D/MediaPlayer: Is using offload now: true
04-11 20:48:27.778 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() in
04-11 20:48:27.798 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() out
release() in
release() out
04-11 20:48:27.798 12675-12675/com.example.android.musicapp D/MediaPlayer: ANDROID_HTC_INVOKE_GET_CALLING_PROCESS packageName: com.example.android.musicapp
04-11 20:48:27.818 12675-12675/com.example.android.musicapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-11 20:48:27.858 12675-12675/com.example.android.musicapp D/MediaPlayer: Is using offload now: true
04-11 20:48:33.868 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() in
04-11 20:48:34.008 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() out
release() in
release() out
04-11 20:48:34.008 12675-12675/com.example.android.musicapp D/MediaPlayer: ANDROID_HTC_INVOKE_GET_CALLING_PROCESS packageName: com.example.android.musicapp
04-11 20:48:34.028 12675-12675/com.example.android.musicapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-11 20:48:34.058 12675-12675/com.example.android.musicapp D/MediaPlayer: Is using offload now: true
04-11 20:48:37.138 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() in
04-11 20:48:37.268 12675-12675/com.example.android.musicapp I/MediaPlayer: reset() out
release() in
release() out
04-11 20:48:37.268 12675-12675/com.example.android.musicapp D/MediaPlayer: ANDROID_HTC_INVOKE_GET_CALLING_PROCESS packageName: com.example.android.musicapp
04-11 20:48:37.288 12675-12675/com.example.android.musicapp D/MediaPlayer: setSubtitleAnchor in MediaPlayer
04-11 20:48:37.318 12675-12675/com.example.android.musicapp D/MediaPlayer: Is using offload now: true
04-11 20:48:38.788 12675-12675/com.example.android.musicapp I/MediaPlayer: release() in
04-11 20:48:38.918 12675-12675/com.example.android.musicapp I/MediaPlayer: release() out
04-11 20:48:38.918 12675-12675/com.example.android.musicapp W/MediaPlayer: mediaplayer went away with unhandled events

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900