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);
Related
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!
I am developing a Video App, and I need to know whether is any BGM playing?
I tried to use AudioManager.isMusicActive(), but it didn't work, because when I play my video, this method always returns true(I guess it detects my app).
In a word, I need to know BGM playing in other App, not Mine.
Finally, I've solved this problem by detecting other app's music every time before my music is going to start.
Because befeore your music is playing, if AudioManager.isMusicActive() returns true, then it means BGM is playing.
Something like this,
public void start() {
AudioManager am = (AudioManager) weak_context.get().getSystemService(Context.AUDIO_SERVICE);
boolean check = am.isMusicActive();
playMyMusic();
}
I am working on a Music player. I want to add default Equalizer ( Sound effect) in my music player app. I successfully add it to my player app but sound effect not working on the media player of my android app. No effect when I change Jazz, Pop, Rock, Folk etc.. how to attach it which current media player in our Android app.
I open it with Intent
val intent = Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL)
if (intent.resolveActivity(packageManager) != null)
{
startActivityForResult(intent, 1234)
}
This link has quiet a lot of information on how to create an equalizer from scratch
https://www.101apps.co.za/articles/perfect-sound-using-the-equalizer-effect-a-tutorial.html
If this is not what you are looking for, perhaps a bit more info could be helpful.
To clear the basics: make sure you are creating the equalizer and attaching it to the media player. Also make sure you are setting up the UI by calling the setupEqualizerFxAndUI.
Hope this helps.
I was wondering if releasing my media player before I play a random sound is bad practice:
So I don't usually deal with media output too much, but I am making a simple app that plays a random sound every time a button is clicked (sounds [] is an array filled with raw media files)
public void onClick(View v){
if(mediaplayer != null){
mediaplayer.release();
}
mediaplayer = MediaPlayer.create(this, sounds[randomNum])
mediaplayer.start();
}
So my question is, would releasing my media player every time before creation be considered good/bad practice? Would there be any better way to do this, as releasing and re-initializing the MediaPlayer object seems like it would consume resources...
Thanks,
Ruchir
You typically use release() when you no longer want to use a MediaPlayer any more. Once you call that, it can never be used again. It effectively destroys the native components that back its functionality.
If you do release, you will have to prepare the media all over again the next time you want to play it. This can be a time consuming process. If you want a sound to play responsively to a button press, you probably don't want to have to prepare it each time.
I want to create a music loop pad, something like this: https://www.youtube.com/watch?v=fwBPYwiYp-Y
Right now, i am using MediaPlayer to play the sounds. I have a global variable that stores the current time (position) of the first audio that is playing, and when i press another button the corresponding audio file starts playing at the position given by the global variable.
My proble is: the sounds are playing out of sync.
can you give me some help? How can i sync the sounds? like this app https://www.youtube.com/watch?v=fwBPYwiYp-Y
Can you point me some info or tutorials about how to create a loop pad?
Thanks
Richardd
Programming something like this is harder than you think. Here are two things you must do to have the sounds play in sync:
Make sure all you sounds are at the same tempo, and are cropped properly - that means that the starting and ending of the sound clip must be exactly on beat - another solution would be to store the starting and ending time for each clip (very easy with OOP)
Use a timer to make sure the sound starts playing on beat. For example, if you wanted to make your music run at 120 bpm, you would need to have your timer fire every 500 ms.
Here is some code (only concept, won't compile):
Timer beatTimer;
boolean isDrumLoopEnabled;
boolean isDrumLoopCurrentlyPlaying;
Sound drumloop;
public void startMusic(){
//prepare timer
beatTimer=new Timer();
beatTimer.interval=500;//for 120 bpm
beatTimer.onTick+=onTickHandler;
isDrumLoopPlaying=false;
isDrumLoopCurrentlyPlaying=false;
//load sounds
Sound drumloop=new Sound("/storage/emulated/drumloop.ogg");
//start music
beatTiemr.start();
}
//this method will run on every beat:
public void onTickHandler(){
if(isDrumLoopEnabled){
//make sure drum loop isn't already playing
if(!isDrumLoopCurrentlyPlaying){
//begin looping drumloop in background
drumloop.startloop();
isDrumLoopCurrentlyPlaying=true;
}
}else{
if(isDrumLoopCurrentlyPlaying){
//stop playing drumloop
drumloop.stopPlaying();
}
}
}
//when the button is pressed...
public void drumloop_buttonpressed(){
//this will toggle whether to play the drum loop:
isDrumLoopEnabled=!isDrumLoopEnabled;
}