uniapp实现直播的方法:首先通过推流,代码为【<button class="btn" @click="start">开始推流</button>】;然后使用video标签实现拉流即可。
本教程操作环境:windows7系统、uni-app2.5.1版本,该方法适用于所有品牌电脑。
(免费):uni-app开发教程
uniapp实现直播的方法:
1、推流
<template> <view class="content"> <template> <view> <live-pusher id="livePusher" ref="livePusher" class="livePusher" url="" **这里需要请求后端接口,拿到推流地址** mode="SD" :muted="true" :enable-camera="true" :auto-focus="true" :beauty="1" whiteness="2" aspect="9:16" @statechange="statechange" @netstatus="netstatus" @error="error" ></live-pusher> <button class="btn" @click="start">开始推流</button> <button class="btn" @click="pause">暂停推流</button> <button class="btn" @click="resume">resume</button> <button class="btn" @click="stop">停止推流</button> <button class="btn" @click="snapshot">快照</button> <button class="btn" @click="startPreview">开启摄像头预览</button> <button class="btn" @click="stopPreview">关闭摄像头预览</button> <button class="btn" @click="switchCamera">切换摄像头</button> <button class="btn" @click="bofang">去播放</button> </view> </template> </view> </template> <script> export default { data() { return { context:[] }; }, onReady() { // 注意:需要在onReady中 或 onLoad 延时text = uni.createLivePusherContext('livePusher', this); }, methods: { statechange(e) { console.log('statechange:' + JSON.stringify(e)); }, netstatus(e) { console.log('netstatus:' + JSON.stringify(e)); }, error(e) { console.log('error:' + JSON.stringify(e)); }, start(){ this.context.start({ success: a => { console.log('livePusher.start:' + JSON.stringify(a)); }, error:err=>{ console.log(err) } }); }, close() { this.context.close({ success: a => { console.log('livePusher.close:' + JSON.stringify(a)); } }); }, snapshot() { this.context.snapshot({ success: e => { console.log(JSON.stringify(e)); } }); }, resume() { this.context.resume({ success: a => { console.log('livePusher.resume:' + JSON.stringify(a)); } }); }, pause() { this.context.pause({ success: a => { console.log('livePusher.pause:' + JSON.stringify(a)); } }); }, stop() { this.context.stop({ success: a => { console.log(JSON.stringify(a)); } }); }, switchCamera() { this.context.switchCamera({ success: a => { console.log('livePusher.switchCamera:' + JSON.stringify(a)); } }); }, startPreview() { this.context.startPreview({ success: a => { console.log('livePusher.startPreview:' + JSON.stringify(a)); } }); }, stopPreview() { this.context.stopPreview({ success: a => { console.log('livePusher.stopPreview:' + JSON.stringify(a)); } }); }, bofang(){ this.$u.route({ url: 'pages/index/index' }) } } }; </script> <style> .content { display: flex; flex-direction: column; align-items: center; justify-content: center; } .logo { height: 200rpx; width: 200rpx; margin-top: 200rpx; margin-left: auto; margin-right: auto; margin-bottom: 50rpx; } .text-area { display: flex; justify-content: center; } .title { font-size: 36rpx; color: #8f8f94; } </style> **
2、拉流
这里是app拉流,用的是video标签,代码如下
<template> <view> <video src="" style="width: 100vw;height: 400rpx;" :autoplay="true" controls></video> </view> </template> <script> export default {} </script>
src是请求接口得到的拉流地址
相关免费学习:php编程
消息,据a早期发行in监测,LAB过去24小时成交量放大至近7日平均水平的3.9倍,价格累计下跌5...
2 阿里巴巴新AI模型称仅次于Anthropic的Fabl消息,阿里巴巴宣布其新AI模型Qwen3.8 Max的参数量达到2.4万亿,声称在领先的前沿AI模型中仅次...
3 Solana网络单日收入重回区块链榜首,为近消息,据Solanafloor数据,Solana在7月18日的网络收入位居所有区块链第一,这是近五个月以来的首...
4 Nouriel Roubini:AI可能迫使政府采用普遍基消息,Nouriel Roubini在接受Bloomberg TV采访时警告,人工智能的快速发展可能迫使各国政府选择普...
5 A股三大股指走低,深证成指跌2%7月20日,A股三大股指走低,深证成指跌2%,创业板指跌1.34%,沪指跌0.51%。...
6 MetaMask聘用朝鲜黑客,未造成实质损失消息,MetaMask聘请了与朝鲜有关的黑客作为开发者,该黑客GitHub账号imyugioh自2025年9月起已在L...
7 BTC、ETH、SOL和XRP ETF上周净流入情况消息,上周比特币、以太坊、索拉纳和瑞波币现货ETF均出现净流入。其中,比特币净流入7567万...
8 国信证券吴信坤:阶段性买点或已出现消息,国信证券策略首席分析师吴信坤发布最新研究报告表示,截至7月17日,全部A股PE为21.8倍...
9 Binance停止支持Moonriver和Moonbeam主网消息,Binance宣布停止支持Moonriver和Moonbeam主网,并开放上述代币通过Base网络进行充值和提现。...
10 SpaceX定于7月23日进行Starship第13次飞行测试消息,SpaceX计划于7月23日进行Starship的第13次飞行测试,此前在7月16日的发射中因发动机点火故...
成都来彰科技 蜀ICP备2025134723号-1
资讯来源互联网,如有版权问题请联系管理员删除。