ImageButton that plays sounds...

All your problems with Audio, Video and Images.

ImageButton that plays sounds...

Postby HiBoost » Thu Jun 10, 2010 10:49 pm

Android newbie here, this is my first application... should be very simple but I just want to make sure I'm starting on the right foot and not building something inelegant...

I have an ImageButton that should only play a sound while it is depressed. As soon as you lift your finger up the sound stops. Each time you touch it the playback starts from the beginning of the sound.

I have it working but sometimes I get MediaPlayer errors if you click it quickly and for short durations. I could install an error handler that resets the sound and data source and re-prepares it, but that seems wasteful if I just need different logic to avoid the errors in the first place.

Here is what I have for the button's onTouch handler, from onCreate():

Code: Select all
       
        mSoundBeep = MediaPlayer.create(appCtx, R.raw.beep);
        mImgBtnBeep = (ImageButton)findViewById(R.id.beepbutton);


        mImgBtnBeep.setOnTouchListener(new OnTouchListener() {
           public boolean onTouch(View v, MotionEvent event) {
              
              switch (event.getAction())
              {
                 case MotionEvent.ACTION_DOWN:
                    mSoundBeep.start();
                    break;
                    
                 case MotionEvent.ACTION_UP:
                    if (mSoundBeep.isPlaying())
                    {
                       mSoundBeep.stop();
                       try {
                          mSoundBeep.prepare();
                          } catch (IllegalStateException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                          } catch (IOException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                          }
                    }
                    break;              
              }
              
              return false;
           }
        });


Another thing is that originally I was pausing the sound instead of stopping it. But the seekTo call is asynchronous so I was ending up with audible clicks at the beginning of the subsequent presses of the button. There may be a better way to do this altogether so I'm wide open for suggestions.

Thanks!
Jeff
HiBoost
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 10, 2010 10:34 pm

Top

Re: ImageButton that plays sounds...

Postby dinamic » Fri Jun 11, 2010 6:23 am

I haven't used MediaPlayer object, but im using SoundPool in a game the way you
want the sound to appear so take a look at.. android.media.SoundPool


/H
dinamic
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 10, 2010 2:31 pm

Re: ImageButton that plays sounds...

Postby HiBoost » Fri Jun 11, 2010 6:27 am

I looked at SoundPool at first but I didn't see a way to get notified of when the sound finished playing... I have other buttons where you touch them and they stay depressed on their own until the sound completes and then they pop back up. This is working fine with MediaPlayer currently. I supposed I could do the one button type with MediaPlayer and the other button type with SoundPool, but I was hoping for a more uniform solution.
HiBoost
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 10, 2010 10:34 pm

Re: ImageButton that plays sounds...

Postby dinamic » Fri Jun 11, 2010 6:41 am

Aah ok, i missed that.. for such a function i would use AudioTrack, wrap my samples into a class an implement a getSamples(int frames) which also loops the sample (if it's ofcourse is a looping one..)

^ thats because i haven't used MediaPlayer... MediaPlayer might be a better choice ? :)

Take a look at my Loopist source (http://code.google.com/p/loopist/) the sourcefile LoopistEngine.java and look up the class LoopistEngine.Playback for source using audiotrack...

/h
dinamic
Freshman
Freshman
 
Posts: 8
Joined: Thu Jun 10, 2010 2:31 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Google [Bot] and 13 guests