开发者社区
社区提问意见反馈
开发者钉组织
扫描二维码
加入支付宝开发者钉组织
开发 > api > 基础 api > 多媒体 > 音频 > 前景音频 > inneraudiocontext > inneraudiocontext 概览
评论
收藏
订阅更新
我的文档
设置

inneraudiocontext 概览

基础库  或更高版本; 且 支付宝客户端 10.1.87 或更高版本; 若版本较低,建议采取 兼容处理

小程序开发者工具: 不支持。请以真机的调试结果为准。

主体: 企业支付宝小程序个人支付宝小程序

全局唯一的前景(内部)音频管理器。

可通过 my.createinneraudiocontext 获取。

属性

名称 类型 只读 功能说明
duration number

当前音频的长度,单位为秒(s)。

paused boolean

当前是否是暂停或停止状态,true 表示暂停或停止,false 表示正在播放。

currenttime number

当前音频的播放位置,单位为秒(s),该值为浮点数。

buffered number

音频缓冲的时间点,仅保证当前播放时间点到此时间点内容已缓冲。

src string

音频的数据源,默认为空字符串,当设置了新的 src 时,会自动开始播放。
注意

  • 音频的数据源为音频码,并且只支持来源于优酷的音频码。参考 获取优酷码指南 生成音频码。
  • 如果开发者不传入音频码,控制台不会报错,但无音频播放。
starttime number

音频开始播放的位置,默认从 0 开始播放,单位为秒(s)。

playbackrate number

播放速度。范围 0.5 - 2.0,默认为 1。
注意:android 需要 6 及以上版本支持。

autoplay boolean

是否自动开始播放。默认为 false。

loop boolean

是否循环播放,默认为 false。

obeymuteswitch boolean

是否遵循系统静音开关,当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音,默认值为 true。此参数仅 ios 支持。

volume number

音量。范围 0~1。例如:this.inneraudiocontext.volume = 0.5

方法

名称 类型 功能说明
inneraudiocontext.play function

播放前景音频。

inneraudiocontext.pause function

暂停播放前景音频。

inneraudiocontext.stop function

停止播放前景音频。

inneraudiocontext.seek function

跳转到指定位置,单位为秒(s)。精确到小数点后 3 位,即支持毫秒(ms)级别精确度。

inneraudiocontext.destroy function

销毁当前实例。

inneraudiocontext.onwaiting function

监听前景音频进入等待状态(开始缓冲)事件。

inneraudiocontext.offwaiting function

取消监听音频进入等待状态(开始缓冲)事件。

inneraudiocontext.oncanplay function

监听前景音频进入可以播放状态事件。但不保证后面可以流畅播放。

inneraudiocontext.offcanplay function

取消监听前景音频进入可以播放状态。

inneraudiocontext.onplay function

监听前景音频播放事件。

inneraudiocontext.offplay function

取消监听前景音频播放事件。

inneraudiocontext.onpause function

监听前景音频暂停事件。

inneraudiocontext.offpause function

取消监听前景音频暂停事件。

inneraudiocontext.onstop function

监听前景音频停止事件。

inneraudiocontext.offstop function

取消监听前景音频停止事件。

inneraudiocontext.onseeking function

监听前景音频开始播放进度跳转事件。

inneraudiocontext.offseeking function

取消监听前景音频开始播放进度跳转事件。

inneraudiocontext.onseeked function

监听前景音频完成播放进度跳转操作。

inneraudiocontext.offseeked function

取消监听前景音频完成播放进度跳转操作。

inneraudiocontext.onended function

监听前景音频自然播放结束事件。

inneraudiocontext.offended function

取消监听前景音频自然播放结束事件。

inneraudiocontext.onerror function

监听前景音频播放错误事件。

inneraudiocontext.offerror function

取消监听前景音频播放错误事件。

inneraudiocontext.ontimeupdate function

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

inneraudiocontext.offtimeupdate function

取消监听前景音频播放进度更新事件。

播放前景音频完整代码示例
规避第二次进入页面监听事件失效问题示例代码
.axml
.js
<view>
  <view>
    <button
      type="primary"
      ontap="playinneraudio"
    >
      开始播放前景音频
    button>
  view>
  <view>
    <button
      type="primary"
      ontap="pauseinneraudio"
    >
      暂停播放前景音频
    button>
  view>
  <view>
    <button
      type="primary"
      ontap="stopinneraudio"
    >
      停止播放前景音频
    button>
  view>
  <view>
    <button
      type="primary"
      ontap="seekinneraudio"
    >
      前景音频播放进度跳转
    button>
  view>
