JoinChannelAudio.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using Agora.Rtc;
  5. using Agora.Util;
  6. using Logger = Agora.Util.Logger;
  7. namespace Agora_RTC_Plugin.API_Example.Examples.Basic.JoinChannelAudio
  8. {
  9. public class JoinChannelAudio : MonoBehaviour
  10. {
  11. [FormerlySerializedAs("appIdInput")]
  12. [SerializeField]
  13. private AppIdInput _appIdInput;
  14. [Header("_____________Basic Configuration_____________")]
  15. [FormerlySerializedAs("APP_ID")]
  16. [SerializeField]
  17. private string _appID = "";
  18. [FormerlySerializedAs("TOKEN")]
  19. [SerializeField]
  20. private string _token = "";
  21. [FormerlySerializedAs("CHANNEL_NAME")]
  22. [SerializeField]
  23. private string _channelName = "";
  24. public Text LogText;
  25. internal Logger Log;
  26. internal IRtcEngine RtcEngine = null;
  27. private Button _stopPublishButton;
  28. public Button _startPublishButton;
  29. // Start is called before the first frame update
  30. private void Start()
  31. {
  32. LoadAssetData();
  33. if (CheckAppId())
  34. {
  35. InitRtcEngine();
  36. JoinChannel();
  37. SetupUI();
  38. }
  39. }
  40. private void Update()
  41. {
  42. PermissionHelper.RequestMicrophontPermission();
  43. }
  44. private bool CheckAppId()
  45. {
  46. Log = new Logger(LogText);
  47. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset!!!!!");
  48. }
  49. //Show data in AgoraBasicProfile
  50. [ContextMenu("ShowAgoraBasicProfileData")]
  51. private void LoadAssetData()
  52. {
  53. if (_appIdInput == null) return;
  54. _appID = _appIdInput.appID;
  55. _token = _appIdInput.token;
  56. _channelName = _appIdInput.channelName;
  57. }
  58. private void InitRtcEngine()
  59. {
  60. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  61. UserEventHandler handler = new UserEventHandler(this);
  62. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  63. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  64. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  65. RtcEngine.Initialize(context);
  66. RtcEngine.InitEventHandler(handler);
  67. }
  68. private void JoinChannel()
  69. {
  70. RtcEngine.EnableAudio();
  71. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  72. RtcEngine.JoinChannel(_token, _channelName);
  73. }
  74. private void SetupUI()
  75. {
  76. _stopPublishButton = GameObject.Find("StopButton").GetComponent<Button>();
  77. _stopPublishButton.onClick.AddListener(StopPublishAudio);
  78. _startPublishButton = GameObject.Find("StartButton").GetComponent<Button>();
  79. _startPublishButton.onClick.AddListener(StartPublishAudio);
  80. _startPublishButton.gameObject.SetActive(false);
  81. }
  82. private void StopPublishAudio()
  83. {
  84. var options = new ChannelMediaOptions();
  85. options.publishMicrophoneTrack.SetValue(false);
  86. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  87. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  88. _stopPublishButton.gameObject.SetActive(false);
  89. _startPublishButton.gameObject.SetActive(true);
  90. }
  91. private void StartPublishAudio()
  92. {
  93. var options = new ChannelMediaOptions();
  94. options.publishMicrophoneTrack.SetValue(true);
  95. var nRet = RtcEngine.UpdateChannelMediaOptions(options);
  96. this.Log.UpdateLog("UpdateChannelMediaOptions: " + nRet);
  97. _stopPublishButton.gameObject.SetActive(true);
  98. _startPublishButton.gameObject.SetActive(false);
  99. }
  100. private void OnLeaveBtnClick()
  101. {
  102. RtcEngine.InitEventHandler(null);
  103. RtcEngine.LeaveChannel();
  104. }
  105. private void OnDestroy()
  106. {
  107. Debug.Log("OnDestroy");
  108. if (RtcEngine == null) return;
  109. RtcEngine.InitEventHandler(null);
  110. RtcEngine.LeaveChannel();
  111. RtcEngine.Dispose();
  112. }
  113. }
  114. #region -- Agora Event ---
  115. internal class UserEventHandler : IRtcEngineEventHandler
  116. {
  117. private readonly JoinChannelAudio _audioSample;
  118. internal UserEventHandler(JoinChannelAudio audioSample)
  119. {
  120. _audioSample = audioSample;
  121. }
  122. public override void OnError(int err, string msg)
  123. {
  124. _audioSample.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  125. }
  126. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  127. {
  128. int build = 0;
  129. _audioSample.Log.UpdateLog(string.Format("sdk version: ${0}",
  130. _audioSample.RtcEngine.GetVersion(ref build)));
  131. _audioSample.Log.UpdateLog(
  132. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  133. connection.channelId, connection.localUid, elapsed));
  134. }
  135. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  136. {
  137. _audioSample.Log.UpdateLog("OnRejoinChannelSuccess");
  138. }
  139. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  140. {
  141. _audioSample.Log.UpdateLog("OnLeaveChannel");
  142. }
  143. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  144. {
  145. _audioSample.Log.UpdateLog("OnClientRoleChanged");
  146. }
  147. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  148. {
  149. _audioSample.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  150. }
  151. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  152. {
  153. _audioSample.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  154. (int)reason));
  155. }
  156. }
  157. #endregion
  158. }