当前位置:首页 > uni-app

uniAPP直播

2026-02-05 19:07:31uni-app

使用uni-app实现直播功能

基础实现方案

// 在pages.json中配置
{
    "path": "pages/live/live",
    "style": {
        "navigationBarTitleText": "直播",
        "app-plus": {
            "titleNView": false,
            "style": "custom",
            "bounce": "none"
        }
    }
}

直播组件集成

推流端实现

<template>
    <view class="container">
        <live-pusher
            url="rtmp://your-push-address"
            mode="SD"
            beauty="1"
            whiteness="2"
            :aspect="'3:4'"
            style="width: 100%; height: 100%"
        ></live-pusher>
        <view class="control-panel">
            <button @click="toggleCamera">切换摄像头</button>
            <button @click="startPush">开始推流</button>
            <button @click="stopPush">停止推流</button>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            isPushing: false,
            camera: 'front'
        }
    },
    methods: {
        toggleCamera() {
            this.camera = this.camera === 'front' ? 'back' : 'front'
            this.context = uni.createLivePusherContext('live-pusher')
            this.context.toggleCamera()
        },
        startPush() {
            this.context.start()
            this.isPushing = true
        },
        stopPush() {
            this.context.stop()
            this.isPushing = false
        }
    }
}
</script>

播放端实现

<template>
    <view class="container">
        <live-player
            src="rtmp://your-play-address"
            mode="live"
            autoplay
            style="width: 100%; height: 100%"
            @statechange="statechange"
        ></live-player>
        <view class="control-panel">
            <button @click="play">播放</button>
            <button @click="pause">暂停</button>
            <button @click="fullscreen">全屏</button>
        </view>
    </view>
</template>

<script>
export default {
    methods: {
        statechange(e) {
            console.log('live-player statechange:', e.detail)
        },
        play() {
            this.context = uni.createLivePlayerContext('live-player')
            this.context.play()
        },
        pause() {
            this.context.pause()
        },
        fullscreen() {
            this.context.requestFullScreen({
                direction: 0
            })
        }
    }
}
</script>

高级功能实现

互动功能

// 实现弹幕功能
sendDanmu(text) {
    this.context.sendDanmu({
        text: text,
        color: this.getRandomColor()
    })
}

getRandomColor() {
    return '#' + Math.floor(Math.random() * 0xffffff).toString(16)
}

// 实现点赞功能
sendLike() {
    uni.request({
        url: 'your-api/like',
        method: 'POST',
        data: {
            liveId: this.liveId,
            userId: this.userId
        },
        success: (res) => {
            uni.showToast({
                title: '点赞成功'
            })
        }
    })
}

直播统计

// 实现观看人数统计
watchCount() {
    this.timer = setInterval(() => {
        uni.request({
            url: 'your-api/watchCount',
            data: { liveId: this.liveId },
            success: (res) => {
                this.watchNum = res.data.count
            }
        })
    }, 30000)
}

// 清除统计定时器
clearWatchCount() {
    if (this.timer) {
        clearInterval(this.timer)
    }
}

注意事项

  1. 直播功能需要获取相关权限
  2. 不同平台支持的格式可能不同
  3. 需要配置合适的服务器和CDN支持
  4. 注意性能优化,特别是移动端设备
// 性能优化建议
1. 合理设置分辨率
2. 根据网络情况动态调整码率
3. 实现断线重连机制
4. 做好电量管理

常见问题解决

  1. 推流失败检查:

    uniAPP直播

    • 检查推流地址是否正确
    • 检查网络连接
    • 检查权限是否获取
  2. 播放卡顿处理:

    uniAPP直播

    • 降低分辨率
    • 调整缓存策略
    • 检查CDN状态
  3. 跨平台兼容性问题:

    • 测试各平台表现
    • 使用条件编译处理差异
      // #ifdef APP-PLUS
      // 特定平台代码
      // #endif

标签: uniAPP
分享给朋友: