Playing audio file into the call android - android

Say I make a call to someone with my android phone.
Is it possible to play some audio file into the call?

I just tried like this
if(state==TelephonyManager.CALL_STATE_OFFHOOK){
AudioManager am = (AudioManager) pccontext.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
MediaPlayer mp = MediaPlayer.create(pccontext, R.raw.beep);
mp.start();
}
But its not working in MODE_NORMAL. When I tried with MODE_IN_CALL phone is playing the audio but caller on the other side is not able to listen the audio played by the receiver phone.

Unfortunately, it is not possible, it is an Android security limitation.

Related

Android - how to play a midi file data to external midi keyboard

Following up (or should have been preceding) my last post, I am now wondering if I am on the wrong path.
I want to play midi file data (not audio) to an external device, such as a midi enabled keyboard, and have the midi data play the keyboard.
As per my last post, I have:
MediaPlayer mediaPlayer;
String music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
mediaPlayer = MediaPlayer.create(MainActivity.this, Uri.parse(music + "/test.mid"));
mediaPlayer.start();
When I run this, it just play the midi file as audio.
Later, the next part is sending this out (hopefully) by Bluetooth, ie code sometime like form the suggestion from my last post:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo device : devices) {
int type = device.getType();
if (device.getType() == AudioDeviceInfo.TYPE_BLE_HEADSET ) {
mediaPlayer.setPreferredDevice(device);
break;
}
}
However, is the above going to midi data, or just the generated audio? Can MediaPlayerMediaPlayer be used for this, or do I need to use something completely different such as java-midi or the MidiManager as used in these examples.
I have not got any Bluetooth midi receiver device yet (ie to play into the keyboard), as I do not want to purchase something like this, for example this, though it only claim to support iOS, but isn't Bluetooth universal?, At any rate, that is a separate topic, this is just about actually playing out the midi data
I only ever want to replay a file, I never want to actually generate the midi events, so was hoping something simpler like MediaPlayer would do what I am after, but is this correct?

Combine parts of SCO with parts of A2DP

I'm looking to pair A2DP stereo sound with the interrupt functionality of AudioManager's MODE_IN_COMMUNICATION. The purpose of this pairing is to insert snippets of custom music into AM/FM broadcasts by using the music as something that "dials" the Android device and stops the AM/FM broadcast.
I have this working decently with SCO using the following code to start the "phone call".
Here is the AudioManager code:
AudioManager localAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
localAudioManager.setMode(0);
localAudioManager.setBluetoothScoOn(true);
localAudioManager.startBluetoothSco();
localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
Here is the MediaPlayer I'm trying to play:
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioAttributes(new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build());
mediaPlayer.setDataSource("https://wmbr.org/WMBR_Archive_128.m3u");
mediaPlayer.prepare();
mediaPlayer.start();
The audio produced by this code is low-quality and mono as opposed to stereo. I would like to change that.
The issue is that the Android Dev site for startBluetoothSco says:
Even if a SCO connection is established, the following restrictions apply on audio output streams so that they can be routed to SCO headset: - the stream type must be STREAM_VOICE_CALL - the format must be mono - the sampling must be 16kHz or 8kHz
Is there any existing way to combine stereo sound and the interrupt functionality?
Additional context: in this answer it seems that MODE_IN_COMMUNICATION and MODE_IN_CALL use the PHONE routing strategy. He also says
If your BT accessory supports the Hands-free profile it will use a SCO link for the voice audio, and the ACL channel used for A2DP should be closed to avoid interference between the two.
I'm assuming this means my only option is a custom routing strategy, and I'm not sure what that entails.

what code can disable headphone jack for an android(4.4) device

Im trying to build an app that can enable or disable the headphone jack on my Android 4.4 device . Once disabled nothing should be able to take control or activate it, even calling. I can root the device for this.
Any help would be appreciated.
As written here: How to mute audio in headset but let it play on speaker programmatically?
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(false);
And then play the sound through the AudioManager.STREAM_SYSTEM stream.
When the sound's finished playing be sure to return the audio manager to its previous state or it'll stay on loudspeaker.

How to mute audio in headset but let it play on speaker programmatically?

I am searching a work-around for my problem specified in this question:
How to disable the wired headset programmatically in Java
As mentioned there, I am getting audio in both my speakers and headphones.
Can someone please tell me how to mute the audio in the headset programmatically, while letting it play undiminished on speaker?
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_IN_CALL);
am.setSpeakerphoneOn(true);
And then play the sound through the AudioManager.STREAM_SYSTEM stream.
When the sound's finished playing be sure to return the audio manager to its previous state or it'll stay on loudspeaker!!
AudioManager mAudioMgr =
(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioMgr.setSpeakerphoneOn(true);
mAudioMgr.setMode(AudioManager.MODE_IN_COMMUNICATION);

How to use internal speaker to play sound while a 3.5mm jack plugged to record sound using an external microphone

I'm now programming an app to give vocal feedback through internal speaker while user performing some exercises.
Now I want to add a function to record the user's heartbeat and breathe through a stethoscope plugged in the phone by a 3.5mm jack. The problem is that when I plug in the jack, the speaker won't play any sound because the phone think that earpieces were connected and the sound will be played through them.
What I want to achieve is to record the sound by a stethoscope plugged in the phone while play sound through internal speaker.
Basically I use two ways to play sound, MediaPlayer and TextToSpeech.
I searched on the internet, found these articles:
Forcing sound output through speaker in Android
want to play TTS on Bluetooth headset
What they told me to do is to add permission MODIFY_AUDIO_SETTINGS to manifest
declare variables:
MediaPlayer mediaPlayer;
AudioManager mAudioManager;
HashMap<String, String> mHashAlarm = new HashMap<String, String>();
in OnCreate method:
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
mediaPlayer = MediaPlayer.create(context, R.raw.ding);
mHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
...
tts.speak(text, TextToSpeech.QUEUE_FLUSH, mHashAlarm);
But neither of them works.
Does anyone know how to achieve this function? Any ideas would be a lot of help. Thanks.
Simply purchase a audio splitter...after that u get two ports plug your external mic on one and plug any speaker on another ....

Categories

Resources