videoBox.vue 15 KB

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