为何调用wx.playbackgroundaudio(object)无法播放音乐
发布于 6 年前 作者 jie84 4042 次浏览 来自 问答

本地//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.西晋越窑青瓷双系罐.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

})

}

}

播放不了

8 回复

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

如果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)

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后 问题就解决了

可以播放吗?

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

是一个地址

dataUrl是一个字符串?

回到顶部