Android - Unable to MUTE Audio in ChromeCast - android

I'm implementing cast feature in one of my application and trying to mute the audio being cast from sender application to default receiver app.
Using "setStreamVolume()" api on RemoteMediaPlayer object to mute the audio, see below code:
remoteMediaPlayer.setStreamVolume(googleApiClient, 0).setResultCallback(
new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {
#Override
public void onResult( MediaChannelResult result) {
Status status = result.getStatus();
Log.d(TAG, "MUTE status:"+status);
}
});
But observed that, its returning 2100 (STATUS_FAILED) status code in result callback and not able mute the audio in receiver application.
Please suggest...

I strongly suggest not using the Stream Volume but rather use the Device Volume. The corresponding mute method can be found here.

Related

AudioFocus request DENIED to OpenTok(Tokbox) when in same added Call, android 9 and 10

Android Telecom Manager no incoming audio/sound on an Added VOIP call
I am trying to add a VOIP video calls in my app. I have registered the phone account and added the call to TelecomManager. Call is accepted successfully. I have already implemented Connection and ConnectionService.
I am using the below code to add a call.
var uri = Android.Net.Uri.FromParts(PhoneAccount.SchemeSip, voipCallInfo.Payload?.CallerName, null);
extras.PutParcelable(TelecomManager.ExtraIncomingCallAddress, uri);
extras.PutParcelable(TelecomManager.ExtraPhoneAccountHandle, phoneAccountHandle);
telecomManager.AddNewIncomingCall(phoneAccountHandle, extras);
and I am using the below code to accept the ringing call.
var telecomManager = GetTelecomManager();
if (telecomManager == null)
{
logger.Error("Telecom Manager is null, May be permissions not granted");
return;
}
try
{
.
.
.
telecomManager.AcceptRingingCall();
.
.
}
catch (Exception ex)
{
logger.Error("RequestSystemToAnswerCall Exception : " + ex.Message);
}
I have tried to request the audio focus, but when I add a call in the telecom manager my app loses the focus because the phone starts ringing. After I accept the call app doesn't get the focus back I believe Telecom/Call has the focus but I can't hear anything. Another person on the call can hear me without a problem. When I end the call apps get the focus back.
I can see below in the logs.
2020-06-22,14:09:34.831 WebRTCManager Trace IsAudioSubscriptionEnabled True
[AudioManager] Use of stream types is deprecated for operations other than volume control
[AudioManager] See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case
[AUDIO_FOCUS] Audio Focus request DENIED !
Below is the code I am using for requesting Audio.
public bool RequestAudioFocus()
{
var amanager = (AudioManager)GetSystemService(AudioService);
AudioFocusRequest audioFocusRequest;
if (Build.VERSION.SdkInt > BuildVersionCodes.O)
{
audioFocusRequest = amanager.RequestAudioFocus(new AudioFocusRequestClass.Builder(AudioFocus.Gain)
.SetAudioAttributes(new AudioAttributes.Builder().SetLegacyStreamType(Stream.VoiceCall).Build())
.SetOnAudioFocusChangeListener(this)
.Build());
}
else
{
audioFocusRequest = amanager.RequestAudioFocus(this, Stream.VoiceCall, AudioFocus.Gain);
}
Debug.WriteLine("MainActivity RequestAudioFocus audioFocusRequest : " + audioFocusRequest);
if (audioFocusRequest == AudioFocusRequest.Granted)
{
return true;
}
return false;
}
When I establish a VOIP connection without using TelecomManager. Everythings work fine. So I believe something goes wrong when I add and accept the call.
Thanks for any idea or fix.
The reason why you can't hear anything is because OpenTok obeys the result of the RequestAudioFocus. Since the audio focus request fails OpenTok will not play audio. You can either find out why RequestAudioFocus fails or download to Xamarin.OpenTok.Android 2.14.2 in order to play audio even if RequestAudioFocus fails.
You should use AudioManager.AUDIOFOCUS_GAIN or AudioManager.AUDIOFOCUS_GAIN_TRANSIENT to request the audio focus. Here you are going to request for an undefined amount of time when call is received, so using the AudioManager.AUDIOFOCUS_GAIN_TRANSIENT is highly recommended for audio focus request. Here is the code snippet.
AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
AudioAttributes mAudioAttributes =
new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build();
AudioFocusRequest mAudioFocusRequest =
new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(mAudioAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(...) // Need to implement listener
.build();
int focusRequest = mAudioManager.requestAudioFocus(mAudioFocusRequest);
switch (focusRequest) {
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
// don’t start playback
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
// actually start playback
}
By setting the listener callback function in setOnAudioFocusChangeListener you can listen to the audio focus change.
On Android N and earlier you can declare this intention without using the AudioFocusRequest object as shown below snippet. You still have to implement AudioManager.OnAudioFocusChangeListener to react to the status change. Here’s the equivalent code to the snippet above.
AudioManager mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
int focusRequest = mAudioManager.requestAudioFocus(
..., // Need to implement listener
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
switch (focusRequest) {
case AudioManager.AUDIOFOCUS_REQUEST_FAILED:
// don't start
case AudioManager.AUDIOFOCUS_REQUEST_GRANTED:
// start
}
As a final word, When your app has completed playing it’s audio, then it should abandon audio focus by calling AudioManager.abandonAudioFocus(…).
For your given code, if the OpenTok doesn't handle audio focus, You have to request the audio focus before AcceptRingingCall() method call. You can follow the official android docs.
I have the same problem. Here's a fix that probably isn't the 'right' way to do it, but works for me. In my VOIP call activity, once VOIP connection established, I kill my Connection object with:
mCallConnection.onDisconnect();
mCallConnection.destroy();
And OpenTok gains the audio focus back. Incoming audio works. I don't yet know the potential consequences of killing the Connection object early (can't perform other call functions like hold, interact with other incoming calls, etc.). But until I find another way for Connection to abandon its audio focus, I'll have to do this.

Android "O" (Oreo, 8) and higher media buttons issue

The code for handling media buttons from headsets that I use in my Text-to-Speech app works great under Android API 22 through 25 (in older versions of Android they are handled by other, now depreciated means). However under Android 8 "Oreo", both public beta and final release, it does not work. Here is the relevant code:
When the service starts, I create MediaSessionCompact object:
mSession = new MediaSessionCompat(getApplicationContext(), "my.package.name._player_session");
mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mSession.setActive(true);
mSession.setCallback(myMediaSessionCallback);
PlaybackStateCompat state = new PlaybackStateCompat.Builder()
.setActions(ACTION_PLAY_PAUSE | ACTION_PLAY | ACTION_PAUSE |
ACTION_SKIP_TO_NEXT | ACTION_SKIP_TO_PREVIOUS |
ACTION_FAST_FORWARD | ACTION_REWIND
)
.setState(PlaybackStateCompat.STATE_PAUSED, 0 /*PlaybackStateCompat.PLAYBACK_POSITION_UNKNOWN*/, 1f)
.build();
mSession.setPlaybackState(state);
There is of course session media callback defined:
private MediaSessionCompat.Callback myMediaSessionCallback = new MediaSessionCompat.Callback() {
#Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
// The log output below never appears on "Oreo", nothing comes here.
Log.d(TAG, "callback onMediaButtonEvent() Compat");
MediaButtonIntentReceiver.handleIntent(mediaButtonIntent.getAction(), (KeyEvent) mediaButtonIntent.getParcelableExtra(Intent.EXTRA_KEY_EVENT));
return true;
}
#Override
public void onSkipToNext() {
//...
}
// etc. other overrides
};
I also experimented with PendingIntent, using MediaButtonReceiver.buildMediaButtonPendingIntent() and set mSession.setMediaButtonReceiver(pendingIntent) for all the actions I'm interested in, then in my service onStartCommand() I call MediaButtonReceiver.handleIntent(mSession, intent):
// still in the same service:
mSession.setMediaButtonReceiver(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this,
mMediaButtonReceiverComponentName,
ACTION_PLAY));
mSession.setMediaButtonReceiver(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this,
mMediaButtonReceiverComponentName,
ACTION_PAUSE));
mSession.setMediaButtonReceiver(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this,
mMediaButtonReceiverComponentName,
ACTION_PLAY_PAUSE));
and in the service onStartCommand():
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
// ...
if (intent != null) {
MediaButtonReceiver.handleIntent(mSession, intent);
// ...
}
return START_NOT_STICKY;
}
Nothing, it's completely dumb to media buttons press events. What's wrong with "O" or my code there??? I'm completely baffled.
Update 8/32/2017
I also created a trivial but working app project that demonstrates the problem, please see: https://github.com/gregko/PlayerServiceSample. This project displays LogCat output when a media button is pressed on a headset under Android 5.x to 7.x, but fails completely under Android 8 "Oreo".
Update 9/1/2017
There is now an open issue on Android Issue Tracker about this, which I submitted, at https://issuetracker.google.com/issues/65175978. Still the media buttons work in several music player apps I tested on Oreo, I just can't figure out what do they do differently to make them work... The context of my app is not playing music, but reading aloud text with Text to Speech service, so a lot of code from Music Player examples does not apply.
Solved. On "Android 8.0 Behavior Changes" Google page we find this text:
In Android 8.0 (API level 26) the handling of media button events is different:
The handling of media buttons in a UI activity has not changed: foreground activities still get priority in handling media button events.
If the foreground activity does not handle the media button event, the system routes the event to the app that most recently played audio locally. The active status, flags, and playback state of a media session are not considered when determining which app receives media button events.
If the app's media session has been released, the system sends the media button event to the app's MediaButtonReceiver if it has one.
For every other case, the system discards the media button event.
All I had to do to make my trivial sample work was to play some sound with MediaPlayer. Apparently playing sound with Text-to-Speech API does not qualify, which in my opinion is a bug.
Here is the code I added to my trivial sample to make it work, playing a very brief and silent WAV file from Raw resources directory:
final MediaPlayer mMediaPlayer;
mMediaPlayer = MediaPlayer.create(this, R.raw.silent_sound);
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mMediaPlayer.release();
}
});
mMediaPlayer.start();
Update
Submitted the bug report to Android issue tracker at https://issuetracker.google.com/issues/65344811
Update 2, Oct. 10, 2017
Google now says that Oreo behavior in this respect is "by design" and won't fix it. Read the reply near the end of the issue tracker post above. I must say I'm disappointed.

