Can anyone please show me how and where can I modify the Phonegap source to play audio through the earpiece?
Try this code:
private AudioManager m_amAudioManager;
m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
Also need to add new permission:
MODIFY_AUDIO_SETTINGS
Either put this code in your activity start (onCreate()) or you need to write a plugin to enable/disable it based on your requirement from the javascript.
Check this post for more
Android - Getting audio to play through earpiece
Related
I'm making an Android audio visualizer app for streaming Spotify music. I'm using Spotify's android-streaming-sdk which creates a local service (no need to have the Spotify app installed) and it plays music from within the app. I'm having a difficult time getting Androids Visualizer library to pick up any audio from Spotify (it works fine if I use a local .mp3 file).
//Start playing spotify track
player.playUri(MainActivity.operationCallback,
VisualizerModel.getInstance().getTrackURI(), 0, 0);
.... other code ....
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
int audioSessionId = mediaPlayer.getAudioSessionId();
visualizer = new Visualizer(audioSessionId);
visualizer.setCaptureSize(audioSampleSize);
visualizer.setDataCaptureListener(this, Visualizer.getMaxCaptureRate(), true, true);
visualizer.setEnabled(true);
If I run this, everything compiles and runs okay, but the visualizer does not pick up any audio. This makes me thing that the audio sessionId is not the same one as Spotify is playing on. However, if I switch the audioSessionId to 0 (which should listen to all the audio sessions mixed from what I understand) it crashes with this error:
E/AudioEffect: set(): AudioFlinger could not create effect, status: -1
E/visualizers-JNI: Visualizer initCheck failed -3
E/Visualizer-JAVA: Error code -3 when initializing Visualizer.
According to their documentation that error means: Operation failed due to bad object initialization.
I'm targetting API 24 (Android 7.0).
Please let me know if you have suggestions or an alternative to using Android's Visualizer library. I feel like my main problem is that I'm not sure how to find the audioSessionId that Spotify is playing on, and because Spotify's android-streaming-sdk is in beta (and not mentioned on their website) there is virtually no documentation on it from what I can see on Github.
Thank you for taking the time to read my issue!
I was facing the same error. Adding "android.permission.RECORD_AUDIO" to manifest and requesting for runtime permissions fixed the issue.
to protect privacy of certain audio data (e.g voice mail) the use of the visualizer requires the permission android.permission.RECORD_AUDIO
Also
Creating a Visualizer on the output mix (audio session 0) requires permission Manifest.permission.MODIFY_AUDIO_SETTINGS
Android docs for Visualizer class
Is there a way we can check programmatically Which Music player is playing right now?
like Google Play, Samsung default Music Player, any 3rd party music player
Actually, we need to programmatically handle play/pause of music player. Google Play and Samsung Music works differently with code :
// Google Play do not play pause with this code
// it is using different package name i guess
CmdStop = "togglepause";
i = new Intent("com.android.music.musicservicecommand.togglepause");
i.putExtra(CmdName, CmdStop);
context.sendBroadcast(i);
Any help is appriciated
Thank you
you don't need a broadcast receiver for this - AudioManager is your friend:
AudioManager.isMusicActive() does the job you want, have a closer look here for details: AudioManager
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or do it not
}
stackoverflow answer
I have an app that keeps on listening to voice and converting it to commands using Google Voice API.
I have been using setStreamMute(AudioManager.STREAM_SYSTEM, true) to mute the beep and it worked until a couple of days ago before "Google Search" new update.
Is there any workaround fix for it?
I know I can use setRingerMode(AudioManager.RINGER_MODE_SILENT), but maybe there is another method?
In the update they switched the output of the 'beep' to the media stream.
So you'll need to mute the AudioManager.STREAM_MUSIC
There's an enhancement request about it here
Mute the beep by muting the notification sound:
(getSystemService(Context.AUDIO_SERVICE) as AudioManager).adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_MUTE,0)
Make sure to unmute it after you start listening:
(getSystemService(Context.AUDIO_SERVICE) as AudioManager).adjustStreamVolume(AudioManager.STREAM_NOTIFICATION, AudioManager.ADJUST_UNMUTE,0)
Please note that there's a beep sound when the listening stops.
The beep sound can be muted by using AudioManager
AudioManager mAudioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, true);
To unmute sound
AudioManager mAudioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setStreamMute(AudioManager.STREAM_MUSIC, false);
Also add permission to Manifest.xml
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>
I am not able to mute the incoming call ring tone and play my own media file using MediaPlayer class.
So far I have tried the following in my Activity that pops on top of incoming call screen, started using a BroadcastReceiver; Activity shows up but audio doesn't play:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_RING,true);
am.setStreamVolume(AudioManager.STREAM_RING, 0, 0);
Then I start playing the audio file. The sound plays perfectly in normal mode, so I also set the audio mode to MODE_NORMAL using am.setMode method but no luck.
Is there any way around? I am currently mobile and so I could not post full code. If anyone has done this before, a block of code would be a great help. Moreover, I dug up the documentation for AudioManager class and found setRoute or similar method which looks good but it has been deprecated long ago so I didn't want to implement it.
Does this approach have different reaults when screen is on or off during an incoming call?
I would like to know whether we could receive the DTMF tones in android.Suppose,i am getting a call,and once i accept the call,is it possible to detect the keys that the other person is pressing during our call.I have go through many stack overflow questions regarding this,but most of them were not providing a solution.
How about implementing it through java reflection or something like that.?All the earlier post were for 2.2 and 3.0 versions.Presently we are in the 4.0 and above,so is it possible in the 4 or above versions?
Thanks in advance
There is no packages to do this in SDK.
FIRST, you need to listen the speaker voice, because you can not record voice call :
AudioManager mAudioManager = (AudioManager) Sos.getContext().getSystemService(Context.AUDIO_SERVICE);
int maxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
mAudioManager.setStreamVolume(AudioManager.STREAM_VOICE_CALL, maxVolume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);
mAudioManager.setSpeakerphoneOn(true);
AND, this project may help you :
http://code.google.com/p/dtmf-decoder/source/checkout
I've done this, so i know it is possible this way.
Good luck