uniapp H5端接入声网SDK agora-rtc-sdk

4 篇文章 0 订阅
1 篇文章 0 订阅

//当前为用户端不是主播端,只针对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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值