Android Developer
Android Developer

Reputation: 9643

Playing audios using MediaPlayer class(track gets played only for first time)

Below is the code for the listview adapter which displays list of tracks.Each list item has play and stop icons to play and stop track.The issue I am having is that track gets played only for first time when i click on play button.After that if i stop it by clicking on stop button and the try to play any track it doesn't get played.Through toast message i found out that holder.img1.setOnClickListener() method does get called but onPrepared() method doesn't get called on clicking on play button for second time or after that.However onPrepared() method does get called for first time thats why track gets played for the first time..

If i put mMediaPlayer=new MediaPlayer();inside onClick() method of holder.img1.setOnClickListenerabove issue does get resolved but then clicking on multiple play buttons will start playing multiple tracks at same time which i don't want.. Here is the code .. holder.img1 is for play icon and holder.img2 is for stop icon

SoundCloudAdapter

public class SoundCloudAdapter extends BaseAdapter{

    private MediaPlayer mMediaPlayer;
    private Context context;
    private ArrayList<SoundCloud> soundcloudList;

    public SoundCloudAdapter(Context context, ArrayList<SoundCloud> soundcloudList)
    {
        this.context=context;
        this.soundcloudList = soundcloudList;
        mMediaPlayer=new MediaPlayer();
    }

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

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

    @Override
    public Object getItem(int position)
    {
        return soundcloudList.get(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        final ViewHolder holder;

        if (convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.soundcloud_item, null);
            holder.textview = (TextView) convertView.findViewById(R.id.title);
            holder.textview2 = (TextView) convertView.findViewById(R.id.id);
            holder.textview3 = (TextView) convertView.findViewById(R.id.download);
            // holder.btn = (Button) convertView.findViewById(R.id.button);
            holder.img1 = (ImageView) convertView.findViewById(R.id.play2);
            holder.img2 = (ImageView) convertView.findViewById(R.id.pause);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        holder.textview.setText(soundcloudList.get(position).getTitle());
        holder.textview2.setText(soundcloudList.get(position).getId());
        holder.textview3.setText("Play Count : "+soundcloudList.get(position).getDownload());
        if(soundcloudList.get(position).getPlaying())
        {

            holder.img1.setVisibility(View.GONE);
            holder.img2.setVisibility(View.VISIBLE);
        }
        else
        {

            holder.img1.setVisibility(View.VISIBLE);
            holder.img2.setVisibility(View.GONE);
        }
        holder.img1.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(final View v)
            {

                Toast.makeText(context,"img1",Toast.LENGTH_LONG).show();
                soundcloudList.get(position).setPlaying(true);
                try
                {
                    holder.img1.setVisibility(View.INVISIBLE);
                    holder.img2.setVisibility(View.VISIBLE);

                    if(mMediaPlayer.isPlaying())
                    {
                        Toast.makeText(context,"isplaying",Toast.LENGTH_LONG).show();
                        mMediaPlayer.pause();
                    }

                    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                    mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/" + soundcloudList.get(position).getId() + "/stream?client_id=xyz");
                    mMediaPlayer.prepareAsync();
                    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
                    {
                        @Override
                        public void onPrepared(MediaPlayer mp)
                        {
                            Toast.makeText(context,"prepatre",Toast.LENGTH_LONG).show();
                            mp.start();

                        }
                    });
                    mMediaPlayer.setOnCompletionListener(
                            new MediaPlayer.OnCompletionListener()
                            {
                                @Override
                                public void onCompletion(MediaPlayer mp)
                                {
                                    soundcloudList.get(position).setPlaying(false);
                                    holder.img1.setVisibility(View.VISIBLE);
                                    holder.img2.setVisibility(View.INVISIBLE);

                                }
                            });
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }

            }

        });
        holder.img2.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(final View v)
            {
                Toast.makeText(context,"stop",Toast.LENGTH_LONG).show();;
                soundcloudList.get(position).setPlaying(false);


                holder.img1.setVisibility(View.VISIBLE);
                holder.img2.setVisibility(View.INVISIBLE);


                mMediaPlayer.pause();


            }

        });

        return convertView;

    }

    static class ViewHolder
    {
        public TextView textview;
        public TextView textview2;
        public TextView textview3;
        public ImageView img1;
        public ImageView img2;
        public Button btn;
    }

}

SoundCloud

public class SoundCloud {
    private String title;
    private String kind;
    private String id;
    private String duration;
    private String download;
    private boolean isPlaying=false;
    public SoundCloud()
    {
        super();


    }
    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }
    public String getKind()
    {
        return kind;
    }

    public void setKind(String kind)
    {
        this.kind = kind;
    }
    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }
    public String getDuration()
    {
        return duration;
    }

    public void setDuration(String duration)
    {
        this.duration = duration;
    }

    public String getDownload()
    {
        return download;
    }

    public void setDownload(String download)
    {
        this.download = download;
    }
    public Boolean getPlaying()
    {
        return isPlaying;
    }

    public void setPlaying(Boolean isPlaying)
    {
        this.isPlaying = isPlaying;
    }

}

Upvotes: 0

Views: 344

Answers (1)

Simas
Simas

Reputation: 44118

It seems like you are calling prepareAsync multiple times, which should throw an error.

In your onClick listener the first you should call is:

mMediaPlayer.reset();

Upvotes: 2

Related Questions