智能小程序 Ray 开发媒体 API —— 音频 API 合集(一)

CreateInnerAudioContextTask createInnerAudioContext

创建内部 audio 上下文 InnerAudioContext 对象

引入

import { createInnerAudioContext } from '@ray-js/ray';

参数

Object object

创建内部 audio 上下文 InnerAudioContext 对象的回调函数

回调参数 Object res

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

object.success 回调参数

参数

Object res

object.fail 回调参数

参数

Object res

属性类型说明
errorMsgstring插件错误信息
errorCodestring错误码
innerErrorobject插件外部依赖错误信息 {errorMsg: string, errorCode: string }

返回值

CreateInnerAudioContextTask 创建内部 audio 上下文 InnerAudioContext 对象

函数定义示例

/**
 * 创建内部audio上下文InnerAudioContext对象
 */
interface CreateInnerAudioContextTask {
  /**
   * 暂停
   */
  pause(params: {
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 恢复
   */
  resume(params: {
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 播放
   */
  play(params: {
    /** src 音频资源的地址 */
    src: string;
    /** startTime 开始播放的位置 */
    startTime?: number;
    /** autoplay 是否自动开始播放 */
    autoplay?: boolean;
    /** 是否循环播放,默认为 false */
    loop?: boolean;
    /** 音量。范围 0~1。默认为 1 */
    volume?: number;
    /** 播放速度。范围 0.5-2.0,默认为 1。(Android 需要 6 及以上版本) */
    playbackRate?: number;
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 跳转到指定位置
   */
  seek(params: {
    /** 跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度 */
    position?: number;
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 停止。停止后的音频再播放会从头开始播放
   */
  stop(params: {
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 销毁当前实例
   */
  destroy(params: {
    complete?: () => void;
    success?: (params: null) => void;
    fail?: (params: {
      errorMsg: string;
      errorCode: string | number;
      innerError: {
        errorCode: string | number;
        errorMsg: string;
      };
    }) => void;
  }): void;
 
  /**
   * 监听音频播放进度更新事件
   */
  onTimeUpdate(
    listener: (params: {
      /** InnerAudioContext 对象 ContextId */
      contextId: string;
      /** 播放进度 【0 - 1】 */
      time: number;
    }) => void,
  ): void;
}
export function createInnerAudioContext(params?: {
  complete?: () => void;
  success?: (params: null) => void;
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): CreateInnerAudioContextTask;

CreateInnerAudioContextTask

CreateInnerAudioContextTask.pause

暂停

参数

Object object

暂停的回调函数

回调参数 Object res

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 暂停
 */
export function pause(params: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

CreateInnerAudioContextTask.resume

恢复

参数

Object object

恢复的回调函数

回调参数 Object res

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 恢复
 */
export function resume(params: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

CreateInnerAudioContextTask.play

播放

参数

Object object

播放的回调函数

回调参数 Object res

属性类型默认值必填说明
srcstringsrc 音频资源的地址
startTimenumberstartTime 开始播放的位置
autoplaybooleanautoplay 是否自动开始播放
loopboolean是否循环播放,默认为 false
volumenumber音量。范围 0~1。默认为 1
playbackRatenumber播放速度。范围 0.5-2.0,默认为 1。(Android 需要 6 及以上版本)
completefunction接口调用完成的回调函数(成功或失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 播放
 */
export function play(params: {
  /** src 音频资源的地址 */
  src: string;
  /** startTime 开始播放的位置 */
  startTime?: number;
  /** autoplay 是否自动开始播放 */
  autoplay?: boolean;
  /** 是否循环播放,默认为 false */
  loop?: boolean;
  /** 音量。范围 0~1。默认为 1 */
  volume?: number;
  /** 播放速度。范围 0.5-2.0,默认为 1。(Android 需要 6 及以上版本) */
  playbackRate?: number;
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

CreateInnerAudioContextTask.seek

跳转到指定位置

参数

Object object

跳转到指定位置的回调函数

回调参数 Object res

属性类型默认值必填说明
positionnumber跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 跳转到指定位置
 */
export function seek(params: {
  /** 跳转的时间,单位 s。精确到小数点后 3 位,即支持 ms 级别精确度 */
  position?: number;
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

CreateInnerAudioContextTask.stop

停止。停止后的音频再播放会从头开始播放

参数

Object object

停止。停止后的音频再播放会从头开始播放的回调函数

回调参数 Object res

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 停止。停止后的音频再播放会从头开始播放
 */
export function stop(params: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

CreateInnerAudioContextTask.destroy

销毁当前实例

参数

Object object

销毁当前实例的回调函数

回调参数 Object res

属性类型默认值必填说明
completefunction接口调用结束的回调函数(调用成功、失败都会执行)
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数

函数定义示例

/**
 * 销毁当前实例
 */
export function destroy(params: {
  /** 接口调用结束的回调函数(调用成功、失败都会执行) */
  complete?: () => void;
  /** 接口调用成功的回调函数 */
  success?: (params: null) => void;
  /** 接口调用失败的回调函数 */
  fail?: (params: {
    errorMsg: string;
    errorCode: string | number;
    innerError: {
      errorCode: string | number;
      errorMsg: string;
    };
  }) => void;
}): void;

CreateInnerAudioContextTask.onTimeUpdate

监听音频播放进度更新事件

参数

function callback

监听音频播放进度更新事件的回调函数

回调参数 Object res

属性类型默认值必填说明
contextIdstringInnerAudioContext 对象 ContextId
timenumber播放进度 【0 - 1】

👉 立即免费领取开发资源,体验涂鸦 MiniApp 小程序开发。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IoT砖家涂拉拉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值