收藏
回答

哪位大神帮忙看看,我是哪里写错了吗?

// pages/player/player.js
const app = getApp().globalData
const song = require('../../utils/song.js')
const Lyric = require('../../utils/lyric.js')


let watch;


Page({


  /**
   * 页面的初始数据
   */
  data: {
    playurl'',
    playIcon : 'icon-play',
    cdCls'pause',
    currentLyricnull,
    currentLineNum : 0,
    toLineNum0,
    currentSongnull,
    dotsArraynew Array(2),
    currentDot0
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoadfunction (options{


  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReadyfunction () {


  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShowfunction () {
    console.log('onshow')
    this._init()
    this._getBackPlayfileName().then((res)=>{
      const current = res.res.currentPosition
      this.data.currentLyric.stop()
      this.data.currentLyric.seek(current * 1000)
    }).catch(()=>{


    })
  },


  // 初始化
  _initfunction () {
    let songlist = (app.songlist.length && app.songlist) || wx.getStorageSync('songlist')
    let currentSong = app.songlist[app.currentIndex] || (songlist && songlist[app.currentIndex])
    let duration = currentSong && currentSong.duration


    this.setData({
      currentSong: currentSong,
      durationthis._formaTime(duration),
      songlist: songlist,
      currentIndex: app.currentIndex
    })


    this._getPlayUrl(currentSong.mid)
    this._getLyric(currentSong)
  },


  //  获取北京播放音乐的songmidid
  _getBackPlayfileNamefunction() {
    return new Promise((resolve,reject) => {
      wx.getBackgroundAudioPlayerState({
        successfunction (res{
          var dataUrl = res.dataUrl
          let ret = dataUrl && dataUrl.split('?')[0].split('/')[3]
          resolve({ret,res})
        },
        failfunction (e{
          let ret = false
          reject(ret)
        }
      })
    })
  },


  //  获取播放地址
  _getPlayUrlfunction (songmidid{
    const _this = this
    wx.request({
      url'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?g_tk=5381&inCharset=utf-8&outCharset=utf-8&notice=0&format=jsonp&hostUin=0&loginUin=0&platform=yqq&needNewCode=0&cid=205361747&uin=0&filename=C400${songmidid}.m4a&guid=3913883408&songmid=${songmidid}&callback=callback',
      data: {
        g_tk5381,
        inCharset'utf-8',
        outCharset'utf-8',
        notice:0,
        format:'jsonp',
        hostUin:0,
        loginUin:0,
        platform:'yqq',
        needNewCode:0,
        cid:205361747,
        uin:0,
        filename:'C400${songmidid}.m4a',
        guid:3913883408,
        songmid:songmidid,
        callback:'callback',
      },
      successfunction(res{
        var res1 = res.data.replace("callback(","")
        var res2 = JSON.parse(res1.substring(0,res1.length - 1))
        const playurl = 'http://dl.stream.qqmusic.qq.com/${res2.data.items[0].filename}?vkey=${res2.data.items[0].vkey}&guid=3913883408&uin=0&fromtag=66'
        _this._getBackPlayfileName().then((nowPlay) => {
          if(!(res2.data.items[0].filename === nowPlay.ret)){
            _this._createAudio(playUrl)
          }
        }).catch((err) => {
          _this._createAudio(playUrl)
        })
      }
    })
  },


  // 创建播放器
  createAudiofunction(playUrl{
    wx.playBackgroundAudio({
      dataUrl'playUrl',
      title:this.data.currentSong.name,
      coverImgUrlthis.data.currentSong.image
    })
    // 监听音乐播放
    wx.onBackgroundAudioPlay(() => {
      this.setData({
        playIcon:'icon-pause',
        cdCls:'play'
      })
    })
    //  监听音乐暂停
    wx.onBackgroundAudioPause(() => {
      this.setData({
        playIcon:'icon-play',
        cdCls:'pause'
      })
    })
    // 监听音乐停止
    wx.onBackgroundAudioStop(() => {
      app.currentIndex++
      this._init()
    })
    // 监听播放拿取播放进度
    const manage = wx.getBackgroundAudioManager()
    manage.onTimeUpdate(() => {
      this.setData({
        currentTimethis._formaTime(manage.currentTime),
        percent:manage.currentTime / this.data.currentSong.duration
      })
    })
  },


  // 获取歌词
  _getLyricfunction(currentSong{
    const _this = this
    this._getBackPlayfileName().then((res) => {
      const nowMid = res.ret.split('.')[0].replace('C400','')
      if(!(nowMid === currentSong.mid)){
        if(this.data.currentLyric) {
          this.data.currentLyric.stop && this.data.currentLyric.stop()
        }
        _this._getLyricAction(currentSong)
      }
    }).catch(() => {
      _this._getLyricAction(currentSong)
    })
  },


  // 获取处理歌词
  _getLyricActionfunction (currentSong{
    console.log('获取歌词')
    song._getLyric(currentSong.musicId).then((res) => {
      if(res.data.showapi_res_body.ret_code == 0) {
        const lyric = this._normalizeLyric(res.data.showapi_res_body.lyric)
        const currentLyric = new Lyric(lyric,this.handleLyric)
        this.setData({
          currentLyric:currentLyric
        })
        this.data.currentLyric.play()
      }else {
        console.log('无歌词')
        this.setData({
          currentLyric: {
            lines: [{txt'暂无歌词'}]
          },
          currentText''
        })
      }
    })
  },


  //  去掉歌词中的转义字符
  _normalizeLyricfunction (lyric{
    return lyric.replace(/:/g':').replace(/
/g'\n').replace(/./g'.').replace(/ /g'').replace(/-/g'-').replace(/(/g'(').replace(/)/g')')
  },


  // 歌词滚动回调函数
  handleLyricfunction({lineNum,txt}{
    console.log(lineNum)
    this.setData({
      currentLineNum:lineNum,
      currentText: txt
    })
    if (lineNum > 5) {
      this.setData({
        toLineNum:lineNum - 5
      })
    }
  },
  _formaTimefunction (interval{
    interval = interval | 0
    const minute = interval / 60 | 0
    const second = this._pad(interval % 60)
    return '${minute} : ${second}'
  },
  /*秒前边加0 */
  _pad(num, n = 2) {
    let len = num.toString().length
    while (len < n) {
      num = '0' + num
      len++
    }
    return num
  },


  prevfunction() {
    if ((app.currentIndex - 1) < 0) {
      app.currentIndex = this.data.songslist.length - 1
      this._init()
      return
    }
    app.currentIndex && app.currentIndex--
    this._init()
  },
  nextfunction() {
    if((app.currentIndex + 1) == this.data.songslist.length) {
      app.currentIndex = 0
      this._init()
      return
    }
    app.currentIndex++
    this._init()
  },
  togglePlaying:function() {
    wx.getBackgroundAudioPlayerState({
     successfunction(res{
       var status = res.status
       if (status == 1) {
         wx.pauseBackgroundAudio()
       } else {
         wx.playBackgroundAudio()
       }
     }
    })
    let timer = setInterval(() => {
      if(this.data.currentLyric) {
        this.data.currentLyric.togglePlay()
        clearInterval(timer)
      }
    },20)
  },
  openListfunction() {
    if(!this.data.songslist.length) {
      return
    }
    this.setData({
      translateCls : 'uptranslate'
    })
  },
  closefunction() {
    this.setData({
      translateCls'downtranslate'
    })
  },
  endfunction() {
    console.log(2)
  },
  playthisfunction(e{
    const index = e.currentTarget.dataset.index
    app.currentIndex = index
    this._init()
    this.close()
  },
  changeDotfunction(e{
    this.setData({
      currentDot: e.detail.current
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefreshfunction () {
  },
})


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

5 个回答

  • 月圆
    月圆
    发表于小程序端
    2020-04-22
    好的
    2020-04-22
    有用
    回复
  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    2020-04-21

    给出错误提示了都不看,哎

    2020-04-21
    有用
    回复 1
    • In love
      In love
      2020-04-21
      看了提示了,不会改啊
      2020-04-21
      回复
  • Admin ²º²³
    Admin ²º²³
    2020-04-21

    谢邀:

    1、playurl和playUrl,U字大小写问题

    应为:const playUrl

    2、song._getLyric是不写错了,我看了,可能是this._getLyric?

    如果不是。需要song.js源码定位问题

    2020-04-21
    有用
    回复 2
    • In love
      In love
      2020-04-21
      改成了this,会报下面的错
      2020-04-21
      回复
    • In love
      In love
      2020-04-21
      2020-04-21
      回复
  • 十年
    十年
    2020-04-21

    2020-04-21
    有用
    回复 1
    • In love
      In love
      2020-04-21
      改了,还是报那个错
      2020-04-21
      回复
  • 子不语
    子不语
    2020-04-21

    1.发一下 song.js的代码

    2.注意大小写

    如果觉得有帮助,请点个「有用」。千山万水总是情,别问「尾巴」行不行

    2020-04-21
    有用
    回复 4
    • In love
      In love
      2020-04-21
      改了,但是还是报那个错
      这是song.js的代码
      2020-04-21
      回复
    • 子不语
      子不语
      2020-04-21回复In love
      要不搞个代码片段吧
      https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html

      如果觉得有帮助,请点个「有用」。千山万水总是情,别问「尾巴」行不行

      2020-04-21
      回复
    • In love
      In love
      2020-04-21回复子不语
      额。。。。。。这个我不会弄啊
      2020-04-21
      回复
    • 子不语
      子不语
      2020-04-21回复In love
      仔细看看吧

      如果觉得有帮助,请点个「有用」。千山万水总是情,别问「尾巴」行不行

      2020-04-21
      回复
登录 后发表内容
问题标签