js中出现 Cannot read property 'play' of nul?

小程序 文章 2021-12-16 10:00 9 0 全屏看文

js中出现 Cannot read property 'play' of nul?Cannot read property'play' of nul appears in js?

js中出现 Cannot read property 'play' of nul?

请大神回复:

const app = getApp()


Page({

  data:{

    isPlayingMusic:false,

  

  },

  bgm:null,

  music_url:'/images/易言 - 太阳.mp3',

  music_coverImgUrl:'/images/50.jpg',

  OnReady:function(){

    this.bgm = wx.getBackgroundAudioManager()


    this.bgm.title='太阳'

    this.bgm.epname='abc'

    this.bgm.singer='易言'

    this.bgm.coverImgUrl = this.music_coverImgUrl

    this.bgm.onCanplay(()=>{

      this.bgm.pause()

    })

    this.bgm.src = this.music_url

  },

  play:function(){

    if(this.data.isPlayingMusic){

      this.bgm.pause()

    }else{

      this.bgm.play()

    }

    this.setData({

      isPlayingMusic: ! this.data.isPlayingMusic

    })

  }

})



请大神回复: const app = getApp() Page({ data:{ isPlayingMusic:false, }, bgm:null, music_url:'/images/易言 - 太阳.mp3', music_coverImgUrl:'/images/50.jpg', OnReady:function(){ this.bgm = wx.getBackgroundAudioManager() this.bgm.title='太阳' this.bgm.epname='abc' this.bgm.singer='易言' this.bgm.coverImgUrl = this.music_coverImgUrl this.bgm.onCanplay(()=>{ this.bgm.pause() }) this.bgm.src = this.music_url }, play:function(){ if(this.data.isPlayingMusic){ this.bgm.pause() }else{ this.bgm.play() } this.setData({ isPlayingMusic: ! this.data.isPlayingMusic }) } })

回答:

Weichat:

bgm未被实例化,OnReady内方法未被执行

莫若:感谢!
xxxidream:

onReady大小写有问题,另外调用方法时最好判断一下对象是否为空

莫若:感谢!
A.伙伴营销拓客:

你自行检查一下这段,试试

-EOF-