Play sound with button repeatedly

All your problems with Audio, Video and Images.

Play sound with button repeatedly

Postby Tony_GPR » Sat Apr 09, 2011 3:51 am

Hi, one month ago I made an app with AppInventor, this app consist on an image of a gun with one giant transparent button in top.. when I click the button the firing sound is played.

Now I'm rewriting it with eclipse to learn, and I have a problem that i can't solve, the problem ocurrs when i click the button quickly and repeatedly. I searched google extensively and I found a lot of "solutions" but any works for me...

In the App I made with AppInventor i don't have any problem, although I press the button 100 times in a row, which means that there must be some way to make it work.. (with the code that is behind AppInventor eg)

I'll be very grateful if someone can help me solve the problem. This is my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Test extends Activity implements OnClickListener{
  2.  
  3.     MediaPlayer t1 = null;
  4.  
  5.     public void onCreate(Bundle savedInstanceState) {
  6.        
  7.         super.onCreate(savedInstanceState);
  8.         setContentView(R.layout.main);
  9.        
  10.         Button trigger= (Button) findViewById(R.id.BG);
  11.        
  12.         trigger.setOnClickListener (this);
  13.        
  14.     } //End onCreate..
  15.    
  16.     public void onClick(View v) {
  17.         switch(v.getId()){
  18.         case R.id.BG:
  19.                 t1 = MediaPlayer.create(Test.this, R.raw.t);
  20.                 t1.start();
  21.                 t1.setOnCompletionListener(new OnCompletionListener() {
  22.                     @Override
  23.                     public void onCompletion(MediaPlayer t1) {
  24.                         t1.release();
  25.                     }
  26.                 });
  27.                 break;
  28.         }
  29.     }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Tony_GPR
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 09, 2011 3:15 am

Top

Re: Play sound with button repeatedly

Postby Tony_GPR » Mon Apr 11, 2011 4:50 pm

I solved the problem using SoundPool instead of MediaPlayer

Thanks to rbgrn for his Code Snippet
Tony_GPR
Freshman
Freshman
 
Posts: 3
Joined: Sat Apr 09, 2011 3:15 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 12 guests