uni-ttLive 基于uni-app+uView-ui跨端開發短視頻+直播聊天實體,
全新研發的一款多端仿制抖音短視頻+直播+聊天專案,基于
uniApp+Vue.js+Vuex+Nvue+uViewUI+uaPopup等技術開發而成,支持播放/暫停/上下滑動切換、全屏沉浸式體驗,

功能特點
- 導航條+底部選單透明式全屏模式
- 流暢的滑動切換體驗
- mini短視頻時間進度條
- 支持小視頻播放/暫停,上下滑動切換
- 全屏沉浸式支持Nvue頁面短視頻



一款集成了小視頻、直播、聊天及朋友圈于一體的跨設備uniapp應用,
專案結構/編譯


















入口main.js配置
import Vue from 'vue'
import App from './App'
import uView from 'uview-ui'
Vue.use(uView)
import API from '@/common/request'
Vue.prototype.$api = API
// 引入狀態管理
import Store from './store'
Vue.prototype.$store = Store
Vue.config.productionTip = false
App.mpType = 'app'
// #ifdef APP-PLUS
plus.navigator.closeSplashscreen()
// #endif
const app = new Vue({
...App
})
app.$mount()
由于視頻及直播頁面使用了Nvue撰寫,Nvue頁面不支持prototype方式掛載的函式,
所有使用了globalData來實作全域獲取導航條/狀態欄高度,
<script>
export default {
globalData: {
// 全域設定狀態欄和導航欄高度
statusBarH: 0,
customBarH: 0,
},
onLaunch: function() {
uni.getSystemInfo({
success: (e) => {
// 獲取手機狀態欄高度
let statusBar = e.statusBarHeight
let customBar
// #ifndef MP
customBar = statusBar + (e.platform == 'android' ? 50 : 45)
// #endif
// #ifdef MP-WEIXIN
// 獲取膠囊按鈕的布局位置資訊
let menu = wx.getMenuButtonBoundingClientRect()
// 導航欄高度 = 膠囊下距離 + 膠囊上距離 - 狀態欄高度
customBar = menu.bottom + menu.top - statusBar
// #endif
// #ifdef MP-ALIPAY
customBar = statusBar + e.titleBarHeight
// #endif
// 兼容nvue寫法(H5/小程式/APP/APP-Nvue)
this.globalData.statusBarH = statusBar
this.globalData.customBarH = customBar
}
})
},
onShow: function() {
console.log('App Show')
},
onHide: function() {
console.log('App Hide')
}
}
</script>
自定義導航條+底部tabbar
專案中的頂部導航欄和底部選單欄采用自定義組件實作功能,支持Nvue頁面懸浮video上面、全屏沉浸式透明模式,

<template>
<view class="ua__navbar">
<view class="ua__navbar-wrap" :class="{'custom': custom, 'fixed': fixed || transparent}"
:style="{'height': customBarH + 'px', 'padding-top': (custom ? statusBarH : 0) + 'px', 'background': bgcolor, 'color': color, 'z-index': zIndex}">
<!-- //左側 (回傳) -->
<view class="action navbar-action__left" v-if="back && back!='false'" @click="onBack">
<template v-if="$slots.back">
<slot name="back" />
</template>
<template v-else><text class="iconfont nvuefont"
:style="{'color': color}">{{'\ue84c'}}</text></template>
<slot name="backText" />
</view>
<slot name="left" />
<!-- //標題 -->
<view v-if="!search" class="navbar-title" :class="{'center': center}">
<template v-if="$slots.title">
<slot name="title" />
</template>
<template v-else><text :style="{'color': color}">{{title}}</text></template>
</view>
<!-- //搜索框 -->
<view v-if="search" class="action navbar-action__search">
<slot name="search" />
</view>
<view class="action navbar-action__right">
<slot name="right" />
</view>
</view>
</view>
</template>
目前兩個插件已經上傳至uniapp插件市場,感興趣的可以去看下,
https://ext.dcloud.net.cn/plugin?id=5593
https://ext.dcloud.net.cn/plugin?id=5592
uniapp升級版彈窗uaPopup組件
該插件是針對這個專案特意開發的一款多端彈窗插件,支持h5+小程式+App+Nvue端,

