I am using AudioManager to mute unmute on Asus memopad 10. I can mute the device but i can't unmute it back. If i use some other application which uses microphone it also doesn't work there. Then i have to restart the device than it start working.
Edti : Mute code
_audioManager.setMicrophoneMute(mute);
Suppose this is the scenario, where you set the microphone to mute using a boolean and then use a button on screen to unmute it.
Boolean mute = true;
final AudioManager myAudio;
myAudio=(AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
myAudio.setMicrophoneMute(mute);
Button unmute = (Button)findViewById(R.id.unm);
unmute.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myAudio.setMicrophoneMute(false);
}
});
Another scenario where you use the same button to mute or unmute :
public Boolean mute = true;
final AudioManager myAudio;
myAudio=(AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
Button unmute = (Button)findViewById(R.id.unm);
unmute.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myAudio.setMicrophoneMute(mute);
if(mute=true){
mute=false;
}
else{
mute=true;
}
}
});
Now everytime,the button
Related
I use MediaPlayer setVolume function to control the audio track volume, that is ok. but when I set Volume 0, then press the volume button, the volume is not 0. Why? please help me.
The volume you are changing is the volume of that mediaplayer instance not the device. To change device volume you need to create the AudioManager Object and use it to change device volume. Example :
AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
Button upButton = (Button) findViewById(R.id.upButton);
upButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//To increase media player volume
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
}
});
Button downButton = (Button) findViewById(R.id.downButton);
downButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//To decrease media player volume
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
}
});
You can look at this complete answer to override volume button from your device.
I'm trying to make a application that switches the audio play between the speaker and earpiece.
I'm using The following code:
public boolean setAudioMode(String mode) {
AudioManager audioManager =
(AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
if (mode.equals("earpiece")) {
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(false);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
return true;
} else if (mode.equals("speaker")) {
audioManager.setMode(AudioManager.STREAM_MUSIC);
audioManager.setSpeakerphoneOn(true);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
return true;
}
return false;
}
This code is working and I can change the audio output correctly.
But after I close my application, my phone does not play any sound through my headphones and the volume control sticks to in call volume. My phone only go back to normal after a full restart.
What can I do to restore the phone audio? Or is there a better way to switch the audio output that does not make this problem?
I need my in-app volume seekbar to change the volume of the audio in my app only. I need it to be totally separate from the main volume of the device. Everything I find changes the device volume when I change the slider in my app.
I want users to have the option of playing audio in my app at a low volume, but maybe still have their device turned up so phone calls and alarms and such could still be at their normal high volume.
How can I make a seekbar in my app only control audio in my app and not bother other device volumes? Here is the code I am using:
Called in onCreate method:
VolumeControls();
Outside of that:
private void VolumeControls() {
final TextView txtView = (TextView)findViewById(R.id.textView1);
SeekBar seekBar = (SeekBar)findViewById(R.id.sbVolume);
final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
seekBar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
txtView.setText(String.valueOf(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC)));
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar arg0){
}
public void onStartTrackingTouch(SeekBar arg0){
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2)
{
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
progress, 0);
txtView.setText(String.valueOf(progress));
}
});
}
I'm currently developing application and i want to control playing sound file on different headphones sides ex the first time on the left side and second time on the right side is there any way to achieve this ?
Yes there is a way. MediaPlayer.setVolume(float leftVolume,float rightVolume).
In the following snippet we're playing an .mp3 file contained in assets folder(note if you have multiple files in the folder you should check this answer). By pressing one of the Button objects the song is played only out of the left or the right headphone :
MediaPlayer AudioObj = new MediaPlayer();
AudioObj.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mediaPlayer) {
findViewById(R.id.progressBar).setVisibility(View.INVISIBLE);
Button btnl = (Button) findViewById(R.id.btnPlayleft);
Button btnr = (Button) findViewById(R.id.btnPlayright);
btnl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.setVolume(1, 0);
mediaPlayer.start();
}
});
btnr.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mediaPlayer.setVolume(0, 1);
mediaPlayer.start();
}
});
}
});
AudioObj.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
AssetFileDescriptor afd = getAssets().openFd("audio.mp3");
AudioObj.setDataSource(afd.getFileDescriptor());
}catch (IOException e){}
AudioObj.prepareAsync();
P.S.
The audio file must be stereo.
Whether you want to check if the headset are plugged or not before playing the audio in order to prompt a message or do something else :
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
if(!audioManager.isSpeakerphoneOn()){
//prompt a message or do something else
}
I am trying to play audio from both the speakerphone and earpiece by having a button toggle between the two. The problem is that I am trying to default the audio to play from the earpiece, but nothing comes out. Then when I press the button to toggle to speakerphone, still no audio plays. I am playing from a local raw file.
I have android.permission.MODIFY_AUDIO_SETTINGS in the Manifest as well.
Here is my code:
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
context = getActivity().getBaseContext();
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
am.setBluetoothScoOn(true);
speakerON = false;
}
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.buttonSpeaker:
if(!speakerON)//speaker off
{
speakerON = true;
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
am.setBluetoothScoOn(false);
speaker.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_silent_mode_off, 0, 0, 0);
}
else
{
speakerON = false;
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
am.setBluetoothScoOn(true);
speaker.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_lock_silent_mode, 0, 0, 0);
}
break;
}
}
Here is how I am setting up the MediaPlayer:
mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.test_message);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.start();
It turns out that I had set the mode wrong.
Here is the updated media player:
mediaPlayer = MediaPlayer.create(getActivity().getBaseContext(), R.raw.test_message);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.start();
And then I set the mode for the audio manager to :
context = getActivity().getBaseContext();
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
And then it worked. So make sure that the media player and audio manager are in the same mode.