基於vue+uniapp直播項目|uni-app仿抖音/陌陌直播室

来源:https://www.cnblogs.com/xiaoyan2017/archive/2019/11/11/11835641.html
-Advertisement-
Play Games

一、項目簡介 uni-liveShow是一個基於vue+uni-app技術開發的集小視頻/IM聊天/直播等功能於一體的微直播項目。界面仿製抖音|火山小視頻/陌陌直播,支持編譯到多端(H5、小程式、App端) 且相容效果一致。 二、效果預覽 在H5、小程式、App端測試效果如下:(後續大圖均為APP端 ...


 一、項目簡介

uni-liveShow是一個基於vue+uni-app技術開發的集小視頻/IM聊天/直播等功能於一體的微直播項目。界面仿製抖音|火山小視頻/陌陌直播,支持編譯到多端(H5、小程式、App端) 且相容效果一致。

二、效果預覽

在H5、小程式、App端測試效果如下:(後續大圖均為APP端)

三、使用技術

  • 編碼器+技術:HBuilderX + vue/NVue/uniapp/vuex
  • iconfont圖標:阿裡字體圖標庫
  • 自定義導航欄 + 底部Tabbar
  • 彈窗組件:uniPop(uni-app封裝自定義彈出窗)
  • 測試環境:H5端 + 小程式 + App端

◆ uniapp計算設備頂部狀態欄高度

 /**
  * @desc   uniapp主頁面App.vue
  * @about  Q:282310962  wx:xy190310
  */

<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仿微信頂部導航條

 

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

視頻滑動切換 播放、暫停 及單擊/雙擊判斷,商品及評論展示

<script>
    // 引入商品廣告、評論
    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頁面開發,可以參看:uniapp開發nvue頁面

◆ uniapp聊天頁面實現

項目中的聊天頁面,功能效果這裡就不詳細介紹了,可參看這篇:uni-app聊天室|vue+uniapp仿微信聊天實例

◆ 直播頁面live.nvue

為避免video不能覆蓋問題,直播頁面採用的是nvue編寫,開發過程也遇到了一些坑,尤其是css,全部是flex佈局,而且不能多級嵌套,有些css屬性不支持。

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

另外引入阿裡字體圖標也需註意:通過weex方式引入

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

至於視頻滑動切換和上面小視頻操作差不多,就不貼碼了。 到這裡,uni-liveShow項目基本介紹完了,希望對大家有些許幫助。

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 本文整理了前端工程師斬獲BATTMD大廠的一些秘訣,從前端知識體系、電腦網路、操作系統、數據結構與演算法、編程題與分析題等八大部分 整理了前端工程師在秋招面試過程中必備的知識點,相信大家只要掌握了這其中的重要知識點,並有相關的項目經驗作為支撐,必定會收到理想的Offer,最後把筆者秋招過程中整理的這 ...
  • 下麵介紹Element-ui中Upload組件如何把圖片上傳的七牛雲(免費版本,有流量限制) 一、準備工作 1.去七牛雲註冊賬號,並實名認證,不認證無法創建存儲空間 2.瞭解Element-ui組件 二、創建上傳頁面(這裡直接使用官方的代碼片段) 三、上傳憑證(Token)如何獲得:https:// ...
  • 網頁加入視頻可以用h5自帶的video標簽,這裡用一個jQuery封裝優化好的video視頻組件videojs。 videojs官方網站:https://docs.videojs.com/index.html videojs下載:https://github.com/videojs/video.js ...
  • 問題:文本框輸入完成後點擊回車頁面刷新問題出在form上,當表單中只有一個文本框的時候獲取焦點並點擊回車之後會提交表單內容,就會發生刷新事件。 解決方法: 1、增加一個隱藏的輸入框 增加了一個隱藏的輸入框之後,表單的文本框不再是唯一的,回車不會觸發提交事件 2、去掉輸入框的回車事件 但是有的需求又要 ...
  • 國瑞前端: js確定取消,在html界面中,有css模擬的模態框,這樣顯示的就會更好看一些,那麼javascript有沒有自帶的彈框呢,當然是有的,接下來我就來給大家介紹一下把: js確定取消-警告框alert() 起到警告作用 js確定取消-確認框confirm() 123456789101112 ...
  • 響應式佈局 一個網站能夠相容多個終端,並且在各個終端都可以很好展示體驗。 媒體類型 在何種設備或者軟體上將頁面打開 123456789 all:所有媒體braille:盲文觸覺設備embossed:盲文印表機print:手持設備projection:印表機預覽screen:彩屏設備speech:'聽 ...
  • 小程式由來 當初結婚,為了給媳婦驚喜,藉助雲開發的推廣,自己做了一個結婚用的邀請函小程式,結完婚後,趁著空閑時間,希望把之前使用的邀請函小程式留存下來,所以做了一次整體改版。改版後的小程式我自己定義為相冊類小程式,主要放一些自己喜歡的照片。 小程式功能 首頁相冊 愛的空間相冊 陌生人的到訪 陌生人的 ...
  • 中間件是什麼呢? 中間件就是客戶端http請求發起傳送到伺服器和伺服器返迴響應之間的一些處理函數。 為什麼要使用中間件? 通過中間件,可以對數據進行操作使得我們能方便地操作請求數據編寫伺服器響應。如body-parse中間件對post請求的參數進行處理讓我們可以通過res.body快速獲取請求參數, ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...