请问最后解决了吗
播放音乐onTimeUpdate不会自动执行,必须点击后才执行需要实现一个播放器加载后自动播放音乐。开发工具模拟器上,可以自动播放出声音,时间和进度条不会变化。但是点击播放按钮后可以。真机测试,安卓有些手机可以,有些不行。苹果手机都不可以。 [图片] [图片] [图片] bgMusic.onTimeUpdate()方法为什点击事件会执行,onload事件不能自动执行,开发工具模拟器的效果是这样的。 [代码]var[代码] [代码]config = require([代码][代码]'../../config.js'[代码][代码]);[代码][代码]const bgMusic = wx.createInnerAudioContext()[代码][代码]const app = getApp()[代码][代码]//动画[代码][代码]var[代码] [代码]audioAnimation;[代码][代码]Page({[代码][代码] [代码][代码]data: {[代码][代码] [代码][代码]index: [代码][代码]'2'[代码][代码],[代码][代码] [代码][代码]title_list: [{[代码][代码] [代码][代码]src: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]title: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]id: 0,[代码][代码] [代码][代码]img: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]}],[代码][代码] [代码][代码]show: [代码][代码]'none'[代码][代码], [代码][代码]//列表弹窗显隐[代码][代码] [代码][代码]isOpen: [代码][代码]true[代码][代码], [代码][代码]//播放开关[代码][代码] [代码][代码]starttime: [代码][代码]'00:00'[代码][代码], [代码][代码]//正在播放时长[代码][代码] [代码][代码]duration: [代码][代码]'00:00'[代码][代码], [代码][代码]//总时长[代码][代码] [代码][代码]music_on: [代码][代码]true[代码][代码],[代码][代码]//音乐是不是在播放 [代码][代码] [代码][代码]music_playing: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]},[代码][代码] [代码][代码]onLoad: [代码][代码]function[代码] [代码](options) {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码] [代码][代码]var[代码] [代码]classid = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]id = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]id: options.id,[代码][代码] [代码][代码]classid: options.classid,[代码][代码] [代码][代码]})[代码][代码] [代码][代码]// 获取应的音频[代码][代码] [代码][代码]that.s_p();[代码][代码] [代码] [代码] [代码][代码]},[代码][代码] [代码][代码]//获取数据[代码][代码] [代码][代码]s_p: [代码][代码]function[代码] [代码]() {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]classid = that.data.classid;[代码][代码] [代码][代码]var[代码] [代码]id = that.data.id;[代码][代码] [代码][代码]var[代码] [代码]network = require([代码][代码]'../../utils/network.js'[代码][代码]); [代码][代码]//引用加载提示效果[代码][代码] [代码][代码]network.request({[代码][代码] [代码][代码]url: config.apiurl + [代码][代码]'/?api=listenStory/detail&id='[代码] [代码]+ id + [代码][代码]'&classid='[代码] [代码]+ classid, [代码][代码]//调用接口[代码][代码] [代码][代码]data: {[代码][代码] [代码][代码]'appid'[代码][代码]: config.appid, [代码][代码]// 传递appid[代码][代码] [代码][代码]'timestamp'[代码][代码]: config.timestamp, [代码][代码]// timestamp[代码][代码] [代码][代码]'signature'[代码][代码]: config.signature, [代码][代码]// signature[代码][代码] [代码][代码]},[代码][代码] [代码][代码]method: [代码][代码]'POST'[代码][代码],[代码][代码] [代码][代码]header: {[代码][代码] [代码][代码]'content-type'[代码][代码]: [代码][代码]'application/json'[代码][代码],[代码][代码] [代码][代码]},[代码][代码] [代码][代码]success: [代码][代码]function[代码] [代码](res) {[代码][代码] [代码][代码]console.log(res.data);[代码][代码] [代码][代码]var[代码] [代码]audiolist = that.data.audiolist;[代码][代码] [代码][代码]that.data.title_list[0].src = res.data.data.content.mp3link;[代码][代码] [代码][代码]//that.data.title_list[2].img = res.data.data.content.titlepic;[代码][代码] [代码][代码]var[代码] [代码]src = res.data.data.content.mp3link;[代码][代码] [代码][代码]var[代码] [代码]img = res.data.data.content.titlepic;[代码][代码] [代码][代码]var[代码] [代码]title = res.data.data.content.title;[代码][代码] [代码][代码]var[代码] [代码]clickId = res.data.data.content.id;[代码][代码] [代码][代码]var[代码] [代码]classname=res.data.data.content.classname;[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]newsdata: res.data.data.content,[代码][代码] [代码][代码]'color'[代码][代码]: [代码][代码]'#0b68f3'[代码][代码],[代码][代码] [代码][代码]'clickId'[代码][代码]: clickId,[代码][代码] [代码][代码]'src'[代码][代码]:src,[代码][代码] [代码][代码]'img'[代码][代码]:img,[代码][代码] [代码][代码]'title'[代码][代码]:title,[代码][代码] [代码][代码]'classname'[代码][代码]:classname,[代码][代码] [代码][代码]});[代码][代码] [代码][代码]//播放音频[代码][代码] [代码][代码]that.listenerButtonPlay();[代码][代码] [代码][代码]//设置页面标题//[代码][代码] [代码][代码]wx.setNavigationBarTitle({[代码][代码] [代码][代码]title:classname,[代码][代码] [代码][代码]})[代码] [代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]// 播放[代码][代码] [代码][代码]listenerButtonPlay: [代码][代码]function[代码] [代码](e) {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码] [代码][代码]//bug ios 播放时必须加title 不然会报错导致音乐不播放 [代码][代码] [代码][代码]var[代码] [代码]sec = [代码][代码]this[代码][代码].data.src[代码][代码] [代码][代码]bgMusic.title = that.data.title;[代码][代码] [代码][代码]bgMusic.epname = that.data.classname;[代码][代码] [代码][代码]bgMusic.src = that.data.src;[代码][代码] [代码][代码]this[代码][代码].data.music_on = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]this[代码][代码].data.music_playing = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]bgMusic.play(); [代码][代码]//图片添加css样式,旋转样式 [代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]music_on: [代码][代码]true[代码][代码],[代码][代码] [代码][代码]music_playing: [代码][代码]this[代码][代码].data.music_playing,[代码][代码] [代码][代码]})[代码][代码] [代码][代码]bgMusic.onTimeUpdate(() => {[代码][代码] [代码][代码]var[代码] [代码]duration = bgMusic.duration;[代码][代码] [代码][代码]var[代码] [代码]offset = bgMusic.currentTime;[代码][代码] [代码][代码]var[代码] [代码]currentTime = parseInt(bgMusic.currentTime);[代码][代码] [代码][代码]var[代码] [代码]min = [代码][代码]"0"[代码] [代码]+ parseInt(currentTime / 60);[代码][代码] [代码][代码]var[代码] [代码]max = parseInt(bgMusic.duration);[代码][代码] [代码][代码]var[代码] [代码]sec = currentTime % 60;[代码][代码] [代码][代码]var[代码] [代码]maxtime = max / 60[代码][代码] [代码][代码]var[代码] [代码]max_time = parseInt(maxtime);[代码][代码] [代码][代码]var[代码] [代码]x_maxtime = maxtime - max_time;[代码][代码] [代码][代码]var[代码] [代码]yu_time = parseInt(x_maxtime * 60)[代码][代码] [代码][代码]if[代码] [代码](sec < 10) {[代码][代码] [代码][代码]sec = [代码][代码]"0"[代码] [代码]+ sec;[代码][代码] [代码][代码]};[代码][代码] [代码][代码]var[代码] [代码]starttime = min + [代码][代码]':'[代码] [代码]+ sec; [代码][代码]/* 00:00 */[代码][代码] [代码][代码]var[代码] [代码]time_time = max_time + [代码][代码]':'[代码] [代码]+ yu_time[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]isOpen: [代码][代码]true[代码][代码],[代码][代码] [代码][代码]duration: time_time,[代码][代码] [代码][代码]offset: currentTime,[代码][代码] [代码][代码]starttime: starttime,[代码][代码] [代码][代码]max: max,[代码][代码] [代码][代码]changePlay: [代码][代码]true[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]})[代码][代码] [代码][代码]// 播放出错[代码][代码] [代码][代码]bgMusic.onError((res) => {[代码][代码] [代码][代码]that.prompt.message([代码][代码]'网络出错,稍后再试'[代码][代码])[代码][代码] [代码][代码]})[代码][代码] [代码][代码]// 播放结束[代码][代码] [代码][代码]bgMusic.onEnded((res) => {[代码][代码] [代码][代码]// console.log('录音播放结束');[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码] [代码][代码]var[代码] [代码]id = parseInt([代码][代码]this[代码][代码].data.index)[代码][代码] [代码][代码]var[代码] [代码]num = id + 1[代码] [代码] [代码][代码]if[代码] [代码](id + 1 > [代码][代码]this[代码][代码].data.title_list.length - 1) {[代码][代码] [代码][代码]wx.showToast({[代码][代码] [代码][代码]title: [代码][代码]'已是最后一个'[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]this[代码][代码].setData({[代码][代码] [代码][代码]music_on: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]index: num,[代码][代码] [代码][代码]'color'[代码][代码]: [代码][代码]'#0b68f3'[代码][代码],[代码][代码] [代码][代码]'clickId'[代码][代码]: num,[代码][代码] [代码][代码]src: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]duration: [代码][代码]''[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]that.s_p()[代码][代码] [代码][代码]}[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]//暂停播放[代码][代码] [代码][代码]listenerButtonPause() {[代码][代码] [代码][代码]this[代码][代码].data.music_on = [代码][代码]true[代码][代码];[代码][代码] [代码][代码]this[代码][代码].data.music_playing = [代码][代码]false[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码] [代码][代码]bgMusic.pause()[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]isOpen: [代码][代码]false[代码][代码],[代码][代码] [代码][代码]music_on: [代码][代码]this[代码][代码].data.music_on,[代码][代码] [代码][代码]music_playing: [代码][代码]this[代码][代码].data.music_playing,[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码][代码]// 进度条拖拽[代码][代码] [代码][代码]sliderChange(e) {[代码][代码] [代码][代码]var[代码] [代码]that = [代码][代码]this[代码][代码] [代码][代码]var[代码] [代码]offset = parseInt(e.detail.value);[代码][代码] [代码][代码]bgMusic.play();[代码][代码] [代码][代码]bgMusic.seek(offset);[代码][代码] [代码][代码]that.setData({[代码][代码] [代码][代码]isOpen: [代码][代码]true[代码][代码],[代码][代码] [代码][代码]})[代码][代码] [代码][代码]},[代码][代码] [代码] [代码]})[代码]
2020-12-22