接入声网音视频SDK-呼叫和被呼叫声音

一、前言

声网音视频SDK是没有呼叫声音的api的,可以说声网音视频SDK文档相当简单,它的demo代码也相当简单。很多东西都没有,如超时挂断,如何通知对方,通知栏,浮悬窗等都没有。
它们SDK只管频道内的数据传输,以及用户在频道内的状态

二、SDK不提供,只好我们自己做

需求:呼叫端用一个mp3,
被呼叫端根据手机铃声情景模式操作:
情景模式是

  1. 铃声+震动的,调起手机铃声和震动,并且开扬声器
  2. 铃声的,调起手机铃声,并且开扬声器
  3. 震动的,调起震动
  4. 静音的,即静音

第二个需求:在呼叫或者通话中,暂停其他app的声音,通话结束恢复其他app的声音

三、关键代码:CallRingingUtil

public class CallRingingUtil {
private static final String TAG = "CallRingingUtil";
private volatile boolean isRinging;
private MediaPlayer mMediaPlayer;
private Vibrator mVibrator;
private Context applicationContext;
private volatile boolean stopServiceAndRingingTag;
private static final String DEFAULT_CHANNEL_NAME = "VOIP";
private final Object mLock = new Object();
private AudioManager am;
private AudioManager.OnAudioFocusChangeListener afChangeListener;

private CallRingingUtil() {
    this.isRinging = false;
    this.stopServiceAndRingingTag = false;

}

public static CallRingingUtil getInstance() {
    return CallRingingUtil.InstanceHolder.instance;
}


public void startRinging(Context context, Boolean isCallOutGoing) {
    RLog.d("CallRingingUtil", "startRinging: ");
    if (!this.isRinging) {
        if (context != null) {
            this.applicationContext = context.getApplicationContext();
            if (!isCallOutGoing) {
                //接听方:根据手机情景模式,看静音,震动、响铃、响铃+震动
                AudioManager audio = (AudioManager)context.getSystemService("audio");
                int ringerMode = audio.getRingerMode();
                //0是静音 1是震动 其他是铃声或者是铃声加震动
                if (ringerMode != 0) {
                    if (ringerMode == 1) {
                        this.startVibrator(context);
                    } else {
                        if (this.isVibrateWhenRinging(context)) {
                            this.startVibrator(context);
                        }
                        this.callRinging(context, false);
                    }
                }

            } else {
                this.callRinging(context, true);
            }
            this.isRinging = true;
        }
    }
}



private boolean isVibrateWhenRinging(Context context) {
    ContentResolver resolver = context.getApplicationContext().getContentResolver();
    if (Build.MANUFACTURER.equals("Xiaomi")) {
        return Settings.System.getInt(resolver, "vibrate_in_normal", 0) == 1;
    } else if (Build.MANUFACTURER.equals("smartisan")) {
        return Settings.Global.getInt(resolver, "telephony_vibration_enabled", 0) == 1;
    } else {
        return Settings.System.getInt(resolver, "vibrate_when_ringing", 0) == 1;
    }
}

private void callRinging(Context context, Boolean isCallOutGoing) {
    this.initMp();

    try {

        if (isCallOutGoing){
            int rawResId =  R.raw.voip_outgoing_ring;
            AssetFileDescriptor assetFileDescriptor = context.getResources().openRawResourceFd(rawResId);
            this.mMediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
            assetFileDescriptor.close();
        } else {
            Uri uri = RingtoneManager.getDefaultUri(1);
            this.mMediaPlayer.setDataSource(context.getApplicationContext(), uri);
        }


        if (Build.VERSION.SDK_INT >= 21) {
            AudioAttributes attributes = (new AudioAttributes.Builder()).setUsage(2).build();
            this.mMediaPlayer.setAudioAttributes(attributes);
        } else {
            this.mMediaPlayer.setAudioStreamType(0);
        }

        this.mMediaPlayer.prepareAsync();

        this.afChangeListener =
                new AudioManager.OnAudioFocusChangeListener() {
                    public void onAudioFocusChange(int focusChange) {
                        synchronized (mLock) {
                            RLog.d(TAG, "OnAudioFocusChangeListener " + focusChange);
                            if (am != null
                                    && focusChange == AudioManager.AUDIOFOCUS_LOSS) {
                                am.abandonAudioFocus(afChangeListener);
                                afChangeListener = null;
                                stopRinging();
                            }
                        }
                    }
                };

        am = (AudioManager)context.getApplicationContext().getSystemService("audio");
        if (am != null) {
            am.setSpeakerphoneOn(true);
            am.setMode(0);
            am.setStreamVolume(0, 80, 0);
        }
        //关闭其他app的声音
        muteAudioFocus(am, true);


    } catch (IOException var5) {
        var5.printStackTrace();
    } catch (Exception var6) {
        RLog.i("CallRingingUtil", "---onOutgoingCallRinging Error---" + var6.getMessage());
    }

}

private void initMp() {
    if (this.mMediaPlayer == null) {
        this.mMediaPlayer = new MediaPlayer();
        this.mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            public void onPrepared(MediaPlayer mp) {
                try {
                    if (mp != null) {
                        mp.setLooping(true);
                        mp.start();
                    }
                } catch (IllegalStateException var3) {
                    var3.printStackTrace();
                    RLog.i("CallRingingUtil", "setOnPreparedListener Error!");
                }

            }
        });
    }

}

public void stopRinging() {
    try {
        RLog.d("CallRingingUtil", "stopRinging: ");
        if (!this.stopServiceAndRingingTag) {
            if (this.mMediaPlayer != null && this.mMediaPlayer.isPlaying()) {
                this.mMediaPlayer.stop();
            }

            if (this.mMediaPlayer != null) {
                this.mMediaPlayer.reset();
            }

            if (this.mVibrator != null) {
                this.mVibrator.cancel();
            }

            if (this.applicationContext != null) {

                if (am != null) {
                    am.setMode(AudioManager.MODE_NORMAL);
                    return;
                }
            }

            return;
        }

        this.stopServiceAndRingingTag = false;
    } catch (Exception var7) {
        var7.printStackTrace();
        RLog.i("CallRingingUtil", "mMediaPlayer stopRing error=" + var7.getMessage());
        return;
    } finally {
        this.isRinging = false;

    }

}

public void releaseAudioFocus(){
    if (am != null) {
        am.setMode(AudioManager.MODE_NORMAL);
        muteAudioFocus(am, false);
        am = null;
    }
}

protected void startVibrator(Context context) {
    if (this.mVibrator == null) {
        this.mVibrator = (Vibrator)context.getApplicationContext().getSystemService("vibrator");
    } else {
        this.mVibrator.cancel();
    }

    this.mVibrator.vibrate(new long[]{500L, 1000L}, 0);
}


private static class InstanceHolder {
    static final CallRingingUtil instance = new CallRingingUtil();

    private InstanceHolder() {
    }
}

@TargetApi(Build.VERSION_CODES.FROYO)
private void muteAudioFocus(AudioManager audioManager, boolean bMute) {
    synchronized (mLock) {
        if (audioManager == null) return;

        if (bMute) {
            audioManager.requestAudioFocus(
                    afChangeListener,
                    AudioManager.STREAM_MUSIC,
                    AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
        } else {
            audioManager.abandonAudioFocus(afChangeListener);
            afChangeListener = null;
        }
    }
}}

更多接入声网SDK的功能,可私信我,基本音视频通话中的功能我都有

你可能感兴趣的:(接入声网音视频SDK-呼叫和被呼叫声音)