I am using webrtc native for android and while using bluetooth, the webrtc lib doesn't select the bluetooth headset as default mic, so how to switch audio source that is microphone, like in the whatsapp,
currently i am creating audio source like as,
AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
AudioTrack localAudioTrack = peerConnectionFactory.createAudioTrack("ARDAMSv1", audioSource);
You don`t need to change webrtc audioTrack, only AudioManager settings. To use bluetooth headset:
private val audioManager: AudioManager = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
if (!audioManager.isBluetoothScoOn) {
audioManager.startBluetoothSco()
}
audioManager.isBluetoothScoOn = true
To switch back to phone speaker:
audioManager.isBluetoothScoOn = false
audioManager.stopBluetoothSco()
And I dont properly remember, but if code above doesnt work, change also audiomanager mode like this:
audioManager.mode = AudioManager.MODE_IN_COMMUNICATION
Related
I need to have some custom behavior in my app when the audio playback is done on the device speaker (not a wired headphone and not a BlueTooth receiver).
To do so I'm doing the following
AudioManager audioManager = (AudioManager) service.getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
isSpeakerPhoneOn = audioManager.isSpeakerphoneOn();
But isSpeakerPhoneOn is always false.
By the way, I'm calling the code after playback is started.
Any idea what I'm doing wrong?
I suggest you to try this
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or don't
}
I am creating Media Player, but it should never play on Speaker. If head phone jack or bluetooth is not available, still Audio should not be played over speaker.
I used below Android API but it still plays over speaker:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
am.setSpeakerphoneOn(false);
You can check the whether Bluetooth and headphone connected or not by using Broadcast receiver using this link http://blog.urvatechlabs.com/detect-programatically-if-headphone-or-bluetooth-headsets-attached-with-android-phone/ . If it is not connected/removed pause/stop the Android Media Player.
From AudioManager official documentation
audioManager.setSpeakerphoneOn(boolean)
Sets the speakerphone on or off.
It means if you set the false it will disable the speaker sound i.e playing out from the speaker and if you set true it will play from the speaker.
In your case, you don't want to play your music from the outer speaker but still your using the am.setSpeakerphoneOn(true); which is actually enables the outer speaker.
So set am.setSpeakerphoneOn(false); so that it won't play the music from outer speaker
You can also set the Mode ( Call / Voice Communication / Music etc) for your AudioManager
audioManager.setMode(AudioManager.STREAM_MUSIC);
Note:: For changing the audio manager settings you need to set Permission: MODIFY_AUDIO_SETTINGS in manifest
add this line in manifest
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
In additional, this is the code to check which type of audio conncection
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
PackageManager packageManager = getPackageManager();
if (audioManager.isBluetoothA2dpOn()) {
// Adjust output for Bluetooth.
Log.d("debug","BluetoothA2dpOn");
} else if (audioManager.isBluetoothScoOn()) {
// Adjust output for Bluetooth of sco.
Log.d("debug","BluetoothScoOn");
} else if (audioManager.isWiredHeadsetOn()) {
// Adjust output for headsets
Log.d("debug","WiredHeadsetOn");
} else if (audioManager.isSpeakerphoneOn()) {
// Adjust output for Speakerphone.
Log.d("debug","SpeakerphoneOn");
} else if (packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
// Has internal speaker or other form of audio output.
Log.d("debug","Internal Speaker On");
} else {
// No device for audio output.
Log.d("debug","No Audio Device");
}
I integrated appRTC code in my Android application for calling purpose, which is done. Now the Video & Audio calling is working fine. My problem is, I need to achieve following things.
1. Mute & Unmute Audio while calling.
2. Switch Video call to Audio Call and vise versa while calling.
I have searched a lot and had no luck so far. It would be nice if you can give me any lead on these things. Thanks in advance.
1. Mute & Unmute Audio while calling.
This class is used to control the audio: https://chromium.googlesource.com/external/webrtc/+/b69ab79338bff71ea411b82f3dd59508617a11d5/talk/examples/android/src/org/appspot/apprtc/AppRTCAudioManager.java
You may need to explicitly add mute functionality here.
2. Switch Video call to Audio Call and vise versa while calling.
PeerConnectionClient class in AppRtc demo depends on the following class:
https://chromium.googlesource.com/external/webrtc/+/b69ab79338bff71ea411b82f3dd59508617a11d5/talk/app/webrtc/java/src/org/webrtc/VideoCapturerAndroid.java
To switch video call to audio, you need to explicitly call stopCapture in VideoCapturerAndroid.java
So far no luck about switching between Audio and Video Call. But I have found a solution to mute microphone audio while calling.
There is a setMicrophoneMute(boolean on) method in AppRTCAudioManager. We can use the same code to mute the microphone. I just created another method like this.
public void setMicroPhoneMute(){
boolean wasMuted = audioManager.isMicrophoneMute();
if(wasMuted)
audioManager.setMicrophoneMute(false);
else
audioManager.setMicrophoneMute(true);
}
Just call this one wherever you want.
Please refer to this Link for switching audio / Video - Link
although this is in javascript but it can be easily implemented in android.
when the connection is created via webrtc we receive Media Stream ,for switching purpose we can remove audiotrack or videotrack and then again send sdp from offerer to answerer side with new configuration.
mediaStream.removeAudioTrack(Audiotrack audioTrack) ; //for audio to video switching
mediaStream.removeVideoTrack(VideoTrack videoTrack) ; // video to audio switching
use this for switch between mute -> unMute, Video -> Audio
just need the MediaStream localMS
//disable video in stream
VideoTrack currentTrack = localMS.videoTracks.get(0);
currentTrack.setEnabled(false);
//disable audio in stream // mute
AudioTrack curentAudioTrack = localMS.audioTracks.get(0);
curentAudioTrack.setEnabled(false);
if you want to unMute just use true like this
AudioTrack curentAudioTrack = localMS.audioTracks.get(0);
curentAudioTrack.setEnabled(true);
same for video
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
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.