videoBox.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <template>
  2. <view class="video-wrap" @click="handleClickWrap">
  3. <video
  4. autoplay
  5. object-fit="contain"
  6. show-mute-btn
  7. enable-play-gesture
  8. :poster="videoInfo.cover_img_url"
  9. :src="videoInfo.video_url"
  10. :id="videoInfo.id"
  11. @ended="handleVideoEnd"
  12. @play="handleVideoPlay"
  13. @pause="handleVideoPause"
  14. @timeupdate="handleTimeUpdate"
  15. @fullscreenchange="handleFullscreenchange"
  16. @controlstoggle="handleControlstoggle"
  17. v-if="videoInfo.id==curVideoId"
  18. >
  19. <!-- 弹幕滚动模块 -->
  20. <view class="danmu-scroll-box" v-show="!closeDM">
  21. <view
  22. :class="[
  23. 'danmu-item',
  24. play?'animat-run':'animat-pause',
  25. item.user_id==selfUserid?'border':''
  26. ]"
  27. v-for="item in danmuList"
  28. :key="item.id"
  29. :style="{color:item.color,top:item.top,animationDuration:item.speed+'s'}"
  30. >{{item.content}}</view>
  31. </view>
  32. <view class="video-inner-right-box" v-if="isShowControls">
  33. <!-- 切换音频播放按钮 -->
  34. <view class="change-music-icon" @click.stop="handleChangeMusic"></view>
  35. <!-- 弹幕控制按钮 -->
  36. <view class="video-danmu-control-box">
  37. <view class="show-btn" v-if="!closeDM" @click.stop="closeDM=true"></view>
  38. <view class="close-btn" v-else @click.stop="closeDM=false"></view>
  39. <view class="send-btn" v-if="!closeDM" @click.stop="showInput=true">发弹幕</view>
  40. </view>
  41. <!-- 倍速控制按钮 -->
  42. <view class="video-speed-btn" @click.stop="showSpeedOpt=true">倍速</view>
  43. </view>
  44. <!-- 倍速选项模块 -->
  45. <view class="speed-opt-box" v-if="showSpeedOpt">
  46. <view
  47. class="item"
  48. :style="{color:item==curSpeed?'#F3A52F':''}"
  49. v-for="item in speedOpts"
  50. :key="item"
  51. @click.stop="handleVideoSpeedChange(item)"
  52. >{{item}}X</view>
  53. </view>
  54. </video>
  55. <image @click="handelClickPlay" v-else class="poster" :src="videoInfo.cover_img_url" mode="aspectFill" lazy-load/>
  56. <!-- 外面弹幕按钮 -->
  57. <view class="danmu-btn-box" v-if="showDanmu">
  58. <view class="big-box" v-if="!closeDM">
  59. <view class="left" @click.stop="showInput=true"></view>
  60. <view class="right" @click.stop="closeDM=true"></view>
  61. </view>
  62. <view class="small-box" v-else @click.stop="handleShowDM"></view>
  63. </view>
  64. <!-- 弹幕输入弹窗 -->
  65. <view class="flex danmu-input-box" :style="{bottom:keyboardheight+'px',paddingLeft:isFullScreen?safeAreaTop+17+'px':'34rpx'}" v-if="showInput">
  66. <view class="flex input-box">
  67. <input
  68. type="text"
  69. v-model="danmuText"
  70. placeholder="发个友善的弹幕见证当下~"
  71. cursor-spacing="20"
  72. maxlength="50"
  73. focus
  74. confirm-type="send"
  75. :adjust-position="false"
  76. @keyboardheightchange="keyboardheightchange"
  77. @confirm="handleSendDanmu"
  78. />
  79. <text>{{danmuText.length}}/50</text>
  80. </view>
  81. <view class="btn" @click="handleSendDanmu">发送</view>
  82. </view>
  83. </view>
  84. </template>
  85. <script>
  86. import {apiVideoDanmuSend} from '@/api/video'
  87. import { debounce } from '@/utils/common.js';
  88. export default {
  89. props:{
  90. showDanmu:{
  91. type:Boolean,
  92. default:false
  93. },//是否显示弹幕
  94. videoInfo:{},//{source:2-视频社区\3-路演视频,id:视频社区id\路演视频id,...其他数据}
  95. curVideoId:0,//当前正在播放的id
  96. },
  97. watch: {
  98. curVideoId(){
  99. this.curSpeed='1.0'
  100. if(this.videoInfo.id==this.curVideoId){//显示弹幕
  101. this.closeDM=false
  102. }else{
  103. this.closeDM=true
  104. this.curVideoIns=null
  105. this.play=false
  106. this.danmuList=[]
  107. this.temdanmuList.forEach(item=>{
  108. item.done=false
  109. })
  110. }
  111. },
  112. showInput(n){
  113. //写弹幕时暂停视频
  114. if(n){
  115. this.curVideoIns.pause()
  116. }else{
  117. this.curVideoIns.play()
  118. }
  119. },
  120. closeDM(){
  121. if(this.closeDM){
  122. this.danmuList=[]//如果关闭了弹幕显示 则清空一次 弹幕列表 防止 切换回来又会重新播放一次已有的弹幕
  123. }
  124. }
  125. },
  126. computed:{
  127. selfUserid(){
  128. return this.$store.state.user.userInfo.user_id;
  129. }
  130. },
  131. data() {
  132. return {
  133. curVideoIns:null,
  134. play:false,//视频播放状态
  135. curVideoTime:0,
  136. showInput:false,//显示悬浮输入弹幕弹窗
  137. closeDM:true,//是否关闭弹幕
  138. danmuText:'',
  139. showSpeedOpt:false,
  140. speedOpts:['0.5','0.8','1.0','1.25','1.5','2.0'],
  141. curSpeed:'1.0',
  142. keyboardheight:0,//键盘高度
  143. isFullScreen:false,//是否为全屏
  144. isShowControls:false,//是否显示视频的控制栏
  145. safeAreaTop:0,
  146. temdanmuList:this.videoInfo.bullet_chat_list||[],
  147. danmuList:[],
  148. }
  149. },
  150. created(){
  151. uni.getSystemInfo({
  152. success:(res)=>{
  153. this.safeAreaTop=res.safeArea.top
  154. }
  155. })
  156. },
  157. methods: {
  158. //点击最外层盒子
  159. handleClickWrap(){
  160. this.showSpeedOpt=false
  161. },
  162. // 点击播放
  163. handelClickPlay(){
  164. this.$emit('videoPlay', this.videoInfo)
  165. setTimeout(() => {
  166. this.curVideoIns=uni.createVideoContext(this.curVideoId.toString(),this)//由于是在自定义组件内 所有this不可少
  167. }, 300);
  168. },
  169. handleVideoPlay(){
  170. this.play=true
  171. },
  172. handleVideoEnd(){
  173. // 此处因为如果不调用退出全屏方法 安卓和ios页面均会表现异常,安卓横屏不恢复竖屏,ios底部tabbar渲染异常
  174. this.curVideoIns.exitFullScreen()
  175. this.curVideoIns=null
  176. this.play=false
  177. this.danmuList=[]
  178. this.temdanmuList.forEach(item=>{
  179. item.done=false
  180. })
  181. this.$emit('ended')
  182. },
  183. handleVideoPause(){
  184. this.play=false
  185. this.$emit('pause')
  186. },
  187. handleTimeUpdate(e){
  188. this.addDanmu(e.detail.currentTime)
  189. this.curVideoTime=e.detail.currentTime
  190. this.$emit('timeupdate',e)
  191. },
  192. handleFullscreenchange(e){
  193. this.isFullScreen=e.detail.fullScreen
  194. },
  195. handleControlstoggle(e){
  196. this.isShowControls=e.detail.show
  197. },
  198. //键盘高度变为0 则收起悬浮弹幕输入框
  199. keyboardheightchange(e){
  200. this.keyboardheight=e.detail.height
  201. this.handlekeyboardheight()
  202. },
  203. handlekeyboardheight:debounce(function(){
  204. if(this.keyboardheight==0&&this.showInput){
  205. this.showInput=false
  206. }
  207. },60),
  208. //切换为背景音频播放
  209. handleChangeMusic(){
  210. console.log('切换背景音频播放');
  211. this.curVideoIns.requestBackgroundPlayback()
  212. },
  213. // 倍速切换
  214. handleVideoSpeedChange(item){
  215. const num=Number(item)
  216. this.curVideoIns.playbackRate(num)
  217. this.curSpeed=item
  218. this.showSpeedOpt=false
  219. },
  220. //点击视频区域外面的 显示弹幕按钮 进行判断如果当前视频没有播放 则不改变状态
  221. handleShowDM(){
  222. if(this.videoInfo.id==this.curVideoId){
  223. this.closeDM=false
  224. }
  225. },
  226. //发送弹幕
  227. handleSendDanmu(){
  228. if(!this.danmuText) return
  229. apiVideoDanmuSend({
  230. content:this.danmuText,
  231. seconds:parseInt(this.curVideoTime),
  232. primary_id:this.videoInfo.id,
  233. source:this.videoInfo.source
  234. }).then(res=>{
  235. this.danmuText=''
  236. if(res.code===200){
  237. this.temdanmuList.push({...res.data,seconds:Number(res.data.seconds)+3})
  238. }
  239. })
  240. },
  241. //添加弹幕到视频上去
  242. addDanmu(ctime){
  243. this.temdanmuList.forEach(item => {
  244. if(item.seconds>ctime-1&&item.seconds<ctime+1){// 前后误差一秒
  245. if(!item.done){
  246. item.done=true
  247. this.danmuList.push({
  248. ...item,
  249. top:this.getTopPosition(),
  250. speed:Math.floor(Math.random()*(16-8+1))+8//8~16 之间的随机数
  251. })
  252. }
  253. }else{
  254. // 如果播放过了 手贱又把进度条拖回去了 则重置done
  255. if(ctime-1<item.seconds){
  256. item.done=false
  257. }
  258. if(ctime+1>item.seconds){
  259. item.done=true
  260. }
  261. }
  262. });
  263. },
  264. //设置弹幕位置
  265. getTopPosition(){
  266. const length=this.danmuList.length
  267. let num=0
  268. if(length%3===1){
  269. num=10
  270. }else if(length%3===2){
  271. num=30
  272. }else{
  273. num=50
  274. }
  275. return num+'px'
  276. }
  277. },
  278. }
  279. </script>
  280. <style lang="scss" scoped>
  281. .video-wrap{
  282. width: 100%;
  283. height: 100%;
  284. position: relative;
  285. video,.poster{
  286. width: 100%;
  287. height: 100%;
  288. border-radius: 20rpx;
  289. position: relative;
  290. }
  291. .poster{
  292. position: relative;
  293. &::after{
  294. content:'';
  295. display: block;
  296. position: absolute;
  297. width: 120rpx;
  298. height: 120rpx;
  299. top: 50%;
  300. left: 50%;
  301. transform: translate(-50%,-50%);
  302. background-image: url('@/static/video-play-btn.png');
  303. background-size: cover;
  304. }
  305. }
  306. .danmu-btn-box{
  307. position: absolute;
  308. bottom: -70rpx;
  309. right: 6rpx;
  310. .big-box{
  311. width: 248rpx;
  312. height: 50rpx;
  313. background-image: url('@/static/danmu-show-btn.png');
  314. background-size: cover;
  315. display: flex;
  316. .left{
  317. width: 67%;
  318. height: 100%;
  319. flex-shrink: 0;
  320. }
  321. .right{
  322. flex: 1;
  323. height: 100%;
  324. }
  325. }
  326. .small-box{
  327. width: 80rpx;
  328. height: 50rpx;
  329. background-image: url('@/static/danmu-close-btn.png');
  330. background-size: cover;
  331. }
  332. }
  333. .danmu-input-box{
  334. position: fixed;
  335. left: 0;
  336. right: 0;
  337. bottom: 0;
  338. z-index: 999999999;
  339. background-color: #ffffff;
  340. padding: 10rpx 34rpx;
  341. border-top: 1px solid #E5E5E5;
  342. align-content: center;
  343. .input-box{
  344. flex:1;
  345. border-radius: 40rpx;
  346. padding: 6rpx 10rpx;
  347. background: #EFEFEF;
  348. border: 2rpx solid #E5E5E5;
  349. align-items: center;
  350. font-size: 12px;
  351. input{
  352. flex:1;
  353. }
  354. text{
  355. color: #999;
  356. }
  357. }
  358. .btn{
  359. width: 100rpx;
  360. flex-shrink: 0;
  361. color: #F3A52F;
  362. font-size: 12px;
  363. display: flex;
  364. align-items: center;
  365. justify-content: center;
  366. }
  367. }
  368. .video-inner-right-box{
  369. position: absolute;
  370. bottom: 30%;
  371. right: 5%;
  372. display: flex;
  373. flex-direction: column;
  374. align-items: center;
  375. }
  376. .change-music-icon{
  377. width: 40rpx;
  378. height: 40rpx;
  379. background-image: url('@/static/headphones-icon.png');
  380. background-size: cover;
  381. margin-bottom: 20rpx;
  382. }
  383. .video-speed-btn{
  384. width: 80rpx;
  385. height: 44rpx;
  386. display: flex;
  387. align-items: center;
  388. justify-content: center;
  389. background: rgba(0, 0, 0, 0.4);
  390. border-radius: 22rpx;
  391. color: #fff;
  392. font-size: 12px;
  393. }
  394. .speed-opt-box{
  395. position: absolute;
  396. right: 0;
  397. top: 0;
  398. bottom: 0;
  399. width: 20%;
  400. background: rgba(0, 0, 0, 0.8);
  401. display: flex;
  402. flex-direction: column;
  403. justify-content: space-around;
  404. padding-top: 55rpx;
  405. .item{
  406. color: #fff;
  407. font-size: 26rpx;
  408. flex: 1;
  409. width: 100%;
  410. text-align: center;
  411. }
  412. }
  413. .video-danmu-control-box{
  414. margin-bottom: 10rpx;
  415. view{
  416. margin-left: auto;
  417. margin-right: auto;
  418. }
  419. .show-btn{
  420. width: 80rpx;
  421. height: 50rpx;
  422. background-image: url('@/static/danmu-show-btn-2.png');
  423. background-size: cover;
  424. }
  425. .close-btn{
  426. width: 80rpx;
  427. height: 50rpx;
  428. background-image: url('@/static/danmu-close-btn-2.png');
  429. background-size: cover;
  430. }
  431. .send-btn{
  432. width: 100rpx;
  433. height: 44rpx;
  434. display: flex;
  435. align-items: center;
  436. justify-content: center;
  437. background: rgba(0, 0, 0, 0.4);
  438. border-radius: 22rpx;
  439. color: #fff;
  440. font-size: 12px;
  441. margin-top: 20rpx;
  442. }
  443. }
  444. .danmu-scroll-box{
  445. .danmu-item{
  446. color: #fff;
  447. animation: move 6s linear;
  448. position: absolute;
  449. top: 0;
  450. display: block;
  451. left: 150%;
  452. position: absolute;
  453. font-size: 12px;
  454. height: 18px;
  455. white-space: nowrap;
  456. }
  457. .animat-pause{
  458. animation-play-state: paused;
  459. }
  460. .animat-run{
  461. animation-play-state: running;
  462. }
  463. .border{
  464. border: 1px solid #fff;
  465. border-radius: 10px;
  466. padding-top: 8px;
  467. padding-left: 2px;
  468. padding-right: 2px;
  469. }
  470. @keyframes move {
  471. 0%{
  472. left: 150%;
  473. }
  474. 100%{
  475. left: -200%;
  476. }
  477. }
  478. }
  479. }
  480. </style>