I have created a WebRTC session from one device to another, the device should be able to control the volume for music stream, but WebRTC is originally designed to stream voice_call so is using the voice_call channel and using the call volume control is not good behavior for non-call app.
I tried to change STREAM_VOICE_CALL to STREAM_MUSIC in WebRTC source WebRtcAudioTrack to use the stream music volume but the only change was android is detecting it as music but volume change with call volume.
I found the solution to this. You have to change the opensls player for this to happen
change this from here
// corresponds to android.media.AudioManager.STREAM_VOICE_CALL.
SLint32 stream_type = SL_ANDROID_STREAM_VOICE;
RETURN_ON_ERROR(
(*player_config)
->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE,
&stream_type, sizeof(SLint32)),
false);
to this
// corresponds to android.media.AudioManager.STREAM_MUSIC.
SLint32 stream_type = SL_ANDROID_STREAM_MEDIA;
RETURN_ON_ERROR(
(*player_config)
->SetConfiguration(player_config, SL_ANDROID_KEY_STREAM_TYPE,
&stream_type, sizeof(SLint32)),
false);
do this here too
Related
I am trying to get my media player to work with the alarm volume and not media volume. I have tried this, but it is not working, any suggestions? Also, am I am looking to change the sound file at some point, can the solution be used with a variable?
val mediaPlayer1 = MediaPlayer.create(context2, R.raw.geese)
mediaPlayer1.setAudioAttributes(AudioAttributes.Builder().setUsage((AudioAttributes.USAGE_ALARM)).build())
mediaPlayer1.start()
mediaPlayer1.setLooping(true)
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
I'm playing an audio clip using OpenSL ES. In my code I have
audioManager.setMode(AudioManager.MODE_IN_CALL);
audioManager.setSpeakerphoneOn(true);
to force audio through the speaker while the headset is plugged in. It works fine, but I can't control the volume. Pressing the volume buttons while the clip is playing makes the volume seekbar appear and move, but the volume doesn't change.
Calling setVolumeControlStream(AudioManager.STREAM_VOICE_CALL) or setVolumeControlStream(AudioManager.STREAM_MUSIC) before playing doesn't seem to help.
Changing any of the volumes outside of my app (e.g. in the Android settings) doesn't affect the playing volume. Volume control works well on both headset and speaker when no routing is applied.
I've also tried routing the audio to the speaker using this code I found in another answer
Class audioSystemClass = Class.forName("android.media.AudioSystem");
Method setForceUse = audioSystemClass.getMethod("setForceUse", int.class, int.class);
// First 1 == FOR_MEDIA, second 1 == FORCE_SPEAKER. To go back to the default
// behavior, use FORCE_NONE (0).
setForceUse.invoke(null, 1, 1);
but it doesn't work on my Android 4.3 Nexus 4. I need the most compatible way to to that anyway.
Any ideas?
Thanks.
Here is a couple ideas:
MODE_IN_CALL sets all sorts of priority/policy on STREAM_VOICE_CALL. During this time, other STREAM may loose volume control focus. See if your audio clip is played over STREAM_VOICE_CALL.
MODE_IN_COMMUNICATION (for VoIP) may be a better fit for you. MODE_IN_CALL is for cellular call and can degrade your audio quality.
You may want to try grab audio focus and see if that helps. http://developer.android.com/training/managing-audio/audio-focus.html
I'm working on some music analysis using the Visualizer class on Android 2.3.1. I am finding that the FFT and waveform magnitudes are affected by the volume of the device. This means that if the user has the volume turned down I receive little or not FFT data.
I've tested this on a Motorola Xoom, Samsung Galaxy Tab and the emulator and it behaves this way.
I am using the code below:
mp = new MediaPlayer();
mp.setDataSource("/sdcard/sine1.wav");
mp.prepare();
mp.setLooping(true);
mp.start();
int audioSessionID = mp.getAudioSessionId();
v = new Visualizer(audioSessionID);
v.setEnabled(true);
Looking at the docs for the Visualizer class it seems that if we are passing in a valid audio session id then the visualizer should operate upon this audio session. It appears that the Visualizer is operating upon the output mix.
Has anyone else encountered this or found a way around it?
Thanks
I was also facing the same problem, but it is working when i am enabled the Eqaulizer and Visualizer for same seession id.I dont know the reason for it ,i checked it remove the equalizer from visualizer class in api demos it is working as you said.
Equalizer mEqualizer = new Equalizer(0, SessionId);
mEqualizer.setEnabled(true); // need to enable equalizer
Visualizer mVisualizer = new Visualizer(SessionId);
There are two options for the Visualizer scaling mode:
SCALING_MODE_AS_PLAYED and SCALING_MODE_NORMALIZED
If you want the Visualizer to be normalized, as in it's consistent no matter what the volume is, then use SCALING_MODE_NORMALIZED.
mVisualizer.scalingMode = Visualizer.SCALING_MODE_AS_PLAYED
Keep in mind though that this drastically changes the values being sent to the Visualizer, so other adjustments may be needed.
I'm at a lost. I want to be able to adjust the speak volume. Whatever I do, I can't increase its volume. How do I make it as loud as that found in the Android settings (as below)?
System Settings -> Voice input and output -> Text-to-Speech settings -> Listen to an example
My code at this moment is:
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mAudioManager.setSpeakerphoneOn(true);
int loudmax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);
mAudioManager.setStreamVolume(AudioManager.STREAM_NOTIFICATION,loudmax, AudioManager.FLAG_PLAY_SOUND);
mTts.speak(name,TextToSpeech.QUEUE_FLUSH, null);
Try using AudioManager.STREAM_MUSIC when calling the setStreamVolume(...) method. The example speech is affected by the media volume if I adjust the volume of music playback on my phone so I guess STREAM_MUSIC is what you need.
EDIT: This code works perfectly for me...
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int amStreamMusicMaxVol = am.getStreamMaxVolume(am.STREAM_MUSIC);
am.setStreamVolume(am.STREAM_MUSIC, amStreamMusicMaxVol, 0);
tts.speak("Hello", TextToSpeech.QUEUE_FLUSH, null);
The max volume for STREAM_MUSIC on my phone is 15 and I've even tested this by replacing amStreamMusicMaxVol in my call to am.setStreamVolume(...) above with the values 3, 6, 9, 12, 15 and the volume of the speech is correctly set.
In your code you are changing the volume of notifications. Is the volume of TTS played at the same volume level as notifications? I suspect it isn't and it probably played at either STREAM_SYSTEM or STREAM_MUSIC Try changing the stream type to one of these:
STREAM_VOICE_CALL, STREAM_SYSTEM, STREAM_RING, STREAM_MUSIC or STREAM_ALARM