我有一个列表视图,显示轨道列表。每个列表项都有播放和停止图标播放和停止跟踪。我遇到的问题是,如果我点击多个播放按钮,MediaPlayer会同时播放多个音频,然后,如果我试图阻止多个音频应用程序崩溃。如何阻止MediaPlayer同时播放多个音频?我希望如果媒体播放器播放一个音轨并且用户点击另一个播放按钮,则媒体播放器应该停止播放上一个音轨并播放音轨点击播放按钮用户..
答案 0 :(得分:0)
我遇到的问题是,如果我点击多个播放按钮 MediaPlayer同时播放多个音频
这是因为您每次MediaPlayer
都会创建onClick
的新实例:
mMediaPlayer = new MediaPlayer();
创建实例一次(例如,在onCreate
中),只需更改onClick
方法中的网址。
答案 1 :(得分:0)
您必须在适配器构造函数上只创建一个MediaPlayer
对象的实例,如下所示:
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)
{
soundcloudList.get(position).setPlaying(true);
try
{
holder.img1.setVisibility(View.INVISIBLE);
holder.img2.setVisibility(View.VISIBLE);
if(mMediaPlayer.isPlaying())
{
mMediaPlayer.seekTo(0);
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)
{
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)
{
soundcloudList.get(position).setPlaying(false);
holder.img1.setVisibility(View.VISIBLE);
holder.img2.setVisibility(View.INVISIBLE);
mMediaPlayer.seekTo(0);
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;
}
}