uniapp直播室仿抖音视频nvueuniapp高仿陌陌直播

43次阅读

共计 6885 个字符,预计需要花费 18 分钟才能阅读完成。

一、介绍说明

U 直播 uniLiveShow 是一款基于 vue+Nvue+uni-app 技术开发的综合小视频 / 聊天室 / 直播等功能的聊天直播项目。界面高仿热门抖音 | 火山小视频 / 陌陌直播,可滑动切换视频播放,且支持编译到多端且兼容效果一致。

二、项目预览

在 H5、小程序、App 端及真机测试效果一致 如下:

三、编码实现:

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

顶部状态条高度获取

<script>
    import Vue from 'vue'
    export default {onLaunch: function() {// console.log('App Launch')
            uni.getSystemInfo({success:function(e){
                    Vue.prototype.statusBar = e.statusBarHeight
                    // #ifndef MP
                    if(e.platform == 'android') {Vue.prototype.customBar = e.statusBarHeight + 50}else {Vue.prototype.customBar = e.statusBarHeight + 45}
                    // #endif
                    
                    // #ifdef MP-WEIXIN
                    let custom = wx.getMenuButtonBoundingClientRect()
                    Vue.prototype.customBar = custom.bottom + custom.top - e.statusBarHeight
                    // #endif
                    
                    // #ifdef MP-ALIPAY
                    Vue.prototype.customBar = e.statusBarHeight + e.titleBarHeight
                    // #endif
                }
            })
        },
    }
</script>

引入公共组件及样式

import Vue from 'vue'
import App from './App'

import './static/fonts/iconfont.css'
import './assets/css/reset.css'
import './assets/css/layout.css'

// 状态管理
import store from './store'
Vue.prototype.$store = store

// 公共组件
import headerBar from './components/header/header.vue'
import tabBar from './components/tabbar/tabbar.vue'
import popupWindow from './components/popupWindow.vue'
Vue.component('header-bar', headerBar)
Vue.component('tab-bar', tabBar)
Vue.component('popup-window', popupWindow)

// 自定义弹窗组件
import uniPop from './components/uniPop/uniPop.vue'
Vue.component('uni-pop', uniPop)

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({...App})
app.$mount()

至于聊天页面功能效果这里就不讲述了。
可参看这篇:uni-app 聊天室 App|vue+uniapp 仿微信聊天界面实例

uniapp 仿抖音效果

uni-app+nvue 技术实现仿抖音界面滑动效果,且有点赞、评论及商品等功能,可以单击、双击判断。

<swiper :indicator-dots="false" :duration="200" :vertical="true" :current="videoIndex" @change="handleSlider" style="height: 100%;">
    <block v-for="(item,index) in vlist" :key="index">
        <swiper-item>
            <view class="uni_vdplayer">
                <video :id="'myVideo' + index":ref="'myVideo'+ index" class="player-video" :src="item.src" 
                :controls="false" :loop="true" :show-center-play-btn="false" objectFit="fill">
                </video>
                <!-- 中间播放按钮 -->
                <view class="vd-cover flexbox" @click="handleClicked(index)"><text v-if="!isPlay" class="iconfont icon-bofang"></text></view>
                <!-- 底部信息 -->
                <view class="vd-footToolbar flexbox flex_alignb">
                    <view class="vd-info flex1">
                        <view class="item at">
                            <view class="kw" v-for="(kwItem,kwIndex) in item.keyword" :key="kwIndex"><text class="bold fs_18 mr_5">#</text> {{kwItem}}</view>
                        </view>
                        <view class="item subtext">{{item.subtitle}}</view>
                        <view class="item uinfo flexbox flex_alignc">
                            <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>
                        </view>
                        <view class="item reply" @tap="handleVideoComment"><text class="iconfont icon-pinglun mr_5"></text> 写评论...</view>
                    </view>
                    <view class="vd-sidebar">
                        <view v-if="item.cart" class="ls cart flexbox bg_linear3" @tap="handleVideoCart(index)"><text class="iconfont icon-cart"></text></view>
                        <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>
                        <view class="ls" @tap="handleVideoComment"><text class="iconfont icon-liuyan"></text><text class="num">{{item.replyNum}}</text></view>
                        <view class="ls"><text class="iconfont icon-share"></text><text class="num">{{item.shareNum}}</text></view>
                    </view>
                </view>
            </view>
        </swiper-item>
    </block>
</swiper>

mock-video.js 视频模拟数据

/**
 * @desc 小视频 JSON 数据
 */

