Adding generic button click sound - java

I'm creating a game app in android studio and need to add a generic click sound when any button is pressed on screen. I figure I can do this a couple different ways, someone said I should use this code
view.playSoundEffect(android.view.SoundEffectConstants.CLICK);
But I am having trouble implementing this because it seems to only work in onTouchListener when I need it for onClickListener... So, my next idea is a can create a MediaPlayer object, and with that object add a resource from the raw directory, with a 1 second audio clip of a click sound (but I don't know where to find this sound). I would then set this MediaPlayer object to play whenever a button is clicked. How can I get this click sound to play when a button is pressed?

Trick is to use Audiomanager to play the sound. Follow the below steps:
define an AudioManager in the Activity class
AudioManager audioManager;
Initialise in onCreate.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
In the click method, use the audioManager to play the sound.
public void play(View view) {
audioManager.playSoundEffect(SoundEffectConstants.CLICK,1.0f);
}
Note that the volume is given as 1.0f if you don't specify the volume, you won't hear any sound. Hope this helps!

Related

Why does Audio stop playing in app?

I have a program that plays a sound every time the user does an action. But after some time none of the audio works.
Sample code :
public void bPressed(View view) {
MediaPlayer mp = MediaPlayer.create(activity.this, R.raw.audio);
mp.start();
}
You need to use mp.release(); When you are done with MediaPlayer:
As an example, consider the problems that could happen if you forgot to release the MediaPlayer when your activity is stopped, but create a new one when the activity starts again. As you may know, when the user changes the screen orientation (or changes the device configuration in another way), the system handles that by restarting the activity (by default), so you might quickly consume all of the system resources as the user rotates the device back and forth between portrait and landscape, because at each orientation change, you create a new MediaPlayer that you never release. (For more information about runtime restarts, see Handling Runtime Changes.)
http://developer.android.com/intl/es/guide/topics/media/mediaplayer.html
add mp.release(); for example in your onStop()

Simple way to implement a mute button in Android app

I am looking for a simple way to implement a mute button.
Obviously I could have a toggle button, mute_button and in every instance where a sound is played I could check:
if(mute_button.isChecked()){
//dont play
}else{
//play the sound
}
But I have a lot of instances that I would need to sort through and add this in. Is there a simpler way of something along the lines of:
mute_button.setOnClickListener() ...
onClick
if(isChecked){
//set entire application to mute mode
}else{
//keep default application sound settings
}
So, really I am looking for a "higher level" or application wide mute, rather than checking if a button is checked every single time a sound may play.
You can try by setting the media volume to zero
MediaPlayer media = MediaPlayer.create(this, R.raw.backgroundSound);
//media.setVolume(leftVolume , rightVolume);
media.setVolume(0 , 0);
For soundpool you can try like this:
soundpool.setVolume(streamID, 0f, 0f);

Open mp3 files in android default media player on the same activity

I am developing an Android application in which I am showing a ListView of mp3 files from the sd card.
Now what I want is that when user clicks on any of the mp3 files it should start playing there on the same Activity. The code which I am using presently is not working for me to play audio files.
My code:
Uri uri = Uri.parse("/sdcard/music/sample.mp3");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
But this is something that I want. This is a screen Shot form android gingerbread
see this link here is a good example of getting all the music files and show in list and play when you click any item...
Display MusicList and play on itemClick
Another link
I suppose you should use another approach in case you want audio played in the same activity. I'd better initialize MediaPlayer object in this activity or as a background service.
In case of any questions feel free to ask.
UPDATE 1
In general case as I said you either need the MediaPlayer object in Activity or Music Service.
Now lets talk about controls.
You can simply put into your layout the block with controls and show/hide it when needed. MediaPlayer provides several convenient callbacks, so you will be able to update progressbar.
So, sum up:
Include controls into layout.
Include MediaPlayer into activity or make it work as a Service.
Bond controls to player.
???
PROFIT

How do I make a music player activity keep running?

I have a music player Activity in my app.
The problem is that, if the user switch to other Activity inside my app, the music keeps playing, but if later he returns to the music player Activity, everything resets(song's title, duration...) except the music itself.
Is there a way to make an activity keep running?(not by using a service).
Take a look here it might help you.
How to mute and unmute it on the onPause and onResume
Use the onPause() and onResume() for that.

Stop Media Player After starting new intent

Here's my problem: I start a media player, and if I start a new intent, or leave the app and go back to it..
mp.stop;
..wont work (mp is my media player variable).
is there anyway to fix this?
Bonus to whoever can tell me how to link it up to a widget.
You always must call mp.release() in your Activity's onDestroy(). It is also recommended to at least call mp.pause() in on onPause() Activity's method.
If you want playback to continue outside of Activity lifecycle (after onPause()/onStop()/onDestroy()) you have to create Service and play audio content from there.

Categories

Resources