简体   繁体   中英

Is there a limit to the sound file playback in Android?

I'm doing an application with audio files and photos.(dictionary-like) I want to play "A" sound for "A" photo. Sound is played when the button clicked(for to be simple).Application works smoothly BUT the sound does not play when the button clicked too (30-40 clicks). I am sharing my code section.

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

Do not create a new MediaPlayer for each sound you play. Create one, and reuse it for each time you want to play a sound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM