tvfe / txv-miniprogram-plugin

这是腾讯视频小程序播放插件。 打广告:腾讯视频小程序开发框架=>
https://github.com/tvfe/wxpage
461 stars 76 forks source link

播放器实例里的方法play()等都无效,创建实例显示是undefined #162

Closed GreatHongyu closed 1 year ago

GreatHongyu commented 1 year ago

我自己封装了播放器 在自定义组件中引用实例 打印video实例显示是undefined 请问是啥问题呢 代码如下

lifetimes: { attached: function () { this.onLoad(), appData.srtTimes = 3; if(iftxv){ store=requirePlugin('txv') video=store.get('txv1x'); console.log('创建txv播放器',video) }else{ video=wx.createVideoContext("video"); console.log('创建myvideo播放器',video) } }, detached: function () {} }

GreatHongyu commented 1 year ago

//js页面中 const store = requirePlugin('txv') const video = store.get('video')

onLoad(options) { print('视频播放器实例',this.video)

}, play(){ video.play() }, pause(){ video.pause() },

//wxml页面中

pause play

点击play自定义控件后,报如下错 VM19030 WAService.js:1 TypeError: Cannot read property 'play' of undefined at Qa.play (index.js:7) at Object.i.safeCallback (VM19030 WAService.js:1) at VM19030 WAService.js:1 at _n (VM19030 WAService.js:1) at VM19030 WAService.js:1 at Y (VM19030 WAService.js:1) at VM19030 WAService.js:1 at VM19030 WAService.js:1 at o.emit (VM19030 WAService.js:1) at D.onMessage (VM19030 WAService.js:1)(env: Windows,mp,1.06.2206090; lib: 2.25.4) errorReport @ VM19030 WAService.js:1 thirdErrorReport @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 i.safeCallback @ VM19030 WAService.js:1 i.call @ VM19030 WAService.js:1 message @ VM19030 WAService.js:1 i.safeCallback @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 _n @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 Y @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 emit @ VM19030 WAService.js:1 (anonymous) @ VM19030 WAService.js:1 dispatch @ VM19030 WAService.js:1 cb @ VM19030 WAService.js:1 a @ VM16488 asdebug.js:10 c @ VM16488 asdebug.js:10 (anonymous) @ VM16488 asdebug.js:1 f @ VM16488 asdebug.js:1 g @ VM16488 asdebug.js:1 (anonymous) @ VM16488 asdebug.js:1 _ws.onmessage @ VM16488 asdebug.js:1 VM19030 WAService.js:1 TypeError: Cannot read property 'pause' of undefined at Qa.pause (index.js:7) at Object.i.safeCallback (VM19030 WAService.js:1) at VM19030 WAService.js:1 at _n (VM19030 WAService.js:1) at VM19030 WAService.js:1 at Y (VM19030 WAService.js:1) at VM19030 WAService.js:1 at VM19030 WAService.js:1 at o.emit (VM19030 WAService.js:1) at D.onMessage (VM19030 WAService.js:1)(env: Windows,mp,1.06.2206090; lib: 2.25.4)

kedanielwu commented 1 year ago

你的wxml里有声明组件吗? requirePlugin应该在代码顶部声明,getContext方法也只是获取到已经被声明的对应playerID的组件,而不是用来初始化的

GreatHongyu commented 1 year ago

哥哥,还是不行啊 图片 图片 图片 图片

问题是我按照腾讯视频插件文档里引入,打印player实例居然显示是undefined

kedanielwu commented 1 year ago

get接口的入参是和playerID相同,不是和id相同

kedanielwu commented 1 year ago
Screen Shot 2022-11-28 at 13 35 01
kedanielwu commented 1 year ago

麻烦直接提供可以复现的微信小程序代码片段吧

GreatHongyu commented 1 year ago

//app.json 中引入插件 "plugins": {

"txv": { "version": "2.1.7", "provider": "wxa75efa648b60994b" } }

//txVideo.json 声明 "usingComponents": { "txv": "plugin://txv/video" }

//txVideo.wxml 使用插件 图片 vid="h0308ot2s1r" id="txv1" playerId="txv0" width="750rpx" '' 'pause' 'play'

//txVideo.js const store = requirePlugin('txv') const player = store.get('txv0') Page({ data: {}, onLoad(options) { console.log('视频播放器实例',player) }, play(){ player.play() }, pause(){ player.pause() },

onReady() { console.log('onReady视频播放器实例',player) }, onShow() { console.log('onShow视频播放器实例',player) } })

//输出结果 图片

//另外,视频插件可以正常播放 图片

kedanielwu commented 1 year ago

很抱歉很抱歉,这里demo写的有点问题。组件上的key是playerid,全小写。

或者你可以用this.selectComponent('#id')的形式获取

GreatHongyu commented 1 year ago

没事,大佬,辛苦了垰fei👍👍👍 但我这儿还是不行,改小写也获取不了player实例,我觉得可能是2.0版本的get接口有问题。你们查查代码

如果在js开始就用const player=this.selectComponent('#tvp-id') 这个语句就报错 图片

我尝试在onready函数里在获取实例,成功了, 图片

但还想请问下,如果我在自定义组件里,没有onready函数的情况下,我应该在哪儿获取play实例呢 我在生命周期attach里获取,也是undefined 获取不到 图片

kedanielwu commented 1 year ago

我这边是可以通过playerid获取到实例的,打开log后可以看到一个player attached的日志,确保你获取的代码是在这个之后就行。

另外组件应该也有ready生命周期,组件包组件的场景我需要晚点试试。没记错的话小程序组件嵌套有点类似洋葱模型,最外层的attach最先触发,最外层的ready最后触发。

GreatHongyu commented 1 year ago

好的,感谢大佬的帮助