gpt4 book ai didi

android - 应用程序第二次在 ListView 的适配器中播放音频时崩溃

转载 作者:行者123 更新时间:2023-11-29 15:57:59 25 4
gpt4 key购买 nike

我的类(class)由包含轨道列表的 ListView 组成。每个列表项由按钮播放和暂停组成。它应该在点击播放时播放轨道,在点击暂停时停止轨道。我没有使用不同的按钮....我是仅使用一个按钮,该按钮会在单击黑白播放和暂停时更改其文本并相应地运行。我面临以下问题-单击播放确实播放了轨道但是当我通过单击暂停停止轨道然后尝试播放任何轨道应用程序崩溃并显示以下错误显示非法状态异常

致命异常:main

java.lang.IllegalStateException
at android.media.MediaPlayer._setDataSource(Native Method)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:966)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:926)
at xyz.SoundCloudAdapter$1.onClick(SoundCloudAdapter.java:86)
at android.view.View.performClick(View.java:4091)
at android.view.View$PerformClick.run(View.java:17072)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5086)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
at dalvik.system.NativeStart.main(Native Method)

声音类适配器 公共(public)类 SoundCloudAdapter 扩展 ArrayAdapter 实现 MediaPlayer.OnPreparedListener{ ArrayList soundcloudList; LayoutInflater vi; 内部资源;

    public static MediaPlayer mMediaPlayer;
public SoundCloudAdapter(Context context, int resource, ArrayList<SoundCloud> objects)
{
super(context, resource, objects);
vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
soundcloudList = objects;
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this);
}


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

// convert view = design

View v = convertView;
if (v == null)
{
holder = new ViewHolder();
v = vi.inflate(Resource, null);

holder.textview = (TextView) v.findViewById(R.id.name);
holder.btn = (Button) v.findViewById(R.id.button);
holder.btn2 = (Button) v.findViewById(R.id.button2);
holder.textview2 = (TextView) v.findViewById(R.id.email);
v.setTag(holder);
}
else
{
holder = (ViewHolder) v.getTag();
}
holder.textview.setText(soundcloudList.get(position).getTitle());
holder.textview2.setText(soundcloudList.get(position).getId());

holder.btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

notifyDataSetChanged();

if (holder.btn.getText().toString().equalsIgnoreCase("Play"))
{
holder.btn.setText("Pause");


mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try
{
mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/"+soundcloudList.get(position).getId()+"/stream?client_id=xyz");
mMediaPlayer.prepareAsync();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();

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

}
notifyDataSetChanged();
}
else if (holder.btn.getText().toString().equalsIgnoreCase("Pause"))
{
holder.btn.setText("Play");
if(mMediaPlayer.isPlaying()) {
mMediaPlayer.stop();

}
mMediaPlayer.release();
notifyDataSetChanged();
}






}
});

return v;

}

@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}

static class ViewHolder
{

public TextView textview;
public Button btn;
public Button btn2;
public TextView textview2;


}


}

如果我声明

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnPreparedListener(this); inside onClick()

轨道不暂停

最佳答案

尝试在播放和暂停时处理每个列表项的 MediaPlayer 状态,也无需将 viewHolder 对象声明为 final,这可能会影响列表项状态。

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

@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)
{
ViewHolder holder;
if (convertView == null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.resource, null);
holder.textview = (TextView) convertView.findViewById(R.id.name);
holder.textview2 = (TextView) convertView.findViewById(R.id.email);
holder.btn = (Button) convertView.findViewById(R.id.button);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}

holder.textview.setText(soundcloudList.get(position).getTitle());
holder.textview2.setText(soundcloudList.get(position).getId());
holder.btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(final View v) {

if(((Button)v).getText().toString().equals("Play")){
try{
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/"+soundcloudList.get(position).getId()+"/stream?client_id=e13865f9debacb5f96375fdd96b7fa1b");
mMediaPlayer.prepareAsync();
mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
((Button)v).setText("Pause");
}
});
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mMediaPlayer.release();
mMediaPlayer = null;
((Button)v).setText("Play");
}
});
}catch (Exception e){
e.printStackTrace();
}
}else{
mMediaPlayer.release();
mMediaPlayer = null;
((Button)v).setText("Play");
}
notifyDataSetChanged();

}
});

return convertView;

}

static class ViewHolder
{
public TextView textview;
public TextView textview2;
public Button btn;
}

}

关于android - 应用程序第二次在 ListView 的适配器中播放音频时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26798716/

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