//当前为用户端不是主播端,只针对uniapp h5端
<template>
<view>
<!-- 在页面中创建用于显示远程流的容器 -->
<view id="remote_container" class="remote_container"></view>\
</view>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue'
import AgoraRTC from 'agora-rtc-sdk';
const agoraAppId = '声网项目控制台App ID';
const channelName = '频道';
let client = '999999'; // Agora 客户端对象
const token ='声网项目控制台项目Token'
onMounted(async () => {
client = AgoraRTC.createClient({ mode: 'live', codec: 'h264' })
// 加入频道
client.init(agoraAppId, () => {
console.log('AgoraRTC client initialized');
client.join(token, channelName, null, (uid) => {
console.log('User ' + uid + ' joined------------------------');
});
});
// 监听远程流加入事件
client.on('stream-added', (evt) => {
const remoteStream = evt.stream;
const uid = remoteStream.getId();
// 订阅远程流
client.subscribe(remoteStream, (err) => {
console.log('Subscribe stream failed----------', err);
});
// 将远程流加入到页面中
remoteStream.play('remote_container');
});
});
onBeforeUnmount(() => {
// 离开频道
client.leave(() => {
console.log('Leave channel successfully');
}, (err) => {
console.log('Leave channel failed', err);
});
});
</script>
<style lang="scss" scoped>
.remote_container {
width: 100%;
height: 100vh;
}
</style>
后期更换了最新的sdk版本
import AgoraRTC from 'agora-rtc-sdk-ng' const client = AgoraRTC.createClient({ mode: 'live', codec: 'h264', mediaType: 'video' }) await client.join(agoraAppId.value, channelName.value, agoraToken.value,userInfo.value.id) // 监听远程流加入事件 client.on('user-published', async (user, mediaType) => { await client.subscribe(user, mediaType); if (mediaType === "video") { // 将远程流加入到页面中 user.videoTrack.play("DOMID") } if (mediaType === "audio") { user.audioTrack.play() } }) //被提出房间 client.on('user-left', (evt) => {}) //后期新增关闭开启直播音效,文档写了很多方法,结果发现了最简单的 //开启音效 user.audioTrack.play() //开启关闭 user.audioTrack.stop()