module.exports = [
    {
        id: 1,
        avator: '/static/uimg/u__chat_img1.jpg',
        poster: '/static/placeholder/video-img4.jpg',
        src: '/static/placeholder/video.mp4',
        author: '猪猪佩奇',
        subtitle: '稻城亚丁 - 人间绝美景色',
        keyword: ['美好回忆', '旅游圣地'],
        playNum: 3172,
        likeNum: 2518,
        replyNum: 292,
        shareNum: 107,
        islike: false,
        attention: false,
        cart: [
            {
                name: '同款冬枣',
                image: '/static/placeholder/cart-img1.jpg',
                price: 9.90
            },
            {
                name: '10 斤装爆甜冰糖心红富士',
                image: '/static/placeholder/cart-img2.jpg',
                price: 9.90
            },
            {
                name: '红心猕猴桃 单果 40-70 克',
                image: '/static/placeholder/cart-img3.jpg',
                price: 10.0
            }
        ]
    },
    {
        id: 2,
        avator: '/static/uimg/u__chat_img12.jpg',
        poster: '/static/placeholder/video-img0.jpg',
        src: 'https://txmov2.a.yximgs.com/bs2/newWatermark/MTg3NDYzOTY3MjM_zh_3.mp4',
        author: 'Alisa',
        subtitle: '不要在乎别人的流言蜚语',
        keyword: ['经典老歌'],
        playNum: 9432,
        likeNum: 5627,
        replyNum: 1285,
        shareNum: 638,
        islike: true,
        attention: true,
        cart: ''
    },
    {
        id: 3,
        avator: '/static/uimg/u__chat_img5.jpg',
        poster: '/static/placeholder/video-img2.jpg',
        src: 'https://txmov2.a.yximgs.com/bs2/newWatermark/MTY3NTU3MzYzMTQ_zh_4.mp4',
        author: '往后余生都是你',
        subtitle: '能不能给我一首歌的时间,让你拾起从前的快乐',
        keyword: '',
        playNum: 7268,
        likeNum: 3438,
        replyNum: 1105,
        shareNum: 327,
        islike: false,
        attention: false,
        cart: [
            {
                name: 'YCID 施蒂蓝玫瑰凝养柔滑唇膏',
                image: 'https://cbu01.alicdn.com/img/ibank/2019/218/182/12384281812_1493014487.jpg',
                price: 7.70
            },
            {
                name: '玛可安迪新款抖音网红推荐口红',
                image: 'https://cbu01.alicdn.com/img/ibank/2019/285/249/10457942582_1068990292.jpg',
                price: 19.9
            },
        ]
    },
    
    ...
]
<script>
    const videoJson = require('./mock-video.js')
    
    // 引入商品广告、评论
    import videoCart from '@/components/cp-video/cart.vue'
    import videoComment from '@/components/cp-video/comment'
    
    let timer = null
    export default {data() {
            return {
                videoIndex: 0,
                vlist: videoJson,
                
                isPlay: true,    // 当前视频是否播放中
                clickNum: 0,    // 记录点击次数
            }
        },
        components: {videoCart, videoComment},
        onLoad(option) {this.videoIndex = parseInt(option.index)
        },
        onReady() {this.init()
        },
        methods: {init() {this.videoContextList = []
                for(var i = 0; i < this.vlist.length; i++) {// this.videoContextList.push(this.$refs['myVideo' + i][0])
                    this.videoContextList.push(uni.createVideoContext('myVideo' + i, this));
                }
                
                setTimeout(() => {this.play(this.videoIndex)
                }, 200)
            },
            
            // 滑动切换
            handleSlider(e) {
                let curIndex = e.detail.current
                if(this.videoIndex >= 0){this.videoContextList[this.videoIndex].pause()
                    this.videoContextList[this.videoIndex].seek(0)
                    this.isPlay = false
                }
                if(curIndex === this.videoIndex + 1) {this.videoContextList[this.videoIndex + 1].play()
                    this.isPlay = true
                }else if(curIndex === this.videoIndex - 1) {this.videoContextList[this.videoIndex - 1].play()
                    this.isPlay = true
                }
                this.videoIndex = curIndex
            },
            // 播放
            play(index) {this.videoContextList[index].play()
                this.isPlay = true
            },
            // 暂停
            pause(index) {this.videoContextList[index].pause()
                this.isPlay = false
            },
            // 点击视频事件
            handleClicked(index) {if(timer){clearTimeout(timer)
                }
                this.clickNum++
                timer = setTimeout(() => {if(this.clickNum >= 2){console.log('双击视频')
                    }else{console.log('单击视频')
                        if(this.isPlay){this.pause(index)
                        }else{this.play(index)
                        }
                    }
                    this.clickNum = 0
                }, 300)
            },
            
            
            // 喜欢
            handleIsLike(index){
                let vlist = this.vlist
                vlist[index].islike =! vlist[index].islike
                this.vlist = vlist
            },
            // 显示评论
            handleVideoComment() {this.$refs.videoComment.show()
            },
            
            // 显示购物车
            handleVideoCart(index) {this.$refs.videoCart.show(index)
            },
        }
    }
</script>

在开发小视频页面过程中,遇到了视频 video 层级过高不能被其他组件覆盖,如是使用 nvue 页面就可以解决 view 覆盖在 video 之上。.nvue (native vue 的缩写)
且在 nvue 页面中使用 iconfont 图标库需注意引入方式

beforeCreate() {
    // 引入 iconfont 字体
    // #ifdef APP-PLUS
    const domModule = weex.requireModule('dom')
    domModule.addRule('fontFace', {
        fontFamily: "nvueIcon",
        'src': "url('../../../static/fonts/iconfont.ttf')"
    });
    // #endif
},

至此 uni-app 开发直播聊天项目介绍到这里,后续继续为大家分享实战项目,希望能有一些许帮助!!

最后分享个基于 H5+css3 开发的视频直播项目
https://www.cnblogs.com/xiaoyan2017/p/9904049.html

正文完
 0