Skip to content

Instantly share code, notes, and snippets.

@ijingo
Created February 2, 2024 10:37
Show Gist options
  • Save ijingo/faec8d53de89425ca868323fa55d87f0 to your computer and use it in GitHub Desktop.
Save ijingo/faec8d53de89425ca868323fa55d87f0 to your computer and use it in GitHub Desktop.
agora_ains
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
using Agora.Rtc;
namespace Agora_RTC_Plugin.API_Example.Examples.Basic.JoinChannelAudio
{
public class JoinChannelAudio : 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;
private IAudioDeviceManager _audioDeviceManager;
private DeviceInfo[] _audioPlaybackDeviceInfos;
public Dropdown _audioDeviceSelect;
// Start is called before the first frame update
private void Start()
{
LoadAssetData();
if (CheckAppId())
{
InitRtcEngine();
SetBasicConfiguration();
}
#if UNITY_IOS || UNITY_ANDROID
var text = GameObject.Find("Canvas/Scroll View/Viewport/Content/AudioDeviceManager").GetComponent<Text>();
text.text = "Audio device manager not support in this platform";
GameObject.Find("Canvas/Scroll View/Viewport/Content/AudioDeviceButton").SetActive(false);
GameObject.Find("Canvas/Scroll View/Viewport/Content/deviceIdSelect").SetActive(false);
GameObject.Find("Canvas/Scroll View/Viewport/Content/AudioSelectButton").SetActive(false);
#endif
}
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;
}
private void InitRtcEngine()
{
RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
UserEventHandler handler = new UserEventHandler(this);
RtcEngineContext context = new RtcEngineContext(_appID, 0,
CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
RtcEngine.Initialize(context);
RtcEngine.InitEventHandler(handler);
}
private void SetBasicConfiguration()
{
var res = RtcEngine.SetAINSMode(true, AUDIO_AINS_MODE.AINS_MODE_BALANCED);
Debug.Log("SetAINSMode return value=: " + res);
RtcEngine.EnableAudio();
RtcEngine.SetChannelProfile(CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_COMMUNICATION);
RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
}
#region -- Button Events ---
public void StartEchoTest()
{
RtcEngine.StartEchoTest(10);
Log.UpdateLog("StartEchoTest, speak now. You cannot conduct another echo test or join a channel before StopEchoTest");
}
public void StopEchoTest()
{
RtcEngine.StopEchoTest();
}
public void JoinChannel()
{
RtcEngine.JoinChannel(_token, _channelName);
}
public void LeaveChannel()
{
RtcEngine.LeaveChannel();
}
public void StopPublishAudio()
{
var options = new ChannelMediaOptions();
options.publishMicrophoneTrack.SetValue(false);
var nRet = RtcEngine.UpdateChannelMediaOptions(options);
this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
}
public void StartPublishAudio()
{
var options = new ChannelMediaOptions();
options.publishMicrophoneTrack.SetValue(true);
var nRet = RtcEngine.UpdateChannelMediaOptions(options);
this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
}
public void GetAudioPlaybackDevice()
{
_audioDeviceSelect.ClearOptions();
_audioDeviceManager = RtcEngine.GetAudioDeviceManager();
_audioPlaybackDeviceInfos = _audioDeviceManager.EnumeratePlaybackDevices();
Log.UpdateLog(string.Format("AudioPlaybackDevice count: {0}", _audioPlaybackDeviceInfos.Length));
for (var i = 0; i < _audioPlaybackDeviceInfos.Length; i++)
{
Log.UpdateLog(string.Format("AudioPlaybackDevice device index: {0}, name: {1}, id: {2}", i,
_audioPlaybackDeviceInfos[i].deviceName, _audioPlaybackDeviceInfos[i].deviceId));
}
_audioDeviceSelect.AddOptions(_audioPlaybackDeviceInfos.Select(w =>
new Dropdown.OptionData(
string.Format("{0} :{1}", w.deviceName, w.deviceId)))
.ToList());
}
public void SelectAudioPlaybackDevice()
{
if (_audioDeviceSelect == null) return;
var option = _audioDeviceSelect.options[_audioDeviceSelect.value].text;
if (string.IsNullOrEmpty(option)) return;
var deviceId = option.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[1];
var ret = _audioDeviceManager.SetPlaybackDevice(deviceId);
Log.UpdateLog("SelectAudioPlaybackDevice ret:" + ret + " , DeviceId: " + deviceId);
}
#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 JoinChannelAudio _audioSample;
internal UserEventHandler(JoinChannelAudio audioSample)
{
_audioSample = audioSample;
}
public override void OnError(int err, string msg)
{
_audioSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
}
public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
{
int build = 0;
_audioSample.Log.UpdateLog(string.Format("sdk version: ${0}",
_audioSample.RtcEngine.GetVersion(ref build)));
_audioSample.Log.UpdateLog(
string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
connection.channelId, connection.localUid, elapsed));
}
public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
{
_audioSample.Log.UpdateLog("OnRejoinChannelSuccess");
}
public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
{
_audioSample.Log.UpdateLog("OnLeaveChannel");
}
public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole, ClientRoleOptions newRoleOptions)
{
_audioSample.Log.UpdateLog("OnClientRoleChanged");
}
public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
{
_audioSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
}
public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
{
_audioSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
(int)reason));
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment