Difference between Audiomanager and MediaPlayer - android

Can anybody explain me what's the difference between AudioManager and MediaPlayer in Android ? If I am correct, then AudioManager can only play audio, while MediaPlayer can play both audio and video. But I believe there must be more to this.
Thanks.

AudioManager doesn't play sound at all. It provides access to sound settings, but to play sounds easily, you should use MediaPlayer, SoundPool, or possibly AudioTrack.
From the docs:
AudioManager provides access to volume and ringer mode control.

AudioManager is used to manage audio settings. This includes volume control and the streaming channels (e.g. ringer, media, in-call, etc.).
MediaPlayer is used for controlling the playback (e.g. stop, play, pause, etc.) of audio/video streams.

Related

Android playing audio output to handset, but has no sound in earphone plugged in

Android AudioManager and output is quite difficult to understand. I am checking AudioManager api and also search for some explanation. But the scenario i want is not working the way i want.
I have the local downloaded audio file. First I play them as STREAM_MUSIC. So the sound will output to the phone speaker and when earphone plugged in, it will played through the earphone.
But i was requested to output the audio file to handset instead of the speaker.
There is no easy way to change it. I set audioManager.setSpeakerPhoneOn(false), the audio is still playing output through the speaker.
So after studying, i modified as below:
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
audioManager.requestAudioFocus(mFocusChangeListener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
audioManager.setSpeakerphoneOn(false);
By changing the audioManager mode to MODE_IN_COMMUNICATION (like the phone call), the audio file is finally output to handset. But when i plugged in the earphone, i didn't hear sound from the earphone, and i don't find the api to set the output route to handset manually in the AudioManager.
Any suggestion?

Is it possible to mute sound coming from the headphone and play the alarm on android on the external speaker?

It is possible to mute the headphone and play an alarm at the same time with one Audiomanager? I tried a couple of combinations of codes here but still has no luck. Here is my sample code.
AudioManager audioManager;
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(false);
audioManager.setStreamVolume(AudioManager.STREAM_SYSTEM, 10, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
MediaPlayer thePlayer = MediaPlayer.create(getApplicationContext(), RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
thePlayer.start();
Can you explain what I'm missing with this code? Thank you!
To Give more detail on my main goal, I'm trying to develop an android app that logs temperature readings, the hardware that is being used for the reading is connected via the 3.5mm jack of any mobile phone, I requirement for this is to have a sound alarm when the temperature reach a certain value, now my problem is that, there is a static sound that the 3.5mm jack generates so if I force all sounds to the speakers, the static sound of the audio alarm would mix up with the sound alarm, so what I'm trying to is to still output the sound on the external speaker, but mute the once coming from the 3.5mm jack, the only problem that I'm getting is that, once you output the sounds to the speakers, it would all go to STREAM_MUSIC, and there is no way to pick the sounds the is originally coming from the 3.5 jack. Any suggestions regarding to this would be really really be appreciated. Thanks!

Android media playback - stop audio output completely?

I have an app that plays video files with no audio. I'm using the mediaplayer function. I want to have the videos play, but not output anything to audio, as there is no audio tracks. However, Android ducks all other audio output (music, for example) when my app is running. Is there a way to use mediaplayer and completely remove my app from audio streams?
I've reviewed Google's Managing Audio Focus article, and I can't devise a way to do so. I just want to use mediaplayer for the video, and completely forget about audio.
Is there a different function I have to call in mediaPlayer.setAudioStreamType?
you can mute all the audio streams using the below code snippet
AudioManager am = (AudioManager)NextVideoPlayer.this.getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);

Play audio trough phone speaker of an android device

Is it possible to play audio trough the phone speaker of an android device?
The smaller speaker inside a phone that produces a low volume sound which can only be heard when listing closely with your ear against the phone.
Hopefully my description is clear enough to understand my question.
If it is possible, a example for how to accomplish this would be really helpful.
EDIT
Currently i'm using the following code to initialize my MediaPlayer.
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnBufferingUpdateListener(this);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
mediaPlayer.setDataSource(audioPath);
mediaPlayer.prepare();
If you want to play audio through the earpiece, use the STREAM_VOICE_CALL stream type. If you use the MediaPlayer class there's a setAudioStreamType method that you can use for this purpose, and for the AudioTrack class you pass the stream type to the constructor.

Android. How to Do Audio Recording with High Volume?

I will describe briefly my trouble with audio recording.
So, I am doing audio recording using MediaRecorder, but unfortunately when I playback the recorded audio, I have media with a very low volume. I hardly hear anything.
Is there any possibility to setup recording volume?
Thanks.
I experienced this issue on the Nexus One while using Froyo when the AudioManager was set to MODE_IN_CALL. Try setting it to MODE_NORMAL. This fixed the problem for me.
AudioManager audioManager = (AudioManager)activity.getSystemService(Context.AUDIO_SERVICE);
audioManager.setMode(AudioManager.MODE_NORMAL);
There is no way to do this while recording - but while playing, you can use the setVolume(float, float) method on MediaPlayer.

Categories

Resources