專案中所有的彈窗均是使用這個uaPopup組件實作功能,親測效果非常OK,
使用非常簡單,hbuilderx2.5起支持easycom自動引入,支持函式式+組件式兩種方式呼叫,
<ua-popup v-model="isVisibleConfirm" shadeClose="false" title="標題" xclose z-index="1001"
content="<div style='color:#ff557f;padding:20px 40px;'>預測未來的最好辦法是自己親手創造未來!</div>"
:btns="[
{text: '取消', click: handleCancel},
{text: '確定', style: 'color:#00aa00;', click: handleOk},
]"
/>
函式式支持多級嵌套呼叫,
<script>
export default {
methods: {
handleOk() {
let $ua = this.$refs.uapopup
$ua.open({
content: '人生漫漫,且行且珍惜',
customStyle: {'background-color': 'rgba(170, 0, 127, 0.6)', 'color': '#fff'},
time: 3,
onClose() {
$ua.open({
type: 'android',
content: '<div style="color:#aa007f">不要等待機會,而要創造機會</div>',
customStyle: {'width': '210px'},
btns: [
{
text: '關閉',
click() {
$ua.close()
}
},
{
text: '確定',
style: 'color:#00aa00;',
click() {
// ...
}
}
]
})
}
})
}
}
}
</script>
擁有20+自定義引數,多種彈窗型別及影片模式,
https://blog.csdn.net/yanxinyun1990/article/details/118633711
uniapp短視頻+直播
專案中的小視頻及直播頁面分為頂部導航區+視頻區+底部選單區三個大模塊,


<view v-if="currentTab == 2" class="ua__tabcnt-recommend">
<swiper class="ua__vdplayer-swiper flex1" :current="currentVideo" vertical @change="handleSwipeVertical">
<swiper-item v-for="(item, index) in videoList" :key="index">
<!-- 視頻模塊 -->
<view class="ua__vdplayer-video flex1">
<video class="vdplayer" :id="'vdplayer' + index" :ref="'vdplayer' + index"
:src="item.src"
:controls="false" :loop="true" :show-center-play-btn="false" object-fit="fill"
:autoplay="index == currentVideo"
@play="isPlaying=true" @timeupdate="handleTimeUpdate"
:style="{'width': winWidth, 'height': winHeight}"
>
</video>
<view class="ua__vdplayer-playwrap" @click="handleVideoClicked"><view v-if="!isPlaying" class="ua__vdplayer-playbtn"><text class="iconfont">{{`\ue607`}}</text></view></view>
</view>
<!-- 資訊模塊 -->
<view class="ua__vdplayer-info flexbox flex-col">
<view class="flexbox flex-row flex-alignb">
<!-- //左側資訊 -->
<view class="vdinfo__left flex1">
<view class="ltitem uavatar flexbox flex-row">
<navigator url="#" class="flexbox flex-alignc flex-row"><image class="uimg" :src="item.avatar" /><text class="uname">{{item.author}}</text></navigator>
<view class="flexbox btn" :class="{'actived': item.isFollow}" @click="handleFollow(index)"><text class="btn-text">{{item.isFollow ? '已關注' : '關注'}}</text></view>
</view>
<view v-if="item.topic" class="ltitem flexbox flex-row">
<view class="kw" v-for="(kw, index2) in item.topic" :key="index2"><text class="lbl">#{{kw}}</text></view>
</view>
<view class="ltitem"><text class="desc">{{item.desc}}</text></view>
</view>
<!-- //右側按鈕 -->
<view class="vdinfo__right flexbox flex-col">
<view class="rtitem ball" v-if="item.goods&&item.goods.length > 0" @click="handleShowGoodsPopup(item.goods)"><text class="icon iconfont">{{`\ue734`}}</text></view>
<view class="rtitem" :class="{'isliked': item.isLike}" @click="handleLiked(index)"><text class="icon iconfont">{{`\ue635`}}</text><text class="num">{{item.likeNum+(item.isLike ? 1 : 0)}}</text></view>
<view class="rtitem" @click="showReplyPopup = true"><text class="icon iconfont">{{`\ue632`}}</text><text class="num">{{item.replyNum}}</text></view>
<view class="rtitem" @click="showSharePopup = true"><text class="icon iconfont">{{`\ue63b`}}</text><text class="num">{{item.shareNum}}</text></view>
</view>
</view>
</view>
</swiper-item>
</swiper>
<!-- 底部播放進度條 -->
<view class="ua__vdplayer-progress"><view class="bar" :style="{'width': progressBar+'px'}"></view></view>
</view>
小視頻底部還新增了一個時間播放進度條,顯示當前視頻播放進度,

