Play specific sound when specific button is pressed - android

I'm really new to Android Studio, and am trying to make a simple app which plays specific sounds when a specific button is pressed. I have looked up many tutorials on YouTube, stack and other websites, but all of them seem to either give me a lot of errors or are too hard for me too understand (as I am too unexperienced).
All I have now are a lot of buttons on the screen which do nothing.
So can someone show me how to make something like this:
When button 1 is pressed, sound 1 plays, and when button 2 is pressed, sound 2 plays. (And that has to go on like that for about 20/30 buttons and sounds)
Thanks in advance,
-Spickle

Create a MediaPlayer mp object and use this in your onClick methods:
mp = MediaPlayer.create(context, R.raw.SoundForSpecificButton);
mp.start();
Then create a raw folder inside a res folder and put there your sounds.
Here is good documentation about this:
http://developer.android.com/guide/topics/media/mediaplayer.html#mediaplayer
If you are having problems please put your code here for review.

If you want to audio for a very short duration for example a 'click' sound when you press a button look for SoundPool class in android.
Play sound using soundpool example
If you want to stream a bigger audio file you will have to implement MediaPlayer.

Related

mediaplayer.pause() not working

I am playing audios in a gridview.I am having issue that once the audio starts it not pausing or stopping.On clicking on pause button I see the pause image changing to play but still the audio keeps playing.If i click on another item's play button then multiple audio plays.I failed to understand why mp.pause() not working?
You are creating a MediaPlayer for each item of your GridView. If you scroll, it will mess up the references to your view and you won't be able to handle your MediaPlayer. You should use only one MediaPlayer, like the one you already have declared mMediaPlayer. Try using it instead of creating one for each item.

how to combine images with mp3 to look like video in android

I am trying to combine images in view-pager with mp3 audio file.
I have 26 images of ABC alphabets in view-pager, and a ABC song in mp3 audio file. I want that when i click start button mp3 song file play and alphabets images in view-pager changes.
I had searched a lot but didn't find any solution that i can understand. Can some one give me code of that?
Please help me.
Thanks
I think the best way to solve your problem would be to create a timer and to simply switch between the bitmap at regular intervals.
When the start button is clicked, just launch the timer using an handler, and inside the timer event, add your logic to switch images.

If using MediaPlayer to play Button click sound, what happens to default click sound?

For the record, I am not asking how to play a sound programmatically with the MediaPlayer. I know how to do that quite well.
I first searched how to replace the default click sound of a Button in the XML. But I have not found out if that is possible. Most answers suggest to use the MediaPlayer to play a sound effect somewhere in/from the onClick() event of the Button, so I assume that's the best way to go?
If I use the MediaPlayer to play a sound when a Button is clicked, do I have to disable the default click sound as well, or will both play, or will Android just know to ignore the default click sound? Should I call setSoundEffectsEnabled(false) on the Button before I play my own sound?
It seems very strange to me that I can't just replace the default click sound of a Button (is it possible to do that?)
Just set android:soundEffectsEnabled=false in your theme defined into your res/styles.xml
or programatically:
myButton.setSoundEffectsEnabled(false);
you may wish to check this link as well: Playing sound effect (CLICK/NAVIGATION_RIGHT) for button clicks - Android
seems that sound effects are kept as android.view.SoundEffectConstants.<value>
correspondingly, there might be a way of adding your own custom sound
it is all about passing the value of sound effect you'd like to hear, into <view variable>.playSoundEffect(<sound effect value>); so you may try to add your sound into res and call that method with its R value

Play audio on button click in viewpager efficiently in android

In my application I am displaying around 30 animal images using viewpager. each page has full screen image and play sound button. Clicking on button will play displayed animals sound. So there are 30 different sound files in raw folder. how to play audio effectively when user press button? What to use mediaplayer or soundpool? and how to use it effectively? Please give me solution.
Keep the song files in the raw folder and then on the button's onClick method execute the songs play.
You can use a switch case for different id's of button.

How do I set spinning ProgressBar to Media?

I'm creating a soundboard application. Im new to developing but I have my whole app running perfectly exept for one last thing. I have it so I have multiple buttons and each button plays a different sound. I want to have a spinning progressbar on my button while the sound is playing, but then dissappear when the sound is done. How would I do this easily?
so i'm not sure how you're playing your sounds, whether its just one line of code that actually PLAYS the sound. In my opinion, you have two options
If your method returns something once the sound stops playing, I'd do something like
while (myapp.playSound() != -1){ //implement spin bar }
Or you can use AsyncTask -- http://developer.android.com/reference/android/os/AsyncTask.html
AsyncTask would probably be your best bet
Add a OnCompletionListener to the mediaplayer, so when you play the sound (or click the button) start the progress bar, and when the track is complete remove/stop it.

Categories

Resources