StartRhythmPlayer.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using UnityEngine.Serialization;
  4. using UnityEngine.SceneManagement;
  5. using Agora.Rtc;
  6. using Agora.Util;
  7. using Logger = Agora.Util.Logger;
  8. using System.IO;
  9. using System;
  10. namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.StartRhythmPlayer
  11. {
  12. public class StartRhythmPlayer : MonoBehaviour
  13. {
  14. [FormerlySerializedAs("appIdInput")]
  15. [SerializeField]
  16. private AppIdInput _appIdInput;
  17. [Header("_____________Basic Configuration_____________")]
  18. [FormerlySerializedAs("APP_ID")]
  19. [SerializeField]
  20. private string _appID = "";
  21. [FormerlySerializedAs("TOKEN")]
  22. [SerializeField]
  23. private string _token = "";
  24. [FormerlySerializedAs("CHANNEL_NAME")]
  25. [SerializeField]
  26. private string _channelName = "";
  27. public Text LogText;
  28. internal Logger Log;
  29. internal IRtcEngine RtcEngine = null;
  30. // Use this for initialization
  31. private void Start()
  32. {
  33. LoadAssetData();
  34. if (CheckAppId())
  35. {
  36. SetupUI();
  37. InitEngine();
  38. JoinChannel();
  39. }
  40. }
  41. private void Update()
  42. {
  43. PermissionHelper.RequestMicrophontPermission();
  44. PermissionHelper.RequestCameraPermission();
  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 SetupUI()
  61. {
  62. var btn = this.gameObject.transform.Find("StartButton").GetComponent<Button>();
  63. btn.onClick.AddListener(OnStartButtonPress);
  64. btn = this.gameObject.transform.Find("StopButton").GetComponent<Button>();
  65. btn.onClick.AddListener(OnStopButtonPress);
  66. btn = this.gameObject.transform.Find("ConfigButton").GetComponent<Button>();
  67. btn.onClick.AddListener(OnConfigButtonPress);
  68. }
  69. private void InitEngine()
  70. {
  71. RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngine();
  72. UserEventHandler handler = new UserEventHandler(this);
  73. RtcEngineContext context = new RtcEngineContext(_appID, 0,
  74. CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING,
  75. AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_DEFAULT);
  76. RtcEngine.Initialize(context);
  77. RtcEngine.InitEventHandler(handler);
  78. }
  79. private void JoinChannel()
  80. {
  81. RtcEngine.EnableAudio();
  82. RtcEngine.EnableVideo();
  83. RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER);
  84. RtcEngine.JoinChannel(_token, _channelName);
  85. }
  86. private void OnStartButtonPress()
  87. {
  88. #if UNITY_ANDROID && !UNITY_EDITOR
  89. // On Android, the StreamingAssetPath is just accessed by /assets instead of Application.streamingAssetPath
  90. string sound1 = "/assets/audio/ding.mp3";
  91. string sound2 = "/assets/audio/dang.mp3";
  92. #else
  93. string sound1 = Path.Combine(Application.streamingAssetsPath, "audio/ding.mp3");
  94. string sound2 = Path.Combine(Application.streamingAssetsPath, "audio/dang.mp3");
  95. #endif
  96. AgoraRhythmPlayerConfig config = new AgoraRhythmPlayerConfig()
  97. {
  98. beatsPerMeasure = 4,
  99. beatsPerMinute = 60
  100. };
  101. int nRet = RtcEngine.StartRhythmPlayer(sound1, sound2, config);
  102. this.Log.UpdateLog("StartRhythmPlayer nRet:" + nRet);
  103. }
  104. private void OnStopButtonPress()
  105. {
  106. int nRet = RtcEngine.StopRhythmPlayer();
  107. this.Log.UpdateLog("StopRhythmPlayer nRet:" + nRet);
  108. }
  109. private void OnConfigButtonPress()
  110. {
  111. AgoraRhythmPlayerConfig config = new AgoraRhythmPlayerConfig()
  112. {
  113. beatsPerMeasure = 6,
  114. beatsPerMinute = 60
  115. };
  116. int nRet = RtcEngine.ConfigRhythmPlayer(config);
  117. this.Log.UpdateLog("ConfigRhythmPlayer nRet:" + nRet);
  118. this.Log.UpdateLog("beatsPerMeasure is config from 4 to 6");
  119. }
  120. private void OnDestroy()
  121. {
  122. Debug.Log("OnDestroy");
  123. if (RtcEngine == null) return;
  124. RtcEngine.InitEventHandler(null);
  125. RtcEngine.LeaveChannel();
  126. RtcEngine.Dispose();
  127. }
  128. internal string GetChannelName()
  129. {
  130. return _channelName;
  131. }
  132. }
  133. #region -- Agora Event ---
  134. internal class UserEventHandler : IRtcEngineEventHandler
  135. {
  136. private readonly StartRhythmPlayer _startRhythmPlayer;
  137. internal UserEventHandler(StartRhythmPlayer videoSample)
  138. {
  139. _startRhythmPlayer = videoSample;
  140. }
  141. public override void OnError(int err, string msg)
  142. {
  143. _startRhythmPlayer.Log.UpdateLog(string.Format("OnError err: {0}, msg: {1}", err, msg));
  144. }
  145. public override void OnJoinChannelSuccess(RtcConnection connection, int elapsed)
  146. {
  147. int build = 0;
  148. Debug.Log("Agora: OnJoinChannelSuccess ");
  149. _startRhythmPlayer.Log.UpdateLog(string.Format("sdk version: ${0}",
  150. _startRhythmPlayer.RtcEngine.GetVersion(ref build)));
  151. _startRhythmPlayer.Log.UpdateLog(
  152. string.Format("OnJoinChannelSuccess channelName: {0}, uid: {1}, elapsed: {2}",
  153. connection.channelId, connection.localUid, elapsed));
  154. }
  155. public override void OnRejoinChannelSuccess(RtcConnection connection, int elapsed)
  156. {
  157. _startRhythmPlayer.Log.UpdateLog("OnRejoinChannelSuccess");
  158. }
  159. public override void OnLeaveChannel(RtcConnection connection, RtcStats stats)
  160. {
  161. _startRhythmPlayer.Log.UpdateLog("OnLeaveChannel");
  162. }
  163. public override void OnClientRoleChanged(RtcConnection connection, CLIENT_ROLE_TYPE oldRole, CLIENT_ROLE_TYPE newRole)
  164. {
  165. _startRhythmPlayer.Log.UpdateLog("OnClientRoleChanged");
  166. }
  167. public override void OnUserJoined(RtcConnection connection, uint uid, int elapsed)
  168. {
  169. _startRhythmPlayer.Log.UpdateLog(string.Format("OnUserJoined uid: ${0} elapsed: ${1}", uid, elapsed));
  170. }
  171. public override void OnUserOffline(RtcConnection connection, uint uid, USER_OFFLINE_REASON_TYPE reason)
  172. {
  173. _startRhythmPlayer.Log.UpdateLog(string.Format("OnUserOffLine uid: ${0}, reason: ${1}", uid,
  174. (int)reason));
  175. }
  176. public override void OnRhythmPlayerStateChanged(RHYTHM_PLAYER_STATE_TYPE state, RHYTHM_PLAYER_ERROR_TYPE errorCode)
  177. {
  178. _startRhythmPlayer.Log.UpdateLog(string.Format("OnRhythmPlayerStateChanged {0},{1}", state, errorCode));
  179. }
  180. }
  181. #endregion
  182. }