uniapp+nvue仿抖音/陌陌直播|uni-app小视频

10,430 阅读5分钟
前段时间有使用uniapp开发了个仿微信界面即时聊天室|朋友圈项目,这次分享的是基于uniapp+vue+nvue+vuex等技术开发的仿制抖音|火山小视频/陌陌直播等功能的微直播uni-liveShow实战项目。功能可以类似抖音上下滑动切换播放。

uni-app仿微信聊天室:www.cnblogs.com/xiaoyan2017…

三端效果预览:


技术实现:

  • 编码器+技术:HBuilderX + vue/NVue/uniapp/vuex
  • iconfont图标:阿里字体图标库
  • 自定义顶部导航 + 底部Tabbar
  • 弹窗组件:uniPop(使用UniApp封装自定义对话框组件)
  • 测试环境:H5端 + 小程序 + App端 + 真机



















  • uniapp顶部导航栏设置

由于原生顶部导航栏功能限制,不能实现项目的一些需求,如是顶部导航栏采用的是自定义模式,可设置透明背景(如:个人主页/朋友圈动态)效果, 具体可参看这篇文章:uni-app自定义导航栏按钮|uniapp仿微信顶部导航条



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

  • 主入口main.js引入样式及组件

 /**
  * @desc 	uni-app主入口页面
  * @about 	Q:282310962  wx:xy190310
  */

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

// ***引入css
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)

// ***引入自定义弹窗组件uniPop
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()

  • 仿抖音短视频效果

uniapp实现类似抖音/火山小视频效果,使用swiper组件实现上下滑动切换视频,点击可播放、暂停,点赞、评论等功能。


<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>

<script>
    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>

  • Nvue页面解决video层级高

由于原生video、map等组件层级较高,虽说提供了cover-view组件可以覆盖其上,但其不能嵌套子组件,且限制较大,故只能采用编写.nvue(native vue)页面了。

nvue页面编写和vue无太大差别,只是需要注意css编写方式,且只能使用flex布局模式,另外一些css属性无效。引入iconfont也有差异。

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

<template>
    <div class="nlv__container">
        <view class="nlv_main">
            <swiper class="nlv-swiper" :indicator-dots="false" :vertical="false" :current="videoIndex" @change="handleSlider">
                <swiper-item v-for="(item, index) in vlist" :key="index">
                    <!-- //直播区 -->
                    <view class="nlv-playerbox">
                        <video :id="'myVideo' + index" :ref="'myVideo' + index" class="player-video" :src="item.src" :autoplay="index == videoIndex"
                        :controls="false" :loop="true" :show-center-play-btn="false" objectFit="fill" :style="{height: winHeight, width: winWidth}">
                        </video>
                        
                        <!-- //顶部 -->
                        <view class="nlv_topbar" :style="{ height: headerBarH, 'padding-top': statusBarH }">
                            ...
                        </view>
                        
                        <!-- //排名信息 -->
                        <view class="nlv-rankbox" :style="{top: headerBarH}">
                            <view class="nlv-rkls">
                                <text class="rkitem">总排名{{item.allRank}}</text>
                                <text v-if="item.hourRank" class="rkitem">小时榜第{{item.hourRank}}名</text>
                            </view>
                            <text class="nlv-uid">U直播:{{item.uid}}</text>
                        </view>
                        
                        <!-- //底部信息栏 -->
                        <view class="nlv-footToolbar">
                            <!-- 送礼物提示 -->
                            <view class="nlv-giftTipPanel">
                                ...
                            </view>
                            
                            <!-- 滚动msg信息 -->
                            <scroll-view class="nlv-rollMsgPanel" scroll-y show-scrollbar="false">
                                <block v-for="(msgitem, msgidx) in item.rollmsg" :key="msgidx">
                                    <view class="nlv-msglist"><view class="msg_bg"><text class="msg_name">{{msgitem.uname}}</text> <text class="msg_text">{{msgitem.content}}</text></view></view>
                                </block>
                            </scroll-view>
                            
                            <view class="nlv-infobox">
                                <view class="nlv_reply" @tap="handleRollMsg(index)"><text class="nlv_reply_text">说点什么...</text></view>
                                <view class="nlv_btntool">
                                    ...
                                    <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>
                                    <view class="btn-toolitem btn-toolitem-cart" @tap="handleLiveGift"><text class="iconfont i-btntool">&#xe600;</text></view>
                                    ...
                                </view>
                            </view>
                        </view>
                    </view>
                </swiper-item>
            </swiper>
        </view>
        
        <!-- 商品广告、滚动消息、礼物 -->
        <live-cart ref="liveCart" :vlist="vlist" />
        <roll-msg ref="rollMsg" :vlist="vlist" />
        <live-gift ref="liveGift" />
    </div>
</template>

至于直播切换和上面小视频切换思路基本一致,可参看其上即可。

好了,今天关于uniapp开发直播聊天室项目介绍到此了,后续会继续为大家分享一些实战项目。希望大家能喜欢~~~

◆ ReactNative仿微信界面聊天室:www.cnblogs.com/xiaoyan2017…

◆ H5仿陌陌/抖音小视频:www.cnblogs.com/xiaoyan2017…