Quickblox Audio call using ear speakers Android

I am implementing Quickblox WebRTC calling . All is successfully implemented, but I want the audio call through ear speakers of the phone. By default, it is only on loud speakers.
How can I enable calling through ear speakers like any other calling app? I also tried this:
AudioManager m_amAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
m_amAudioManager.setMode(AudioManager.MODE_IN_CALL);
m_amAudioManager.setSpeakerphoneOn(false);
But still didn't worked for me!
To switch the audio socket you need to call the switchAudioOutput(); method on your current version
it switches between the loudspeakers and phone speakers (if the headset is on, then it switches between the headset and the loudspeakers)
If you are using QuickBlox SDK version 2.5.2, you can set the AudioManager like this:
audioManager = AppRTCAudioManager.create(this, new AppRTCAudioManager.OnAudioManagerStateListener() {
#Override
public void onAudioChangedState(AppRTCAudioManager.AudioDevice audioDevice) {
}
});
audioManager.setDefaultAudioDevice(AppRTCAudioManager.AudioDevice.EARPIECE);
audioManager.setOnWiredHeadsetStateListener(new AppRTCAudioManager.OnWiredHeadsetStateListener() {
#Override
public void onWiredHeadsetStateChanged(boolean plugged, boolean hasMicrophone) {
}
});
audioManager.init();

