gpt4 book ai didi

android - 使用 MediaPlayer 类播放音频(仅第一次播放轨道)

转载 作者:行者123 更新时间:2023-12-03 00:16:46 24 4
gpt4 key购买 nike

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

如果我把 mMediaPlayer=new MediaPlayer(); holder.img1.setOnClickListener 的 onClick() 方法内部上面的问题确实得到了解决,但是然后单击多个播放按钮将开始同时播放多个轨道,这是我不想要的..
这是代码.. holder.img1用于播放图标和 holder.img2用于停止图标

声音云适配器

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

}

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

}

最佳答案

好像您在调用 prepareAsync多次,这应该会引发错误。

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

mMediaPlayer.reset();

关于android - 使用 MediaPlayer 类播放音频(仅第一次播放轨道),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com