I have a great list. Each list view item has a play button (image). Pressing this play button will play a small audio file.
I created an adapter for loading items in a list and implemented an onClick listener during playback.
When I run the application, it loads the list correctly, but there are some problems with the audio. When I play several (for example, 10-12) elements many times (for example, 100), this will not create problems, everything will work smoothly.
But if I play more items like 30-40 items in the list. It stops loading audio files for subsequent items. For elements, it worked, and it will continue to work, as new elements do not work.

public class WordListAdapter extends BaseAdapter{
private ArrayList<Word> wordArrayList;
private Activity activity;
private static LayoutInflater inflater=null;
MediaPlayer mp;
public WordListAdapter(Activity activity, ArrayList<Word> wordArrayList) {
this.activity = activity;
this.wordArrayList = wordArrayList;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return wordArrayList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
View view = convertView;
if(convertView == null) {
view = inflater.inflate(R.layout.item_word, null);
}
TextView word1 = (TextView) view.findViewById(R.id.word1);
TextView word2 = (TextView) view.findViewById(R.id.word2);
ImageView imageWord = (ImageView) view.findViewById(R.id.imageWord);
ImageView playWord = (ImageView) view.findViewById(R.id.playWord);
word1.setText(wordArrayList.get(position).lang1);
word2.setText(wordArrayList.get(position).lang2);
if(wordArrayList.get(position).pic == 0) {
imageWord.setVisibility(GONE);
}
else {
imageWord.setImageResource(wordArrayList.get(position).pic);
}
playWord.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp = MediaPlayer.create(activity, wordArrayList.get(position).audio);
mp.start();
}
});
return view;
}
}
I think the problem is that I created too many Media Player objects.
How to fix it?
source
share