1 Star 1 Fork 0

agoraio-community/Agora-Unity-Quickstart

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
AudioCallRoute.cs 5.85 KB
一键复制 编辑 原始数据 按行查看 历史
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Agora.Rtc;
using io.agora.rtc.demo;
namespace Agora_RTC_Plugin.API_Example.Examples.Basic.AudioCallRoute
{
public class AudioCallRoute : MonoBehaviour
{
[FormerlySerializedAs("appIdInput")]
[SerializeField]
private AppIdInput _appIdInput;
[Header("_____________Basic Configuration_____________")]
[FormerlySerializedAs("APP_ID")]
[SerializeField]
private string _appID = "";
[FormerlySerializedAs("TOKEN")]
[SerializeField]
private string _token = "";
[FormerlySerializedAs("CHANNEL_NAME")]
[SerializeField]
private string _channelName = "";
public Text LogText;
internal Logger Log;
internal IRtcEngine RtcEngine = null;
public Toggle defaultAudioRouteToSpeakerphoneToggle;
public Toggle enableSpeakerPhoneToggle;
// Start is called before the first frame update
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
InitRtcEngine();
}
}
private void Update()
{
PermissionHelper.RequestMicrophontPermission();
}
private bool CheckAppId()
{
Log = new Logger(LogText);
return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset!!!!!");
}
//Show data in AgoraBasicProfile
[ContextMenu("ShowAgoraBasicProfileData")]
private void LoadAssetData()
{
if (_appIdInput == null) return;
_appID = _appIdInput.appID;
_token = _appIdInput.token;
_channelName = _appIdInput.channelName;
}
public void InitRtcEngine()
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
UserEventHandler handler = new UserEventHandler(this);
RtcEngineContext context = new RtcEngineContext();
context.appId = _appID;
context.channelProfile = CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING;
context.audioScenario = AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT;
context.areaCode = AREA_CODE.AREA_CODE_GLOB;
var result = RtcEngine.Initialize(context);
this.Log.UpdateLog("Initialize result : " + result);
RtcEngine.InitEventHandler(handler);
RtcEngine.EnableAudio();
RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
}
#region -- Button Events ---
public void OnDefaultAudioRouteToSpeakerphoneToggle(bool value) {
var result = RtcEngine.SetDefaultAudioRouteToSpeakerphone(value);
this.Log.UpdateLog(string.Format( "SetDefaultAudioRouteToSpeakerphone value: {0}, result: {1}", value, result));
}
public void OnEnableSpeakerPhoneToggle(bool value) {
var result = RtcEngine.SetEnableSpeakerphone(value);
this.Log.UpdateLog(string.Format("SetEnableSpeakerphone value: {0}, result: {1}", value, result));
}
public void JoinChannel()
{
RtcEngine.JoinChannel(_token, _channelName, "", 0);
}
public void LeaveChannel()
{
RtcEngine.LeaveChannel();
}
#endregion
private void OnDestroy()
{
Debug.Log("OnDestroy");
if (RtcEngine == null) return;
RtcEngine.InitEventHandler(null);
RtcEngine.LeaveChannel();
RtcEngine.Dispose();
}
}
#region -- Agora Event ---
internal class UserEventHandler : IRtcEngineEventHandler
{
private readonly AudioCallRoute _sample;
internal UserEventHandler(AudioCallRoute audioSample)
{
_sample = audioSample;
}
public override void OnError(int err, string msg)
{
_sample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
}
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
int build = 0;
_sample.Log.UpdateLog(string.Format("sdk version: ${0}",
_sample.RtcEngine.GetVersion(ref build)));
_sample.Log.UpdateLog(
string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
connection.channelId, connection.localUid, elapsed));
}
public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
{
_sample.Log.UpdateLog("OnRejoinChannelSuccess");
}
public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
{
_sample.Log.UpdateLog("OnLeaveChannel");
}
public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole, ClientRoleOptions newRoleOptions)
{
_sample.Log.UpdateLog("OnClientRoleChanged");
}
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
_sample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
}
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
_sample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
(int)reason));
}
public override void OnAudioRoutingChanged(int routing)
{
_sample.Log.UpdateLog(string.Format("OnAudioRoutingChanged: {0}", (AudioRoute)routing));
}
}
#endregion
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/agoraio-community/Agora-Unity-Quickstart.git
git@gitee.com:agoraio-community/Agora-Unity-Quickstart.git
agoraio-community
Agora-Unity-Quickstart
Agora-Unity-Quickstart
main

搜索帮助