自定义呼叫铃声

更新时间: 2023/06/06 09:55:42

本文介绍在呼叫组件中如何自定义呼叫铃声,包括被叫铃声、呼叫等待铃声等。

功能介绍

SoundHelper 类主要用于设置呼叫等待时、被叫时的铃声。

实现方法

如果您希望自定义呼叫组件中用户的响铃,请在初始化呼叫组件时,在 CallKitUIOptions 对象中生成 soundHelper 实例,设置呼叫铃声。

操作步骤

  1. 将自定义铃声放在 raw 文件夹下。

  2. soundResources 方法中设置铃声的资源 ID。

    其中 soundResources 方法的 ringerTypeEnum 参数为枚举类型, 组件在内部实现了响铃逻辑,共分为5种类型的铃声,通过 AVChatSoundPlayer.RingerTypeEnum 进行枚举铃声的种类,具体如下:

    类型 说明
    CONNECTING 主叫呼叫提示
    NO_RESPONSE 被叫超时未响应
    PEER_BUSY 被叫忙线
    PEER_REJECT 被叫拒接
    RING 被叫响铃
  3. (可选)自实现铃声播放。

    呼叫组件默认通过 SoundPool 类实现铃声播放,存在一些铃声文件的限制。您可以重写 playstop 方法,实现自己的铃声播放逻辑。

    呼叫组件 V2.0.0 版本才支持该功能。

示例代码(V2.0.0及之后版本)

javaCallKitUIOptions options = new CallKitUIOptions.Builder()
  .soundHelper(new SoundHelper(){
    /**
    * 若用户需要修改铃声,其他不需要变化,若不需要播放则直接返回null
    */
    @Nullable
    @Override
    public Integer soundResources(@NonNull AVChatSoundPlayer.RingerTypeEnum type) {
      return super.soundResources(type);
    }
    /**
    * (可选)用户可在此实现自己的铃声播放逻辑和{@link SoundHelper#stop(Context, AVChatSoundPlayer.RingerTypeEnum)}方法同时修改
    */
    @Override
    public void play(@NonNull Context context, @NonNull AVChatSoundPlayer.RingerTypeEnum type) {
      super.play(context, type);
    }
    /**
    * (可选)用户可在此实现自己的铃声停止逻辑和{@link SoundHelper#play(Context, AVChatSoundPlayer.RingerTypeEnum)}方法同时修改
    */
    @Override
    public void stop(@NonNull Context context, @Nullable AVChatSoundPlayer.RingerTypeEnum type) {
      super.stop(context, type);
    }
  })
// 若重复初始化会销毁之前的初始化实例,重新初始化
CallKitUI.init(getApplicationContext(), options);

示例代码(V1.8.2及之前版本)

SoundHelper#isEnable true 表示响铃,false 表示禁止响铃,您可以通过此方法完成响铃的开关。

javaCallKitUIOptions options = new CallKitUIOptions.Builder()
  .soundHelper(new SoundHelper(){
      @Override
      public boolean isEnable() {
        return true;
      }

    @Nullable
    @Override
    public Integer soundResources(@NonNull AVChatSoundPlayer.RingerTypeEnum type) {
      return super.soundResources(type);
    }
  }
// 若重复初始化会销毁之前的初始化实例,重新初始化
CallKitUI.init(getApplicationContext(), options);
此文档是否对你有帮助?
有帮助
去反馈
  • 功能介绍
  • 实现方法