收藏
回答

为何调用wx.playbackgroundaudio(object)无法播放音乐

问题模块 框架类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本
API和组件 小程序 wx.playbackgroundaudio(object) 客户端 6.6.3 1.9.91

本地//data.js

var local_database = [

{

play_title: '01.西晋越窑青瓷双系罐',

play_img: '../../images/play_img.png',

time_img: '../../images/time_img.png',

time_wz: '00:55',

last_img: '../../images/last_img.jpg',

gelixian: '../../images/middle.png',


title_img_src: "../../../images/1.jpg",

xiangqing_play_img: "../../../images/play_img.png",

xiangqing_play_stop: "../../../images/play_stop.png",

xiangqing_play_wz: "01.西晋越窑青瓷双系罐",

xiangqing_play_wz_1: "西晋青瓷双系罐,1981年慈溪石堰公社立新大队出土。直口,丰肩,圆鼓腹,平底内凹。肩部刻划弦纹数条,饰网格带纹一周,上置半环形竖系一对,贴印佛像铺首一对。通体施青釉,略偏黄,近底处无釉。贴印铺首、釉不及底等都是西晋越窑青瓷罐的特点之一。",

postId: 0,

music: {

url: "https://shanglinhu-1256137439.cos.ap-shanghai.myqcloud.com/yuyin/changzhan/01.%E8%A5%BF%E6%99%8B%E8%B6%8A%E7%AA%91%E9%9D%92%E7%93%B7%E5%8F%8C%E7%B3%BB%E7%BD%90.m4a",

title:"01.西晋越窑青瓷双系罐",

}

},




//xiangqing.js

onMusicTap: function (event) {

var currentPostId = this.data.currentPostId;

var xiangqing = post_play.post_list[currentPostId];

var isPlayingMusic = this.data.isPlayingMusic;

if (isPlayingMusic) {

wx.pauseBackgroundAudio();

this.setData({

isPlayingMusic: false

})

}


else {

wx.playBackgroundAudio({

dataUrl: 'xiangqing.music.url',

title:'xiangqing.music.title'

})

this.setData({

isPlayingMusic: true

})

}

}


播放不了

最后一次编辑于  2018-03-01  (未经腾讯允许,不得转载)
邀请回答
复制链接收藏投诉关注问题回答

8 个回答

  • 仙森ღ₅₂₀¹³¹⁴
    仙森ღ₅₂₀¹³¹⁴
    2018-03-01

    dataUrl是一个字符串?

    2018-03-01
    赞同
    回复
  • 小宋
    小宋
    2018-03-01

    是一个地址

    2018-03-01
    赞同
    回复
  • 小宋
    小宋
    2018-03-01

    如果dataUrl没有引号 就会报错

    报错如下

    thirdScriptError

    Cannot read property 'music' of undefined;at pages/yuyin/xiangqing/xiangqing page onMusicTap function

    TypeError: Cannot read property 'music' of undefined

        at e.onMusicTap (http://127.0.0.1:59971/appservice/pages/yuyin/xiangqing/xiangqing.js:65:27)

        at e.<anonymous> (http://127.0.0.1:59971/appservice/__dev__/WAService.js:15:21527)

        at e.a (http://127.0.0.1:59971/appservice/__dev__/WAService.js:15:16414)

        at J (http://127.0.0.1:59971/appservice/__dev__/WAService.js:15:10828)

        at Function.<anonymous> (http://127.0.0.1:59971/appservice/__dev__/WAService.js:15:12417)

        at http://127.0.0.1:59971/appservice/__dev__/WAService.js:15:17917

        at http://127.0.0.1:59971/appservice/__dev__/WAService.js:6:13087

        at e.(anonymous function) (http://127.0.0.1:59971/appservice/__dev__/WAService.js:4:2997)

        at a (http://127.0.0.1:59971/appservice/appservice:1156:9451)

        at e.registerCallback (http://127.0.0.1:59971/appservice/appservice:1156:9638)


    2018-03-01
    赞同
    回复
  • 仙森ღ₅₂₀¹³¹⁴
    仙森ღ₅₂₀¹³¹⁴
    2018-03-01

    改成不加引号的 this.data.local_database[0].music.url  试试

    2018-03-01
    赞同
    回复
  • 小宋
    小宋
    2018-03-01

    谢谢

    2018-03-01
    赞同
    回复
  • 仙森ღ₅₂₀¹³¹⁴
    仙森ღ₅₂₀¹³¹⁴
    2018-03-01

    可以播放吗?

    2018-03-01
    赞同
    回复
  • 小宋
    小宋
    2018-03-01

    var post_play = require('../../../data/post-data.js');

    var app = getApp();

    // 获取本地数据

    Page({

    data: {


    },


    onLoad: function (option) {

    var postId = option.id;

    // 从yuyin传递来id值

    this.data.currentPostId = postId;

    var xiangqing = post_play.post_list[postId];

    // 得到id文章值

    this.setData({

    xiangqing: xiangqing

    });

    },



    onMusicTap: function (event) {

    var currentPostId = this.data.currentPostId;

    var postData = post_play.post_list[currentPostId];

    var isPlayingMusic = this.data.isPlayingMusic;

    if (isPlayingMusic) {

    wx.pauseBackgroundAudio();

    this.setData({

    isPlayingMusic: false

    })

    app.globalData.g_isPlayingMusic = false;

    }


    else {

    wx.playBackgroundAudio({

    dataUrl: postData.music.url,

    title: postData.music.title,

    coverImgUrl: postData.music.coverImg,

    })

    this.setData({

    isPlayingMusic: true

    })

    app.globalData.g_currentMusicPostId = this.data.currentPostId;

    app.globalData.g_isPlayingMusic = true;

    }

    }


    })



    当我把

    onMusicTap: function (event) {

    var currentPostId = this.data.currentPostId;

    var postData = post_play.post_list[currentPostId];

    中的xiangqing改成postData后 问题就解决了

    2018-03-01
    赞同
    回复
  • 小宋
    小宋
    2018-03-01

    改成不加引号的 this.data.local_database[0].music.url  这个不行 还是无法播放

    2018-03-01
    赞同
    回复