管理中心
我的
指南线性马达能力自定义振动类型自定义对象作为输入参数

自定义对象作为输入参数

本文导读

开发步骤

  1. 导入需要的包。
    import com.huawei.devices.hapticsengine.HapticAttributes;
    import com.huawei.devices.hapticsengine.HapticPlayer;
    import com.huawei.devices.hapticsengine.HapticChannel; 
    import com.huawei.devices.hapticsengine.HapticCurve; 
    import com.huawei.devices.hapticsengine.HapticWave;
  2. 创建HapticPlayer对象。
    HapticPlayer mHapticPlayer = new HapticPlayer(this);
  3. 自定义参数设置振动波形。
    private boolean defineWave() {
        HapticChannel channel = new HapticChannel.Builder() 
            // 通过addSlice方法添加马达的振动单元,最大为64个 
            .addSlice(0, HapticChannel.SLICE_TYPE_CONTINUOUS, 3000, 0.25f, 0.3f)
            .addSlice(0, HapticChannel.SLICE_TYPE_CONTINUOUS, 4000, 0.35f, 0.5f)
            // 通过setIntensityCurve方法设置马达的静态强度调节曲线,最大为64个
            .setIntensityCurve(new HapticCurve.Builder().addAdjustPoint(300, 0.9f).addAdjustPoint(1000, 0.2f).build())
            // 通过setSharpnessCurve方法设置马达的静态锐度调节曲线,最大为64个
            .setSharpnessCurve(new HapticCurve.Builder().addAdjustPoint(300, 0.9f).addAdjustPoint(1000, 0.2f).build())  
            .setChannelId(HapticChannel.HAPTIC_CHANNEL_A)
            .build();
        HapticWave wave = new HapticWave();
        wave.addHapticChannel(channel);
        HapticAttributes attr = new HapticAttributes();
        attr.setUsage(HapticAttributes.USAGE_GAME);
        return mHapticPlayer.setHapticWave(attr, wave);
    }
  4. 设置循环振动与振动播放。
    1. 设置循环播放,true是循环播放,false是停止循环播放。
      mHapticPlayer.setLooping(true);
    1. 查询是否循环播放。
      boolean isLoop = mHapticPlayer.isLooping();
  5. 启动振动播放。
    1. 启动播放。
      int ret = mHapticPlayer.play();
    2. 查询播放状态。
      boolean isPlaying = mHapticPlayer.isPlaying();
    3. 查询播放时长。
      int duration = mHapticPlayer.getDuration();
  6. (可选) 如果需要动态调节振动波形,参见动态调节振动波形。
  7. 停止振动播放。
    mHapticPlayer.stop();