Get MediaPlayer & AudioManager to play through earpiece instead of speaker? - android

Hey I'm trying to play a R.raw file thru my earpiece instead of the main phone speaker on my app but everyhting I throw at it isn't working. WIht my current code... it just plays very light static through the earpiece and not the R.raw I want.
Here is my code below and I set it in the manifest: <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(false);
am.setMode(AudioManager.MODE_IN_CALL);
MediaPlayer tellSecret = MediaPlayer.create(this, R.raw.secret);
tellSecret.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
tellSecret.start();
Any thoughts?

Try setting the Audio Manager and the Media Player modes to be AudioManager.STREAM_MUSIC.

do not use 'create' api for media player, instead use 'setDataSource' and 'prepare' apis seperately. Also call 'setAudioStreamType' api on mediaplayer instance with STREAM_VOICE_CALL before calling 'setDataSource'.
In the end after starting media player,
call setSpeakerphoneOn(false) on audio manager.

Related

How do I mute all sounds of my application?

I'm creating a quiz app just for learning, and I've got a problem.
In my mainactivity I have a media player. It plays music even going through other activities, and I have a mute button to stop the music. I also have a sound effect when I successfully hit the right answer (on a question which is in other activity). But I don't know how to mute this SFX along with the background music when I press the mute button. Any hints? ;)
If it has a way to mute my entire application will be so good. I found some ways to mute, but this way will mute the entire system. And it's
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
If you want to mute only the running apps sound, then you better do this:
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1); //Use your mediaplayer instance instead of this. Also maintain a single instance of mediaplayer for the entire app.
mp.start();
For Mute
mp.setvolume(0,0);
& Unmute or full volume
mp.setvolume(0,1);
Reference:
http://developer.android.com/reference/android/media/MediaPlayer.html#setVolume(float, float)
You can just use AudioManager.setStreamMute(). Feel free to use the code below.
//mute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
//unmute audio
AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
Try this. It may be of help to you.
AudioManager manager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
manager.setStreamVolume(AudioManager.STREAM_ALARM, 0, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

How can I use the androids' music stream as the data source of the media player library

In my app, I am trying to alter the volume of both(left/right headphone music streams) coming out of the device with a Seekbar.
The AudioManager Class can access the music stream coming from the device:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxValue = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curValue = am.getStreamVolume(AudioManager.STREAM_MUSIC);
But the AudioManager .setStreamVolume() class can only change the volume coming from both ears together.
I figured out how to set the volume separately with the MediaPlayer class but how do I link the MediaPlayer class's .setDataSource() method to the stream coming out of the android device?
I looked everywhere and still haven't found an answer. Any help is appreciated!
You use setAudioStreamType() after you setDataSource(), but before calling
prepare

Playing voicemail via MediaPlayer turns alarm volume silent

I play an audio file from a URL using MediaPlayer. The file plays fine, however after playback (if the app hasnt't been explicitly killed), alarm ringtone is silent. What do I need to clear/reset on end of playback to make this not happen?
Code for the file I am using to control the voicemail playback.
http://hastebin.com/ehijobuwoc.coffee
You can try getStreamVolume at start of playback then use setStreamVolume to set it back after your playback has finished.
http://developer.android.com/reference/android/media/AudioManager.html#setStreamVolume(int, int, int)
Ok have found something that works. Solves both the alarm issue and allows me to play the voicemail through the earpiece.
I needed to setMode back to what it was when I started, in onStop():
// set mode back to normal, and speakerphone back to true.
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(prevMode);
am.setSpeakerphoneOn(true);
and in init:
private void initMediaPlayer() {
// Make the media play through earpiece instead of through speaker(default)
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
prevMode = am.getMode();
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.setSpeakerphoneOn(false);
Thanks #DreadfulWeather for leading me to this answer, with some helpful questions.

How do I get the system volume programatically

I want to get the system volume level to use programatically, the one you set with the hardware keys when being outside any application.
I tried with below method but it doesnt do anything
MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.notif);
AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int volume_level = am.getStreamVolume(AudioManager.STREAM_SYSTEM);
mediaPlayer.setAudioStreamType(volume_level);
mediaPlayer.start();
What do I need to change?
You have to change this line
mediaPlayer.setAudioStreamType(volume_level);
to this:
mediaPlayer.setAudioStreamType(AudioManager.STREAM_SYSTEM);
setAudioStreamType()
Used to identify the type of audio stream
AudioManager.STREAM_ALARM is for alarm
AudioManager.STREAM_DTMF is for touch sounds
AudioManager.STREAM_MUSIC is for music
AudioManager.STREAM_NOTIFICATION is for notification
AudioManager.STREAM_RING is for phone ring
AudioManager.STREAM_SYSTEM is for system sounds
AudioManager.STREAM_VOICE_CALL is for phone calls
Please reffer offical doc here to set or get the system volume of android device.
To set volume level to 20 you can use following code:
AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

MediaPlayer - use ringtone volume instead of media volume

How can I set volume level from ringtone instead of media volume level on MediaPlayer?
Use setAudioStreamType(int) to set the media type to type STREAM_RING, then it should use the ringer volume instead of the default STREAM_MUSIC.
Note that you must do this before the media is prepared, so you'll have to prepare it manually with setDataSource instead of using MediaPlayer.create().
Since Lollipop, setAudioAttributes() is to be used over setAudioStreamType(int).
A catch that I had missed was that setAudioAttributes won't work if MediaPlayer was created as MediaPlayer.create()
Please refer to this answer: https://stackoverflow.com/a/51008956/10045546

Categories

Resources