uniapp 仿火山 / 抖音短視訊|uni-App+vue 直播例項

xiaoyan2015發表於2019-11-13

uniapp仿火山/抖音短視訊|uni-app+vue直播例項

U直播是一個基於uniapp+vue+nvue+vuex等技術實現的小視訊/直播介面/聊天等功能的跨端專案,分別仿製了抖音小視訊、陌陌直播介面功能,可實現上下滑動切換播放、暫停,評論、商品等功能。

  • 編輯器+技術:HBuilderX + vue/NVue/uniapp/vuex
  • iconfont圖示:阿里字型圖示庫
  • 自定義導航欄 + 底部Tabbar
  • 彈窗元件:uniPop(uni-app封裝自定義Modal彈窗)
  • 測試環境:H5端/小程式/App端/真機

uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項

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

由於原生導航欄功能限制,有些效果不好實現,於是就自定義頂部導航欄模式,可設定透明背景(如:個人主頁/朋友圈動態)效果, 具體可參看這篇文章:uni-app自定義導航欄按鈕|uniapp仿微信頂部導航條
uniapp仿火山/抖音短視訊|uni-app+vue直播例項
uniapp仿火山/抖音短視訊|uni-app+vue直播例項

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

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>

uniapp仿火山/抖音短視訊|uni-app+vue直播例項

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

由於原生video、map等原生元件層級較高,雖說cover-view元件可以覆蓋其上,但其不能巢狀子元件,且限制較大,故只能採用編寫.nvue(native vue)頁面了。nvue頁面語法結構和vue無太大差別,只是需要注意css編寫方式,且只能使用flex佈局模式,另外一些css屬性無效。引入iconfont也有差異。
nvue引入iconfont方式:

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

好了,以上就是uniapp開發小視訊/直播專案的介紹,希望對大家有些許幫助吧 ✍?
最後分享個之前開發的H5即時IM專案
HTML5趣聊|仿微信h5語音|搖一搖|地圖|紅包
uniapp仿火山/抖音短視訊|uni-app+vue直播例項

本文為原創文章,未經作者允許不得轉載,歡迎大家一起交流 QQ(282310962) wx(xy190310)

相關文章