AudioMixing.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.Advanced.AudioMixing
  8. {
  9. public class AudioMixing : 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. [SerializeField] public string Sound_URL = "";
  25. private string _localPath = "";
  26. public Text LogText;
  27. internal Logger Log;
  28. internal IRtcEngine RtcEngine = null;
  29. // Start is called before the first frame update
  30. private void Start()
  31. {
  32. LoadAssetData();
  33. if (CheckAppId())
  34. {
  35. InitRtcEngine();
  36. SetupUI();
  37. // enable it after joining
  38. EnableUI(false);
  39. JoinChannel();
  40. }
  41. }
  42. private void Update()
  43. {
  44. PermissionHelper.RequestMicrophontPermission();
  45. }
  46. //Show data in AgoraBasicProfile
  47. [ContextMenu("ShowAgoraBasicProfileData")]
  48. private void LoadAssetData()
  49. {
  50. if (_appIdInput == null) return;
  51. _appID = _appIdInput.appID;
  52. _token = _appIdInput.token;
  53. _channelName = _appIdInput.channelName;
  54. }
  55. private bool CheckAppId()
  56. {
  57. Log = new Logger(LogText);
  58. return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset");
  59. }
  60. private void InitRtcEngine()
  61. {
  62. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  63. UserEventHandler handler = new UserEventHandler(this);
  64. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  65. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  66. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  67. RtcEngine.Initialize(context);
  68. RtcEngine.InitEventHandler(handler);
  69. }
  70. private void SetupUI()
  71. {
  72. _mixingButton = GameObject.Find("MixButton").GetComponent<Button>();
  73. _mixingButton.onClick.AddListener(HandleAudioMixingButton);
  74. _effectButton = GameObject.Find("EffectButton").GetComponent<Button>();
  75. _effectButton.onClick.AddListener(HandleEffectButton);
  76. _urlToggle = GameObject.Find("Toggle").GetComponent<Toggle>();
  77. _loopbackToggle = GameObject.Find("Loopback").GetComponent<Toggle>();
  78. #if UNITY_ANDROID && !UNITY_EDITOR
  79. // On Android, the StreamingAssetPath is just accessed by /assets instead of Application.streamingAssetPath
  80. _localPath = "/assets/audio/Agora.io-Interactions.mp3";
  81. #else
  82. _localPath = Application.streamingAssetsPath + "/audio/" + "Agora.io-Interactions.mp3";
  83. #endif
  84. Log.UpdateLog(string.Format("the audio file path: {0}", _localPath));
  85. }
  86. internal void EnableUI(bool enable)
  87. {
  88. _mixingButton.enabled = enable;
  89. _effectButton.enabled = enable;
  90. }
  91. private void JoinChannel()
  92. {
  93. RtcEngine.EnableAudio();
  94. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  95. RtcEngine.JoinChannel(_token, _channelName);
  96. }
  97. #region -- Test Control logic ---
  98. private void StartAudioMixing()
  99. {
  100. Debug.Log("Playing with " + (_urlToggle.isOn ? "URL" : "local file"));
  101. var ret = RtcEngine.StartAudioMixing(_urlToggle.isOn ? Sound_URL : _localPath, _loopbackToggle.isOn, 1);
  102. Debug.Log("StartAudioMixing returns: " + ret);
  103. }
  104. private void PlayEffectTest()
  105. {
  106. Debug.Log("Playing with " + (_urlToggle.isOn ? "URL" : "local file"));
  107. RtcEngine.PlayEffect(1, _urlToggle.isOn ? Sound_URL : _localPath, 1, 1.0, 0, 100, true);
  108. }
  109. private void StopEffectTest()
  110. {
  111. RtcEngine.StopAllEffects();
  112. }
  113. #endregion
  114. private void OnDestroy()
  115. {
  116. Debug.Log("OnDestroy");
  117. if (RtcEngine == null) return;
  118. RtcEngine.InitEventHandler(null);
  119. RtcEngine.LeaveChannel();
  120. RtcEngine.Dispose();
  121. }
  122. #region -- Application UI Logic ---
  123. private bool _isMixing = false;
  124. private Button _mixingButton { get; set; }
  125. private void HandleAudioMixingButton()
  126. {
  127. if (_effectOn)
  128. {
  129. Log.UpdateLog("Testing Effect right now, can't play effect...");
  130. return;
  131. }
  132. if (_isMixing)
  133. {
  134. RtcEngine.StopAudioMixing();
  135. }
  136. else
  137. {
  138. StartAudioMixing();
  139. }
  140. _isMixing = !_isMixing;
  141. _mixingButton.GetComponentInChildren<Text>().text = (_isMixing ? "Stop Mixing" : "Start Mixing");
  142. }
  143. private bool _effectOn = false;
  144. private Button _effectButton { get; set; }
  145. private void HandleEffectButton()
  146. {
  147. if (_isMixing)
  148. {
  149. Log.UpdateLog("Testing Mixing right now, can't play effect...");
  150. return;
  151. }
  152. if (_effectOn)
  153. {
  154. StopEffectTest();
  155. }
  156. else
  157. {
  158. PlayEffectTest();
  159. }
  160. _effectOn = !_effectOn;
  161. _effectButton.GetComponentInChildren<Text>().text = (_effectOn ? "Stop Effect" : "Play Effect");
  162. }
  163. private Toggle _urlToggle { get; set; }
  164. private Toggle _loopbackToggle { get; set; }
  165. #endregion
  166. }
  167. #region -- Agora Event ---
  168. internal class UserEventHandler : IRtcEngineEventHandler
  169. {
  170. private readonly AudioMixing _audioMixing;
  171. internal UserEventHandler(AudioMixing audioMixing)
  172. {
  173. _audioMixing = audioMixing;
  174. }
  175. public override void OnError(int err, string msg)
  176. {
  177. _audioMixing.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  178. }
  179. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  180. {
  181. int build = 0;
  182. _audioMixing.Log.UpdateLog(string.Format("sdk version: ${0}",
  183. _audioMixing.RtcEngine.GetVersion(ref build)));
  184. _audioMixing.Log.UpdateLog(
  185. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  186. connection.channelId, connection.localUid, elapsed));
  187. _audioMixing.EnableUI(true);
  188. }
  189. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  190. {
  191. _audioMixing.Log.UpdateLog("OnRejoinChannelSuccess");
  192. }
  193. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  194. {
  195. _audioMixing.Log.UpdateLog("OnLeaveChannel");
  196. }
  197. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  198. {
  199. _audioMixing.Log.UpdateLog("OnClientRoleChanged");
  200. }
  201. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  202. {
  203. _audioMixing.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  204. }
  205. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  206. {
  207. _audioMixing.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  208. (int)reason));
  209. }
  210. public override void OnAudioMixingStateChanged(AUDIO_MIXING_STATE_TYPE state, AUDIO_MIXING_REASON_TYPE errorCode)
  211. {
  212. _audioMixing.Log.UpdateLog(string.Format("AUDIO_MIXING_STATE_TYPE: ${0}, AUDIO_MIXING_REASON_TYPE: ${1}",
  213. state, errorCode));
  214. }
  215. }
  216. #endregion
  217. }