// 播放進度變化時觸發
handleTimeUpdate(e) {
let { currentTime, duration } = e.detail
this.progressBar = parseInt((currentTime / duration).toFixed(2) * parseInt(this.winWidth))
},
小視頻支持播放/暫停,上下滑動切換效果,
<script>
const app = getApp()
import videoJSON from '@/mock/videolist.js'
export default {
data() {
return {
// 導航欄高度
customBarHeight: app.globalData.customBarH,
navbarBgcolor: '#21252b',
tabbarBgcolor: '#21252b',
tabNavLs: [
{label: '附近動態', badge: 5, lists: []},
{label: '關注', lists: []},
{label: '推薦', dot: true, lists: []},
],
// 當前選項卡
currentTab: 0,
// 當前視頻索引
currentVideo: 0,
// 視頻資料
videoList: videoJSON,
// 視頻是否播放中
isPlaying: false,
// 點擊次數
clickNum: 0,
// 視頻播放進度條
progressBar: 0,
clickTimer: null,
// 螢屏寬高
winWidth: '',
winHeight: '',
popupGoodsList: [],
showGoodsPopup: false,
showReplyPopup: false,
showSharePopup: false,
}
},
watch: {
currentTab(val) {
this.changeTabPanel(val)
}
},
computed:{
customBarMargin() {
return `margin-top: ${this.customBarHeight}px`
}
},
created() {
// 引入iconfont字體
// #ifdef APP-NVUE
const domModule = weex.requireModule('dom')
domModule.addRule('fontFace', {
fontFamily: "nvueIcon",
'src': "url('/static/fonts/iconfont.ttf')"
});
// #endif
let wW = uni.getSystemInfoSync().windowWidth
let wH = uni.getSystemInfoSync().windowHeight
this.winWidth = `${wW}px`
this.winHeight = `${wH}px`
},
methods: {
// 長按動態
handleDynamicMenu(e) {
let points
// #ifndef APP-NVUE
points = [e.touches[0].clientX, e.touches[0].clientY]
// #endif
// #ifdef APP-NVUE
points = [e.touches[0].screenX, e.touches[0].screenY]
// #endif
this.$refs.uapopup.open({
type: 'contextmenu',
follow: points,
btns: [
{text: '不感興趣'},
{text: '復制'},
{
text: '舉報',
style: 'color:#f00;',
click: () => {
this.$refs.uapopup.close()
}
},
],
})
},
/* ++++++++++ { 視頻播放模塊 } ++++++++++ */
getVideoCtx() {
// return this.$refs['vdplayer' + this.currentVideo][0]
return uni.createVideoContext('vdplayer'+ this.currentVideo, this)
},
// 垂直滑動視頻
handleSwipeVertical(e) {
let index = e.detail.current
this.progressBar = 0
this.isPlaying = false
let video = this.getVideoCtx()
if(!video) return
video.pause()
// 重新開始
video.seek(0)
this.currentVideo = index
// 自動播放
this.handlePlay()
},
handlePlay() {
let video = this.getVideoCtx()
if(!video) return
video.play()
this.isPlaying = true
},
handlePause() {
let video = this.getVideoCtx()
if(!video) return
video.pause()
this.isPlaying = false
},
// 點擊視頻(單擊/雙擊)
handleVideoClicked() {
this.clickTimer && clearTimeout(this.clickTimer)
this.clickNum++
this.clickTimer = setTimeout(() => {
if(this.clickNum >= 2) {
console.log('你雙擊了')
}else {
console.log('你單擊了')
if(this.isPlaying) {
this.handlePause()
}else {
this.handlePlay()
}
}
this.clickNum = 0
}, 250)
},
...
}
}
</script>
以上就是uniapp開發仿抖音/快手短視頻+直播實體,后續還會分享一些專案實戰案例,
最后附上兩個專案實體
◆ Vue3+Electron13+ElementPlus仿macui桌面管理平臺
https://blog.csdn.net/yanxinyun1990/article/details/118187205
◆ Vue3+electron11+antdv仿制QQ聊天實體
https://blog.csdn.net/yanxinyun1990/article/details/114162969

轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/301988.html
標籤:其他
