经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » HTML5 » 查看文章
基于vue+uniapp直播项目|uni-app仿抖音/陌陌直播室
来源:cnblogs  作者:xiaoyan2017  时间:2019/11/12 8:43:41  对本文有异议

 一、项目简介

uni-liveShow是一个基于vue+uni-app技术开发的集小视频/IM聊天/直播等功能于一体的微直播项目。界面仿制抖音|火山小视频/陌陌直播,支持编译到多端(H5、小程序、App端) 且兼容效果一致。

二、效果预览

在H5、小程序、App端测试效果如下:(后续大图均为APP端)

三、使用技术

  • 编码器+技术:HBuilderX + vue/NVue/uniapp/vuex
  • iconfont图标:阿里字体图标库
  • 自定义导航栏 + 底部Tabbar
  • 弹窗组件:uniPop(uni-app封装自定义弹出窗)
  • 测试环境:H5端 + 小程序 + App端

◆ uniapp计算设备顶部状态栏高度

  1. /**
  2. * @desc uniapp主页面App.vue
  3. * @about Q:282310962 wx:xy190310
  4. */
  5.  
  6. <script>
  7. import Vue from 'vue'
  8. export default {
  9. onLaunch: function() {
  10. // console.log('App Launch')
  11. uni.getSystemInfo({
  12. success:function(e){
  13. Vue.prototype.statusBar = e.statusBarHeight
  14. // #ifndef MP
  15. if(e.platform == 'android') {
  16. Vue.prototype.customBar = e.statusBarHeight + 50
  17. }else {
  18. Vue.prototype.customBar = e.statusBarHeight + 45
  19. }
  20. // #endif
  21. // #ifdef MP-WEIXIN
  22. let custom = wx.getMenuButtonBoundingClientRect()
  23. Vue.prototype.customBar = custom.bottom + custom.top - e.statusBarHeight
  24. // #endif
  25. // #ifdef MP-ALIPAY
  26. Vue.prototype.customBar = e.statusBarHeight + e.titleBarHeight
  27. // #endif
  28. }
  29. })
  30. },
  31. }
  32. </script>

◆ 项目中顶部透明导航栏设置

顶部导航栏采用的是自定义模式,可设置透明背景(如:个人主页/朋友圈动态) 具体可参看这篇文章:uni-app自定义导航栏按钮|uniapp仿微信顶部导航条

 

  1. <header-bar :isBack="true" title=" " :bgColor="{background: 'transparent'}" transparent>
  2. <text slot="back" class="uni_btnIco iconfont icon-guanbi" style="font-size: 25px;"></text>
  3. <text slot="iconfont" class="uni_btnIco iconfont icon-dots mr_5" style="font-size: 25px;"></text>
  4. </header-bar>

◆ uniapp仿抖音小视频效果

项目中小视频界面功能效果类似抖音/火山小视频,使用swiper组件实现上下滑动切换视频播放。

  1. <swiper :indicator-dots="false" :duration="200" :vertical="true" :current="videoIndex" @change="handleSlider" style="height: 100%;">
  2. <block v-for="(item,index) in vlist" :key="index">
  3. <swiper-item>
  4. <view class="uni_vdplayer">
  5. <video :id="'myVideo' + index" :ref="'myVideo' + index" class="player-video" :src="item.src"
  6. :controls="false" :loop="true" :show-center-play-btn="false" objectFit="fill">
  7. </video>
  8. <!-- 中间播放按钮 -->
  9. <view class="vd-cover flexbox" @click="handleClicked(index)"><text v-if="!isPlay" class="iconfont icon-bofang"></text></view>
  10. <!-- 底部信息 -->
  11. <view class="vd-footToolbar flexbox flex_alignb">
  12. <view class="vd-info flex1">
  13. <view class="item at">
  14. <view class="kw" v-for="(kwItem,kwIndex) in item.keyword" :key="kwIndex"><text class="bold fs_18 mr_5"></text> {{kwItem}}</view>
  15. </view>
  16. <view class="item subtext">{{item.subtitle}}</view>
  17. <view class="item uinfo flexbox flex_alignc">
  18. <image class="avator" :src="item.avator" mode="aspectFill" /><text class="name">{{item.author}}</text> <text class="btn-attention bg_linear1" :class="item.attention ? 'on' : ''" @tap="handleAttention(index)">{{item.attention ? '已关注' : '关注'}}</text>
  19. </view>
  20. <view class="item reply" @tap="handleVideoComment"><text class="iconfont icon-pinglun mr_5"></text> 写评论...</view>
  21. </view>
  22. <view class="vd-sidebar">
  23. <view v-if="item.cart" class="ls cart flexbox bg_linear3" @tap="handleVideoCart(index)"><text class="iconfont icon-cart"></text></view>
  24. <view class="ls" @tap="handleIsLike(index)"><text class="iconfont icon-like" :class="item.islike ? 'like' : ''"></text><text class="num">{{ item.likeNum+(item.islike ? 1: 0) }}</text></view>
  25. <view class="ls" @tap="handleVideoComment"><text class="iconfont icon-liuyan"></text><text class="num">{{item.replyNum}}</text></view>
  26. <view class="ls"><text class="iconfont icon-share"></text><text class="num">{{item.shareNum}}</text></view>
  27. </view>
  28. </view>
  29. </view>
  30. </swiper-item>
  31. </block>
  32. </swiper>

