简体   繁体   English

Android中的声音文件播放是否有限制?

[英]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. 我正在用音频文件和照片做一个应用程序。(字典式)我想为“A”照片播放“A”声。 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). 单击按钮时播放声音(为了简单起见)。应用程序运行顺畅但是当按钮点击时(30-40次点击)声音也不会播放。 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. 不要为您播放的每种声音创建新的MediaPlayer。 Create one, and reuse it for each time you want to play a sound. 创建一个,并在每次播放声音时重复使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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