I am making an internal calls application, when I call a number, the native android dialer opens and then instead of following the call in the native dialer, I follow the call in the app. The problem is that from the app it has no effect when I want to activate or deactivate the native dialer speaker. The native dialer is in the background, and the app is in the foreground, but for some reason when I try to activate the speaker from the app, to follow the call from here, it doesn't work. This only happens in versions of Android 11 and 12, however in versions of Android 8,9 and 10 it works perfectly, I can manipulate the speaker from the app, without going to the native dialer.
This is my code to activate and desactivate the speaker:
private fun enableSpeaker() {
audioManager?.let {
if (!it.isSpeakerphoneOn) {
audioManager!!.mode = AudioManager.MODE_IN_COMMUNICATION
audioManager!!.isSpeakerphoneOn = true
}
}
}
private fun disableSpeaker() {
audioManager?.let {
if (it.isSpeakerphoneOn) {
//audioManager.setMicrophoneMute(false)
audioManager!!.mode = AudioManager.MODE_NORMAL
audioManager!!.isSpeakerphoneOn = false
}
}
}
For Android 12 and above, use audioManager.setCommunicationDevice instead of audioManager.isSpeakerPhoneOn = true
More info here : https://developer.android.com/reference/kotlin/android/media/AudioManager#setCommunicationDevice(android.media.AudioDeviceInfo)
Related
I'm trying and failing to get a camera-feed on the Mavic Enterprise 2 dual, using the Mobile SDK. I am connected to the drone, and my app runs fine and displays the video with both the Mini2 and Air 2S. I am on MSDK 4.16.4.
It seems like the problem video-data listener, defined in activity onCreate, is never called. Here is the snippet of relevant code. On the Mini2 and Air2S (with R1-N1 controller), the video-data-listener is called. On the ME2 Dual (with the controller that came with the drone), the video-data-listener is never called.
textureView.surfaceTextureListener = FakeTextureView(
codecCallback = {
mCodecManager = it
primaryVideoFeed.addVideoDataListener { videoBuffer, size ->
freshFrameAvailable = true
mCodecManager?.sendDataToDecoder(videoBuffer, size)
}
},
applicationContext = applicationContext
)
Is there some extra step required for ME2 Dual that I'm missing?
Prior to Android 12, I used code below to detect physical volume buttons pressed to show my custom UI, but it stopped working on Android 12 devices and onAdjustVolume is never called when I press volume buttons:
mediaSessionCompat = MediaSessionCompat(context, "My App")
mediaSessionCompat?.isActive = true
mediaSessionCompat?.setPlaybackState(
PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN, 1f)
.build())
mediaSessionCompat?.setPlaybackToRemote(object :
VolumeProviderCompat(VOLUME_CONTROL_ABSOLUTE, 7, 4) {
override fun onAdjustVolume(direction: Int) {
Log.v("My App", "Volume adjusted")
}
})
Please, check this link:
https://issuetracker.google.com/issues/201546605?pli=1
It seems like Google changed it because of "privacy changes", according to them, but it is supposed it will be reverted in the next release (nobody knows when).
I've implemented Android client using Twilio SDK to do video calls. It works as expected but I found an edge case which I can not figure out how to fix. Here is essence of video call code:
val connectionOptions = ConnectOptions.Builder(accessToken)
.audioTracks(listOf(audioManager.getLocalAudioTrack()))
.roomName(roomId)
.build()
val roomListener = RoomListener()
Video.connect(androidContext, connectOptions, roomListener)
class RoomEventListener : Room.Listener {
override fun onParticipantDisconnected(room: Room, remoteParticipant: RemoteParticipant) {
// remove participant from the screen, unsubscribe from events
}
override fun onConnectFailure(room: Room, twilioException: TwilioException) {
exitScreenWithErrorMessage(R.string.video_consult_room_connection_error)
}
override fun onReconnected(room: Room) {
_shouldShowReconnectionActivity.value = false
}
override fun onParticipantConnected(room: Room, remoteParticipant: RemoteParticipant) {
onRemoteParticipantConnected(remoteParticipant)
}
override fun onConnected(room: Room) {
_shouldShowConnectionActivity.value = false
this#VideoCallViewModel.room = room
room.remoteParticipants.forEach { onRemoteParticipantConnected(it) }
determineMainParticipant()
onLocalParticipantConnected(room)
}
override fun onDisconnected(room: Room, twilioException: TwilioException?) {
exitVideoConsultScreen()
}
override fun onReconnecting(room: Room, twilioException: TwilioException) {
_shouldShowReconnectionActivity.value = true
}
}
Test case:
Bob joins video call using Android phone
Jane joins same video call from any device (iOS, web, Android)
When Jane loses connection (i.e. turn off internet)
Then Bob sees for 1-2 minutes reconnecting (programmatically "onReconnecting" callback triggered)
[Actual] And Bob disconnected from room (in logs I see Media connection failed or Media activity ceased with error code 53405)
[Expected] Bob stays at the room.
I'm not sure why under such conditions Android client has been disconnected (We tested it on different devices with Android 8/9).
Couple more details:
If Jane exits room using "End call" button (so room.disconnect() code from Twilio SDK has been called) then Bob stays in the room.
When Bob using iOS device (implementation of iOS and Android quite the same) then described use case passes.
We tried 5.0.1 and 5.1.0 version of com.twilio:video-android library.
I noticed Known Issue for Android Twilio Video library in Release notes and I'm not sure can it affects described use case or not:
Unpublishing and republishing a LocalAudioTrack or LocalVideoTrack might not be seen by Participants. As a result, tracks published after a Room.State.RECONNECTED event might not be subscribed to by a RemoteParticipant.
I opened issue on twilio github repo https://github.com/twilio/video-quickstart-android/issues/454 - and this is expected behaviour for twilio video sdk 5.x+. Both for Android and iOS sdks.
i am working on AudioManager which is a Android SystemService.
with Android System 5.0+ , i encounter a problem which AudioManager the setMode method is not working .
i through a test ,
Android M, Lollipop.. 5.0+ version , AudioManager setMode is not working .
example :
public void initAudioImageIcon(boolean initLoad) {
boolean isAudioHeaderMode = IMSharedPreferences.getBooleanExtra(this, IMSPConstant.SP_NAME_MESSAGE,
IMSPConstant.SP_KEY_AUDIO_HEADER_MODE);
if (isAudioHeaderMode) {
mAudioHanderMode.setVisibility(View.VISIBLE);
// audioManager.setMode(AudioManager.MODE_IN_CALL) , but android system 5.0+ no any change, getMode() == AudioManager.MODE_NORMAL
setAudioMode(AudioManager.MODE_IN_CALL);
audioManager.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL);
if (!initLoad) {
showAudioModePrompt(this.getText(R.string.im_audio_in_call), 1000);
}
} else {
mAudioHanderMode.setVisibility(View.GONE);
setAudioMode(AudioManager.MODE_NORMAL);
if (!initLoad) {
showAudioModePrompt(this.getText(R.string.im_audio_in_speeker), 1000);
}
}
}
but Android 3.0+,4.0+ is ok ,only 5.0+ .
so ,i don`t know where happen mistakes.
With audio mode set to :
setMode(AudioManager.MODE_IN_COMMUNICATION);
setSpeakerphoneOn(false);
while my audio stream is set to STREAM_MUSIC I can easily route audio to earpiece. I have tested it myself in AOSP Lollipop code.
Here in the question you have never mentioned about your stream type. Do set your stream to STREAM_MUSIC or STREAM_VOICE_CALL and the code should work for you too.
In android Lollipop setAudioMode(AudioManager.MODE_IN_CALL) is restricted. It can be used only by system application with MODIFY_PHONE_STATE permission. However you can use MODE_IN_COMMUNICATION and MODE_NORMAL in normal applications.
I am writing a game using Monogame for Android.
In my music manager, I have some code like this:
private void ChangeMusic()
{
if (!MediaPlayer.GameHasControl)
{
return;
}
// ... Play music
}
I am finding that MediaPlayer.GameHasControl is always returning true, regardless of what is happening on the phone. I have tried playing music in every music app I have on my phone (including Samsung Music player and Google Play Music), and then starting my app, and MediaPlayer.GameHasControl is still true.
Is this expected? Has anyone seen this work before?
Took a look at the MonoGame source, and GameHasControl just isn't even implemented on Android.
public static bool GameHasControl
{
get
{
#if IOS
...
#elif WINDOWS_PHONE
...
#else
// TODO: Fix me!
return true;
#endif
}
}
Anyone interested in the subject should check out this article:
http://developer.android.com/training/managing-audio/index.html