视频滑动切换 播放、暂停 及单击/双击判断,商品及评论展示

  1. <script>
  2. // 引入商品广告、评论
  3. import videoCart from '@/components/cp-video/cart.vue'
  4. import videoComment from '@/components/cp-video/comment'
  5. let timer = null
  6. export default {
  7. data() {
  8. return {
  9. videoIndex: 0,
  10. vlist: videoJson,
  11. isPlay: true, //当前视频是否播放中
  12. clickNum: 0, //记录点击次数
  13. }
  14. },
  15. components: {
  16. videoCart, videoComment
  17. },
  18. onLoad(option) {
  19. this.videoIndex = parseInt(option.index)
  20. },
  21. onReady() {
  22. this.init()
  23. },
  24. methods: {
  25. init() {
  26. this.videoContextList = []
  27. for(var i = 0; i < this.vlist.length; i++) {
  28. // this.videoContextList.push(this.$refs['myVideo' + i][0])
  29. this.videoContextList.push(uni.createVideoContext('myVideo' + i, this));
  30. }
  31. setTimeout(() => {
  32. this.play(this.videoIndex)
  33. }, 200)
  34. },
  35. // 滑动切换
  36. handleSlider(e) {
  37. let curIndex = e.detail.current
  38. if(this.videoIndex >= 0){
  39. this.videoContextList[this.videoIndex].pause()
  40. this.videoContextList[this.videoIndex].seek(0)
  41. this.isPlay = false
  42. }
  43. if(curIndex === this.videoIndex + 1) {
  44. this.videoContextList[this.videoIndex + 1].play()
  45. this.isPlay = true
  46. }else if(curIndex === this.videoIndex - 1) {
  47. this.videoContextList[this.videoIndex - 1].play()
  48. this.isPlay = true
  49. }
  50. this.videoIndex = curIndex
  51. },
  52. // 播放
  53. play(index) {
  54. this.videoContextList[index].play()
  55. this.isPlay = true
  56. },
  57. // 暂停
  58. pause(index) {
  59. this.videoContextList[index].pause()
  60. this.isPlay = false
  61. },
  62. // 点击视频事件
  63. handleClicked(index) {
  64. if(timer){
  65. clearTimeout(timer)
  66. }
  67. this.clickNum++
  68. timer = setTimeout(() => {
  69. if(this.clickNum >= 2){
  70. console.log('双击视频')
  71. }else{
  72. console.log('单击视频')
  73. if(this.isPlay){
  74. this.pause(index)
  75. }else{
  76. this.play(index)
  77. }
  78. }
  79. this.clickNum = 0
  80. }, 300)
  81. },
  82. // 喜欢
  83. handleIsLike(index){
  84. let vlist = this.vlist
  85. vlist[index].islike =! vlist[index].islike
  86. this.vlist = vlist
  87. },
  88. // 显示评论
  89. handleVideoComment() {
  90. this.$refs.videoComment.show()
  91. },
  92. // 显示购物车
  93. handleVideoCart(index) {
  94. this.$refs.videoCart.show(index)
  95. },
  96. }
  97. }
  98. </script>

在项目开发过程中,遇到了视频video层级高不能覆盖的问题,使用nvue页面就可以解决view覆盖在video之上。.nvue (native vue的缩写)

更多关于nvue页面开发,可以参看:uniapp开发nvue页面