Need a simple demo for only check whether background Music running or not? [duplicate]

This question already has an answer here:
How to detect that music play in background
(1 answer)
Closed 4 years ago.
I need a simple demo, In which I want to check whether the music is running in the background or not? And also does not effected on the music which is running in Background
I read from here study Link
and made a demo code
Demo code is here but this is not giving me the O/P:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN);
int result = am.requestAudioFocus(new OnAudioFocusChangeListener() {
#Override
public void onAudioFocusChange(int focusChange) {
if(//background Music is running )
{}
else{
//background Music is not running
}
}
},AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
What is need to be add for what I want. Please help to make it east this thing.
The current code you've posted appears to be that you are actually requesting to use the STREAM_MUSIC stream rather than detecting it. I believe you may have two options for this.
I haven't tried is this method, but it sounds like AudioManager.isMusicActive() may work for you.
From this page, it says
Checks whether any music is active.
Returns true if any music tracks are active.
Your other option is to use AudioManager.OnAudioFocusChangeListener. You must first create this listener and then register it to detect changes. This means your app would need to be running in the background before another app started using the stream.
Create a listener (examples from http://developer.android.com/training/managing-audio/audio-focus.html)
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT) {
// another app took the stream temporarily
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// another app let go of the stream
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
// another app took full control of the stream
}
}
};
Now you can register the listener and detect changes
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// You successfully took control of the stream and can detect any future changes
}
This isn't a terribly elegant solution since you will most likely take away the music stream from other apps. It depends on your use case, but it may be a starting point.

Android Chromecast: Call messageStream.playFrom(position) does not work in MediaProtocolCommand onCompleted

I had a media player, when user connect to chromecast in the middle of the video, I would like to cast the video to chromecast at the same position in the video where the user left off.
mediaProtocolCommand.setListener(new MediaProtocolCommand.Listener() {
public void onCompleted(MediaProtocolCommand mPCommand) {
messageStream.playFrom(currentVideoPosition);
}
});
Using the code snippet above I manage to get the video playing but it always start at the beginning of the video
Works fine for me using the Cast Android sample app. Make sure the parameter value for playFrom is the time in seconds.

Categories

Resources