我正在做一个包含音频文件和照片的应用程序。(类似于字典)我想为“A”照片播放“A”声音。单击按钮时会播放声音(为了简单起见)。应用程序运行顺利,但单击按钮时也不会播放声音(单击 30-40 次)。我正在分享我的代码部分。
...
public MediaPlayer mp;
public Context con;
public LayoutInflater lf;
//Sounds and photos
public Integer[] audios = {
R.raw.a,
R.raw.b,
R.raw.c,
R.raw.d };
public Integer[] cards = {
R.drawable.a,
R.drawable.b,
R.drawable.c,
R.drawable.d };
public Object instantiateItem(@NonNull ViewGroup container, final int position) {
lf = (LayoutInflater) con.getSystemService(con.LAYOUT_INFLATER_SERVICE);
View view = lf.inflate(R.layout.slide_layout,container,false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
ImageButton btn;
btn=(ImageButton) view.findViewById(R.id.imageButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mp = MediaPlayer.create(con,audios[position]);
mp.start();
}
});
imageView.setImageResource(cards[position]);
ViewPager vp = (ViewPager) container;
vp.addView(view,0);
return view;
}
...