◆ uniapp聊天页面实现

项目中的聊天页面,功能效果这里就不详细介绍了,可参看这篇:uni-app聊天室|vue+uniapp仿微信聊天实例

◆ 直播页面live.nvue

为避免video不能覆盖问题,直播页面采用的是nvue编写,开发过程也遇到了一些坑,尤其是css,全部是flex布局,而且不能多级嵌套,有些css属性不支持。

  1. <template>
  2. <div class="nlv__container">
  3. <view class="nlv_main">
  4. <swiper class="nlv-swiper" :indicator-dots="false" :vertical="false" :current="videoIndex" @change="handleSlider">
  5. <swiper-item v-for="(item, index) in vlist" :key="index">
  6. <!-- //直播区 -->
  7. <view class="nlv-playerbox">
  8. <video :id="'myVideo' + index" :ref="'myVideo' + index" class="player-video" :src="item.src" :autoplay="index == videoIndex"
  9. :controls="false" :loop="true" :show-center-play-btn="false" objectFit="fill" :style="{height: winHeight, width: winWidth}">
  10. </video>
  11. <!-- //顶部 -->
  12. <view class="nlv_topbar" :style="{ height: headerBarH, 'padding-top': statusBarH }">
  13. ...
  14. </view>
  15. <!-- //排名信息 -->
  16. <view class="nlv-rankbox" :style="{top: headerBarH}">
  17. <view class="nlv-rkls">
  18. <text class="rkitem">总排名{{item.allRank}}</text>
  19. <text v-if="item.hourRank" class="rkitem">小时榜第{{item.hourRank}}名</text>
  20. </view>
  21. <text class="nlv-uid">U直播:{{item.uid}}</text>
  22. </view>
  23. <!-- //底部信息栏 -->
  24. <view class="nlv-footToolbar">
  25. <!-- 送礼物提示 -->
  26. <view class="nlv-giftTipPanel">
  27. ...
  28. </view>
  29. <!-- 滚动msg信息 -->
  30. <scroll-view class="nlv-rollMsgPanel" scroll-y show-scrollbar="false">
  31. <block v-for="(msgitem, msgidx) in item.rollmsg" :key="msgidx">
  32. <view class="nlv-msglist"><view class="msg_bg"><text class="msg_name">{{msgitem.uname}}</text> <text class="msg_text">{{msgitem.content}}</text></view></view>
  33. </block>
  34. </scroll-view>
  35. <view class="nlv-infobox">
  36. <view class="nlv_reply" @tap="handleRollMsg(index)"><text class="nlv_reply_text">说点什么...</text></view>
  37. <view class="nlv_btntool">
  38. ...
  39. <view v-if="item.cart" class="btn-toolitem" @tap="handleLiveCart(index)"><text class="iconfont i-btntool" style="color: #ff4e0e;font-size: 20px;">&#xe61e;</text></view>
  40. <view class="btn-toolitem btn-toolitem-cart" @tap="handleLiveGift"><text class="iconfont i-btntool">&#xe600;</text></view>
  41. ...
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </swiper-item>
  47. </swiper>
  48. </view>
  49. <!-- 商品广告、滚动消息、礼物 -->
  50. <live-cart ref="liveCart" :vlist="vlist" />
  51. <roll-msg ref="rollMsg" :vlist="vlist" />
  52. <live-gift ref="liveGift" />
  53. </div>
  54. </template>

另外引入阿里字体图标也需注意:通过weex方式引入

  1. beforeCreate() {
  2. // 引入iconfont字体
  3. // #ifdef APP-PLUS
  4. const domModule = weex.requireModule('dom')
  5. domModule.addRule('fontFace', {
  6. fontFamily: "nvueIcon",
  7. 'src': "url('../../../static/fonts/iconfont.ttf')"
  8. });
  9. // #endif
  10. },

至于视频滑动切换和上面小视频操作差不多,就不贴码了。 到这里,uni-liveShow项目基本介绍完了,希望对大家有些许帮助。??

最后,附上两个vue/react项目案例:

vue+vuex+vue-router仿微信网页版聊天室  https://www.cnblogs.com/xiaoyan2017/p/10793728.html

angular+ng-router手机端聊天IM实战开发   https://www.cnblogs.com/xiaoyan2017/p/11194828.html

 

原文链接:http://www.cnblogs.com/xiaoyan2017/p/11835641.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号