示例#1
0
        private void ConfigureAudio(bool enable)
        {
            if (enable)
            {
                previousAudioMode = audioManager.Mode;
                // Request audio focus before making any device switch.
                audioManager.RequestAudioFocus(null, Stream.VoiceCall,
                                               AudioFocus.GainTransient);

                /*
                 * Use MODE_IN_COMMUNICATION as the default audio mode. It is required
                 * to be in this mode when playout and/or recording starts for the best
                 * possible VoIP performance. Some devices have difficulties with
                 * speaker mode if this is not set.
                 */
                audioManager.Mode = Mode.InCommunication;

                /*
                 * Always disable microphone mute during a WebRTC call.
                 */
                previousMicrophoneMute      = audioManager.MicrophoneMute;
                audioManager.MicrophoneMute = false;
            }
            else
            {
                audioManager.Mode = previousAudioMode;
                audioManager.AbandonAudioFocus(null);
                audioManager.MicrophoneMute = previousMicrophoneMute;
            }
        }
 public void ReleaseAudioResources()
 {
     if (listener != null)
     {
         audioManager.AbandonAudioFocus(listener);
     }
 }
示例#3
0
        private void SetAudioFocus(bool focused)
        {
            try
            {
                if (AudioManager == null)
                {
                    AudioManager = (AudioManager)Application.Context.GetSystemService(Context.AudioService);
                }

                if (focused)
                {
                    PreviousAudioMode      = AudioManager.Mode;
                    PreviousSpeakerphoneOn = AudioManager.SpeakerphoneOn;

                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                    {
                        var playbackAttributes = new AudioAttributes.Builder()
                                                 .SetUsage(AudioUsageKind.VoiceCommunication)
                                                 .SetContentType(AudioContentType.Speech)
                                                 .Build();

                        var focusRequest = new AudioFocusRequestClass.Builder(AudioFocus.Gain)
                                           .SetAudioAttributes(playbackAttributes)
                                           .SetAcceptsDelayedFocusGain(true)
                                           .SetOnAudioFocusChangeListener(this)
                                           .Build();

                        AudioManager.RequestAudioFocus(focusRequest);
                    }
                    else
                    {
#pragma warning disable 618
                        AudioManager.RequestAudioFocus(this, Stream.VoiceCall, AudioFocus.GainTransient);
#pragma warning restore 618
                    }

                    //Start by setting MODE_IN_COMMUNICATION as default audio mode. It is
                    //required to be in this mode when playout and/or recording starts for
                    //best possible VoIP performance. Some devices have difficulties with speaker mode
                    //if this is not set.
                    AudioManager.SpeakerphoneOn = TypeCall != TypeCall.Audio;

                    //AudioManager.SpeakerphoneOn = false;
                    AudioManager.Mode = Mode.InCommunication;
                }
                else
                {
                    AudioManager.Mode           = PreviousAudioMode;
                    AudioManager.SpeakerphoneOn = PreviousSpeakerphoneOn;
#pragma warning disable 618
                    AudioManager.AbandonAudioFocus(null);
#pragma warning restore 618
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#4
0
 private void GiveUpAudioFocus()
 {
     Logger.Debug("GiveUpAudioFocus");
     if (_audioFocusState == AudioFocusState.Focused)
     {
         if (_audioManager.AbandonAudioFocus(this) == AudioFocusRequest.Granted)
         {
             _audioFocusState = AudioFocusState.NoFocusAndNoHide;
         }
     }
 }
示例#5
0
 void GiveUpAudioFocus()
 {
     LogHelper.Debug(Tag, "giveUpAudioFocus");
     if (audioFocus == AudioFocused)
     {
         if (audioManager.AbandonAudioFocus(this) == AudioFocusRequest.Granted)
         {
             audioFocus = AudioNoFocusNoDuck;
         }
     }
 }
        // REMOVED: addSubtitleSource
        // REMOVED: mPendingSubtitleTracks

        public virtual void stopPlayback()
        {
            if (mMediaPlayer != null)
            {
                mMediaPlayer.Stop();
                mMediaPlayer.Release();
                mMediaPlayer  = null;
                mCurrentState = STATE_IDLE;
                mTargetState  = STATE_IDLE;
                AudioManager am = (AudioManager)mAppContext.GetSystemService(Context.AudioService);
                am.AbandonAudioFocus(null);
            }
        }
示例#7
0
        void Pause()
        {
            Toast.MakeText(ApplicationContext, "Stream Paused", ToastLength.Short).Show();
            player.Pause();
            am.AbandonAudioFocus(this);

            using (var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager)
            {
//				notificationManager.Notify (NotificationID, notification);
                notificationManager.Notify(NotificationID, buildCustomNotification(ServiceActions.Pause));
            }

            currentState = MediaStates.Paused;
        }
 /*
  * release the media player in any state
  */
 public void release(bool cleartargetstate)
 {
     if (mMediaPlayer != null)
     {
         mMediaPlayer.Reset();
         mMediaPlayer.Release();
         mMediaPlayer  = null;
         mCurrentState = STATE_IDLE;
         if (cleartargetstate)
         {
             mTargetState = STATE_IDLE;
         }
         AudioManager am = (AudioManager)this.GetSystemService(Context.AudioService);
         am.AbandonAudioFocus(null);
     }
 }
示例#9
0
 void SetAudioFocus(bool focused)
 {
     if (focused)
     {
         _previousAudioMode      = _audioManager.Mode;
         _previousSpeakerphoneOn = _audioManager.SpeakerphoneOn;
         _audioManager.RequestAudioFocus(null, Stream.VoiceCall, AudioFocus.GainTransient);
         _audioManager.SpeakerphoneOn = true;
         _audioManager.Mode           = Mode.InCommunication;
     }
     else
     {
         _audioManager.Mode           = _previousAudioMode;
         _audioManager.SpeakerphoneOn = _previousSpeakerphoneOn;
         _audioManager.AbandonAudioFocus(null);
     }
 }
示例#10
0
 /*
  * release the media player in any state
  */
 public virtual void release(bool cleartargetstate)
 {
     if (mMediaPlayer != null)
     {
         mMediaPlayer.Reset();
         mMediaPlayer.Release();
         mMediaPlayer = null;
         // REMOVED: mPendingSubtitleTracks.clear();
         mCurrentState = STATE_IDLE;
         if (cleartargetstate)
         {
             mTargetState = STATE_IDLE;
         }
         AudioManager am = (AudioManager)mAppContext.GetSystemService(Context.AudioService);
         am.AbandonAudioFocus(null);
     }
 }
示例#11
0
 void SetAudioFocus(bool focused)
 {
     try
     {
         if (focused)
         {
             _previousAudioMode      = _audioManager.Mode;
             _previousSpeakerphoneOn = _audioManager.SpeakerphoneOn;
             _audioManager.RequestAudioFocus(null, Stream.VoiceCall, AudioFocus.GainTransient);
             _audioManager.SpeakerphoneOn = false;
             _audioManager.Mode           = Mode.InCommunication;
         }
         else
         {
             _audioManager.Mode           = _previousAudioMode;
             _audioManager.SpeakerphoneOn = _previousSpeakerphoneOn;
             _audioManager.AbandonAudioFocus(null);
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e);
     }
 }
示例#12
0
        public override void OnCallStateChanged([GeneratedEnum] CallState state, string incomingNumber)
        {
            base.OnCallStateChanged(state, incomingNumber);

            try
            {
                switch (state)
                {
                case CallState.Idle:
                    Log.Info(TAG, "OnCallStateChanged: Phone is idle");
                    if (_audioManager != null)
                    {
                        _audioManager.Mode = Mode.Normal;
                        Log.Info(TAG, "OnCallStateChanged: Speaker phone set to OFF");
                        _audioManager.SpeakerphoneOn = false;
                        _audioManager.AbandonAudioFocus(this);
                    }
                    if (_previousCallState == CallState.Offhook)
                    {
                        // we had a call and it has ended, in this api we have no way of knowing why
                        // as there are only three call states:
                        // Idle
                        // OffHook
                        // Ringing
                        //
                        // Ringing is no good to us - we are making outgoing emergency calls
                        // Idle is self-explanatory
                        // Off hook occurs as soon as we start dialling
                        //
                        // Q: What happens if the called number is busy?  How many (roughly) ticks pass before the OS kicks the call back to idle?
                        // Q: What happens when the phone rings and goes to Voicemail?
                        // To support back to Ice-Cream-Sandwich (API 15) we must use the Telephony API, as we are here
                        // The Telecom API was added in Marshmallow (API 23) which is no good to us either as we will cut most of the user base out
                        // By supporting that
                        // Perhaps we can support both in a later version of MYM
                        _ticksOnCall = DateTime.Now.Ticks - StartTick;
                        Log.Info(TAG, "OnCallStateChanged: Calling back to parent activity status callback function with " + _ticksOnCall.ToString() + " ticks");
                        ((HelpNowActivity)_activity).CallStatusCallback(_ticksOnCall);
                        _ticksOnCall = 0;
                    }
                    else
                    {
                        _ticksOnCall = 0;
                    }
                    break;

                case CallState.Offhook:
                    Log.Info(TAG, "OnCallStateChanged: Phone is Off the Hook");
                    if (_audioManager != null)
                    {
                        _audioManager.Mode = Mode.InCall;
                        var setting      = GlobalData.Settings.Find(set => set.SettingKey == "EmergencyCallSpeaker");
                        var speakerValue = true;
                        if (setting != null)
                        {
                            speakerValue = (setting.SettingValue == "True" ? true : false);
                        }
                        if (speakerValue == true)
                        {
                            Log.Info(TAG, "OnCallStateChanged: Speaker phone set to ON");
                            _audioManager.SpeakerphoneOn = true;
                        }
                        _previousCallState = CallState.Offhook;
                    }
                    break;

                case CallState.Ringing:
                    Log.Info(TAG, "OnCallStateChanged: Phone is ringing");
                    break;
                }
            }
            catch (Exception e)
            {
                Log.Error(TAG, "OnCallStateChanged: Exception - " + e.Message);
                if (_activity != null)
                {
                    if (GlobalData.ShowErrorDialog)
                    {
                        ErrorDisplay.ShowErrorAlert(_activity, e, _activity.GetString(Resource.String.ErrorOnCallStateChange), "EmergencyPhoneStateListener.OnCallStateChanged");
                    }
                }
            }
        }
示例#13
0
 private bool RemoveAudioFocus()
 {
     return(AudioFocusRequest.Granted ==
            audioManager.AbandonAudioFocus(this));
 }
示例#14
0
    public void stop()
    {
        if (!running)
        {
            return;
        }
        running = false;

        if (echoCanceller != null)
        {
            try
            {
                echoCanceller.Release();
                echoCanceller.Dispose();
            }
            catch (Exception)
            {
            }
            echoCanceller = null;
        }

        if (noiseSuppressor != null)
        {
            try
            {
                noiseSuppressor.Release();
                noiseSuppressor.Dispose();
            }
            catch (Exception)
            {
            }
            noiseSuppressor = null;
        }

        if (audioRecorder != null)
        {
            try
            {
                audioRecorder.Stop();
                audioRecorder.Release();
            }
            catch (Exception)
            {
            }
            audioRecorder.Dispose();
            audioRecorder = null;
        }

        if (audioEncoder != null)
        {
            audioEncoder.stop();
            audioEncoder.Dispose();
            audioEncoder = null;
        }

        buffer       = null;
        shortsBuffer = null;
        bufferSize   = 0;
        lock (outputBuffers)
        {
            outputBuffers.Clear();
        }


        AudioManager am = (AudioManager)MainActivity.Instance.GetSystemService(Context.AudioService);

        if (Build.VERSION.SdkInt < BuildVersionCodes.O)
        {
            if (focusListener != null)
            {
#pragma warning disable CS0618 // Type or member is obsolete
                am.AbandonAudioFocus(focusListener);
#pragma warning restore CS0618 // Type or member is obsolete
                focusListener.Dispose();
                focusListener = null;
            }
        }
        else
        {
            if (focusListener != null)
            {
                if (focusRequest != null)
                {
                    am.AbandonAudioFocusRequest(focusRequest);
                    focusRequest.Dispose();
                    focusRequest = null;
                }
                focusListener.Dispose();
                focusListener = null;
            }
        }
    }