开发
API

BackgroundAudioManager.play

播放。若通过 pause 暂停播放 ,调用此 API 会从暂停的位置继续播放;若通过 stop 停止播放,或者自动播放结束,调用此 API 后从头开始播放。

语法

BackgroundAudioManager.play()

参数说明

扫码体验

请使用字节宿主APP扫码

代码示例

开发者工具中预览

【代码示例 1】:播放网络音频文件

<!-- index.ttml -->
<button type="primary" bindtap="play">开始播放</button>
// index.js
Page({
  onLoad() {
    const backgroundAudioManager = tt.getBackgroundAudioManager();
    backgroundAudioManager.title = "背景音乐";
    backgroundAudioManager.epname = "测试音频";
    backgroundAudioManager.singer = "测试";
    backgroundAudioManager.coverImgUrl =
      "https://s3.pstatp.com/toutiao/static/img/logo.201f80d.png";
    backgroundAudioManager.src = "https://tosv.byted.org/obj/developer/sdk/0000-0001.mp3";
    this.backgroundAudioManager = backgroundAudioManager;
  },
  play() {
    this.backgroundAudioManager.play();
  },
});

【代码示例 2】:播放本地临时路径音频文件

<!-- index.ttml -->
<button type="primary" bindtap="play">开始播放</button>
// index.js

Page({
  onLoad() {
    const backgroundAudioManager = tt.getBackgroundAudioManager();
    backgroundAudioManager.title = "背景音乐";
    backgroundAudioManager.epname = "测试音频";
    backgroundAudioManager.singer = "测试";
    backgroundAudioManager.coverImgUrl =
      "https://s3.pstatp.com/toutiao/static/img/logo.201f80d.png";
    this.backgroundAudioManager = backgroundAudioManager;
  },
  play() {
    tt.downloadFile({
      url: "https://sf1-ttcdn-tos.pstatp.com/obj/developer/sdk/0000-0001.mp3", // 待下载的音频文件,需要在小程序开发者平台配置downloadFile 合法域名
      success: (res) => {
        if (res.statusCode === 200) {
          // 下载到本地的文件临时地址
          console.log("下载成功", res.tempFilePath);
          this.backgroundAudioManager.src = res.tempFilePath;
          this.backgroundAudioManager.play();
        }
      },
      fail: (err) => {
        console.log("下载失败", res.errMsg);
        tt.showModal({
          title: "下载失败",
          content: err.errMsg,
          showCancel: false,
        });
      },
    });
  },
});

Bug & Tip

暂无

点击纠错
评价此篇文档