Switching between earpiece and speakerphone on button press - android

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.

Related

Android Mediaplayer setVolume not work when I press volume button on moblie

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.

How to set Videoview Volume Programmatically?

I have 4 Videoview which is created dynamically. I want to set a custom volume for only one video which is coming from the server, the remaining three videos will be mute.
I want to set a custom volume for particular videos I tried below code it's not working
vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
//mp.setVolume(100f, 100f);
//mp.setLooping(true);
vid1.enableSound(20,mp); //here i will set music sound dynamically
vid1.start();
playingvideo1 = true;
}
});
//startTimeForContent = dateFormatForContent.format(new Date());
vid1.setOnErrorListener(mOnErrorListener2);
playBackfunction1();
}
public void enableSound(int sound, MediaPlayer mp){
Float f = Float.valueOf(sound);
Log.e("checkingsounds","&&&&& "+f);
mp.setVolume(f,f);
mp.setLooping(true);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND);
}
When I give volume 0 it is working...but when change 10,20,30 video is playing full sound....
I already research below:
sound volume not working on Android
Using SeekBar to Control Volume in android?
How to increase and decrease volume in android
After researching some hours I got a solution
You need to set Volume 0 to 15
vid1.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
{
public void onPrepared(MediaPlayer mp)
{
//mp.setVolume(100f, 100f);
//mp.setLooping(true);
vid1.enableSound(10,mp); //set Volume 0 to 15.
vid1.start();
playingvideo1 = true;
}
});
//startTimeForContent = dateFormatForContent.format(new Date());
vid1.setOnErrorListener(mOnErrorListener2);
playBackfunction1();
}
public void enableSound(int sound, MediaPlayer mp){
Float f = Float.valueOf(sound);
Log.e("checkingsounds","&&&&& "+f);
mp.setVolume(f,f);
mp.setLooping(true);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); //Max Volume 15
audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); //this will return current volume.
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, sound, AudioManager.FLAG_PLAY_SOUND); //here you can set custom volume.
}

Android system audio not working correctly after using setSpeakerPhoneOn of AudioManager

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?

MediaPlayer.start() not play sounds in s3

I added in my app a simple feature that plays a simple sound when the button is clicked. It works fine with my Nexus 7 AVD, but when i run the code in Galaxy s3, it not displays any sound. The code is:
private void turnOnFlash() {
if(!isFlashOn) {
if(camera == null || parameters == null) {
return;
}
}
this.playSound();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
isFlashOn = true;
}
and the playSound method:
private void playSound() {
mediaPlayer = MediaPlayer.create(this, R.raw.bar);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_NOTIFICATION);
mediaPlayer.start(); //working on AVD, but do not work in S3.
}
Can someone help me fix this problem ? Thanks.
The problem is that the device sets the media volume to zero,
You can set media volume programmatically using this snippet of code :
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
This should help..

Playing a WAV File Multiple Times

I need to have the same short wav file (1 second) play each time the user presses a single button. I have the following code that works for about 30 clicks and then the app "Force closes" on the device. I think what is going on is that new instances of media player are being created and then the instances build up (approx 30 clicks) and the app crashes. So I then added the "release" instance in the hope that on button-click the wav would play and then the mediaplayer would be released. However, it doesnt work that way. With the mp.release() no sound is played possibly becaue the medaiplayer gets released too soon for the user to hear the sound?
Anyone have a good tip to help me getting this to work? Thank you all so much.
Button button2 = (Button) findViewById(R.id.button10);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.clicker);
mp.start();
mp.release();
Its simple just create the MediaPlayer once, and play it over time.
private MediaPlayer mp;
public void onClick(View v) {
// Perform action on click
if (mp == null){
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicker);
}
mp.start();
}
Why use MediaPlayer, when SoundPool is better suited for small audio files? As a base you could use this:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 70);
final HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();
final int soundID = 4;
soundPoolMap.put(soundID, soundPool.load(this, R.raw.wav_sound, 4));
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener()
{
public void onLoadComplete(SoundPool soundPool, int sampleId, int status)
{
if (sampleId == 4)
{
soundPool.play(4, 50, 50, 1, 0, 1f);
}
}
});
Here is the simple solution that will work
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicker);
public void onClick(View v) {
// Perform action on click
if(mp.isPlaying())
{
mp.stop();
mp.reset();
mp.release();
}
mp = MediaPlayer.create(getApplicationContext(), R.raw.clicker);
mp.start();
This is will check if mediaplayer is already playing..If it already playing it will stop and release it and then initialize that mediaplayer(mp) object and start the mediaplayer.
If it is not playing it will execute the code after the if statement and the start the media player after initialzing the mediaplayer object(mp in this case)
To learn more about MediaPlayer study this http://developer.android.com/reference/android/media/MediaPlayer.html. Study the Mediaplayer life cycle

Categories

Resources