使用MediaPlayer类播放音频(仅首次播放音轨)

时间:2014-11-16 14:32:01

标签: android audio android-mediaplayer

下面是listview适配器的代码,它显示了track的列表。每个列表项都有播放和停止图标来播放和停止跟踪。我遇到的问题是,当我点击播放时,该轨道才会首次播放按钮。之后如果我通过单击停止按钮停止它并尝试播放任何轨道它不会被播放。通过toast消息我发现holder.img1.setOnClickListener()方法确实被调用但是onPrepared(第二次或之后点击播放按钮时,方法不会被调用。但是,第一次调用onPrepared()方法时,这就是第一次播放轨道的原因..

如果我将mMediaPlayer=new MediaPlayer();放在上面holder.img1.setOnClickListener的onClick()方法中,问题会得到解决,但点击多个播放按钮会同时开始播放多首曲目,我不想要.. 以下是代码.. holder.img1用于播放图标,holder.img2用于停止图标

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

}

1 个答案:

答案 0 :(得分:2)

好像你多次调用prepareAsync,这会引发错误。

在你的onClick监听器中,你应该调用的第一个是:

mMediaPlayer.reset();