收藏
回答

播放音乐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,
    })
  },
  
 
})


回答关注问题邀请回答
收藏

2 个回答

登录 后发表内容