view>
page ({
  onload () {
    //创建前景音频上下文对象。
    this.inneraudiocontext = my.createinneraudiocontext ();
    //来源于优酷的音频码,用于直接播放。支持音频格式:aac,mp3。如果开发者不传入音频码,控制台不会报错,但无音频播放。
    this.inneraudiocontext.src = 'xndy2nte2mje4na==';
    //是否自动开始播放,默认为 false。
    this.inneraudiocontext.autoplay = false;
    //是否循环播放,默认为 false。
    this.inneraudiocontext.loop = false;
    //是否遵循系统静音开关,当此参数为 false 时,即使用户打开了静音开关,也能继续发出声音,默认值为 true (注意:此参数仅 ios 支持)。
    this.inneraudiocontext.obeymuteswitch = false;
    this.inneraudiocontext.onplay (() => {
      console.log ('inneraudiocontext onplay 开始播放');
      my.alert ({content: 'inneraudiocontext 前景音频播放事件 onplay'});
    });
    this.inneraudiocontext.onpause (() => {
      console.log ('inneraudiocontext onpause 暂停播放');
      my.alert ({content: 'inneraudiocontext 前景音频暂停事件 onpause'});
    });
    this.inneraudiocontext.onstop (() => {
      console.log ('inneraudiocontext onstop 停止播放');
      my.alert ({content: 'inneraudiocontext 前景音频停止事件 onstop'});
    });
    this.inneraudiocontext.onseeking (() => {
      console.log ('inneraudiocontext onseeking 跳转中播放事件');
      my.alert ({
        content: 'inneraudiocontext 前景音频跳转中播放事件 onseeking',
      });
    });
    this.inneraudiocontext.onseeked (() => {
      console.log ('inneraudiocontext onseeked 完成跳转播放事件');
      my.alert ({
        content: 'inneraudiocontext 前景音频完成跳转播放事件 onseeked',
      });
    });
    this.inneraudiocontext.onerror (err => {
      console.log ('inneraudiocontext onerror 前景音频播放错误事件', err);
      my.alert ({content: 'inneraudiocontext 前景音频播放错误事件 onerror'});
    });
  },
  playinneraudio () {
    console.log ('inneraudiocontext.play 播放前景景音频');
    this.inneraudiocontext.play ();
  },
  stopinneraudio () {
    console.log ('inneraudiocontext.stop 停止播放前景音频');
    this.inneraudiocontext.stop ();
  },
  pauseinneraudio () {
    console.log ('inneraudiocontext.pause 暂停播放前景音频');
    this.inneraudiocontext.pause ();
  },
  seekinneraudio () {
    console.log ('inneraudiocontext.seek 跳转到指定位置播放音频');
    // 举例跳到 20 s
    this.inneraudiocontext.seek (20);
  },
});
page ({
  onload () {
    // 页面加载时,注册了 onplay、onstop、ontimeupdate、onerror 监听事件
    this.inneraudiocontext = my.createinneraudiocontext ();
    this.inneraudiocontext.onplay (() => {
      console.log ('inneraudiocontext onplay 开始播放');
    });
    this.inneraudiocontext.onstop (() => {
      console.log ('inneraudiocontext onstop 停止播放');
    });
    this.inneraudiocontext.ontimeupdate (() => {
      console.log ('inneraudiocontext ontimeupdate 前景音频播放进度更新事件');
    });
    this.inneraudiocontext.onerror (err => {
      console.log ('inneraudiocontext onerror 前景音频播放错误事件', err);
    });
  },
  onunload () {
    // 页面卸载时,卸载已监听的音频事件
    this.inneraudiocontext.offplay ();
    this.inneraudiocontext.offstop ();
    this.inneraudiocontext.onerror ();
    this.inneraudiocontext.offtimeupdate ();
  },
});
  • tip
    支付宝提供了简单的快速示例,可以点此下载 。
  • bug
    支付宝客户端版本 10.5.50 之前,退出创建 inneraudiocontext 的页面时,如果存在任一音频事件监听没有取消,在页面退出这些事件监听函数后不会再被触发,并且后续在新的页面创建的 inneraudiocontext 实例上所有事件监听也都会无效。规避办法:在页面卸载时将已注册的音频监听事件全部都 off 掉,参考 示例代码
内容没有解决您的问题?您还可以前往 或 寻求帮助
凯发k8官方网娱乐官方 copyright © 2022 支付宝(杭州)信息技术有限公司  | icp证浙b2-20160559
该文档对你是否有帮助?
当前页面目录
网站地图