gpt4 book ai didi

Android Recyclerview MediaPlayer 上一个Item声音停止,点击新一个后释放

转载 作者:搜寻专家 更新时间:2023-11-01 08:34:52 27 4
gpt4 key购买 nike

在我的应用程序中,我有 recyclerview 项目,当我单击它时,每个项目都会播放声音。问题是当我点击几个项目时,它们都会同时播放声音直到完成。假设我点击了 10 个项目,所有 10 个同时播放声音,一段时间后应用程序崩溃了,我得到了这个错误:E/MediaPlayer: error (1, -19)。我通过使用以下代码实现自动发布来避免应用程序崩溃:

    mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();

}
});

但这不是我想要的,同时听到几个声音很烦人。我想要的是在单击新声音时停止以前的声音,我尝试实现媒体播放器停止和释放,但没有成功。

这个是google play里面的app,大家可以自行查看:https://play.google.com/store/apps/details?id=org.pero.androidd.animalSoundsQuiz

这是我播放声音的适配器代码。在此代码中有部分 MediaPlayer.OnCompletionListener() ,我在声音完成后释放声音以避免 E/MediaPlayer: error (1, -19) 错误,但这不是我想要的应用程序.单击新声音后,我想停止以前的声音。

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pero.androidd.animalsoundsforchildren.R;

import java.util.List;


public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

private List<Zivotinje> mZivotinje;


@Override
public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);

// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}

@Override
public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {


Zivotinje zivotinjeIme = mZivotinje.get(position);

// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(zivotinjeIme.getmAnimal_name());

//THIS IS WHERE I HANDALE IMAGE VIEW. Using glide for images
//Dohvacamo context svakog views
ImageView slika_source = viewHolder.imageView;
Glide.with(slika_source.getContext())
.load((Integer) zivotinjeIme.getmAnimal_slika())
.into(slika_source);


viewHolder.setItem(mZivotinje.get(position));

}


@Override
public int getItemCount() {
return mZivotinje.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;
public TextView nameTextView;
private Zivotinje mItem;



public void setItem(Zivotinje item) {
this.mItem = item;
}



public ViewHolder(final View itemView) {
super(itemView);

nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
imageView = (ImageView) itemView.findViewById(R.id.slika_id);


//Handdaling clicks
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

final MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
mediaPlayer.start();


//releace media player after sound finish
// BUT I DO NOT WANT TIS, I WANT TO STOP PREVIOUS SOUND WHEN I CLICK NEW ONE
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();

}
});
}
});

}


}


public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}


}

编辑编辑编辑编辑编辑编辑:

我找到了解决方案,我不知道这是不是最聪明的方法,但它对我来说很好:

我所做的是我制作了静态媒体播放器变量,这样我就可以从 ViewHolder 访问媒体播放器并在我单击新播放器时停止之前的声音。希望对您有所帮助。

完整代码如下:

public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

private static MediaPlayer mediaPlayer; //THIS IS THE CHANGE I DONE

@Override
public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);

// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);

// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}

@Override
public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {


Zivotinje zivotinjeIme = mZivotinje.get(position);

// Set item views based on the data model
TextView textView = viewHolder.nameTextView;
textView.setText(zivotinjeIme.getmAnimal_name());

//THIS IS WHERE I HANDALE IMAGE VIEW. Using glide for images
ImageView slika_source = viewHolder.imageView;
Glide.with(slika_source.getContext())
.load((Integer) zivotinjeIme.getmAnimal_slika())
.into(slika_source);


viewHolder.setItem(mZivotinje.get(position));

}


@Override
public int getItemCount() {
return mZivotinje.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

public ImageView imageView;
public TextView nameTextView;
private Zivotinje mItem;


public void setItem(Zivotinje item) {
this.mItem = item;
}


public ViewHolder(final View itemView) {
super(itemView);

nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
imageView = (ImageView) itemView.findViewById(R.id.slika_id);


//handaling clicks
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {


if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}


mediaPlayer = MediaPlayer.create(v.getContext(), mItem.getmSound());
mediaPlayer.start();

}
});

}


}

private List<Zivotinje> mZivotinje;

public ZivotinjeAdapter(List<Zivotinje> animals) {
mZivotinje = animals;
}


}

最佳答案

您首先必须检查是否有正在播放的声音,如果有,您应该在用户决定开始播放新声音时停止播放。

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaplayer.isPlaying ()) {
if (mediaplayer != null){
mediaplayer.stop ();
}
} else {
if (mediaplayer != null) {
mediaplayer.start ();
}
}
}
});

setOnCompletionListner 中,如果用户收听了整个声音 fragment ,您将释放声音。

        mediaplayer. setOnCompletionListner (new MediaPlayer.OnCompletionListner () {
public void OnCompletion (MediaPlayer mediaplayer) {
mediaplayer.release ();
}
});

关于Android Recyclerview MediaPlayer 上一个Item声音停止,点击新一个后释放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37644981/

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