using System; using UnityEngine; using UnityEngine.UI; using UnityEngine.Serialization; using Agora.Rtc; using Agora.Util; using Logger = Agora.Util.Logger; namespace Agora_RTC_Plugin.API_Example.Examples.Advanced.SpatialAudioWithMediaPlayer { public class SpatialAudioWithMediaPlayer : 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 IRtcEngineEx RtcEngine = null; internal IMediaPlayer MediaPlayer = null; private const string MPK_URL = "https://agoracdn.s3.us-west-1.amazonaws.com/videos/Agora.io-Interactions.mp4"; private Button _button1; private Button _button2; private Button _button3; public uint UidUseInEx = 123; public uint UidUseInMPK = 67890; public ILocalSpatialAudioEngine SpatialAudioEngine; public int x = 0; // Use this for initialization private void Start() { LoadAssetData(); if (CheckAppId()) { SetUpUI(); InitEngine(); InitMediaPlayer(); InitSpatialAudioEngine(); JoinChannelEx(_channelName, UidUseInEx); JoinChannelExWithMPK(_channelName, UidUseInMPK, MediaPlayer.GetId()); } } // Update is called once per frame private void Update() { PermissionHelper.RequestMicrophontPermission(); PermissionHelper.RequestCameraPermission(); } //Show data in AgoraBasicProfile [ContextMenu("ShowAgoraBasicProfileData")] private void LoadAssetData() { if (_appIdInput == null) return; _appID = _appIdInput.appID; _token = _appIdInput.token; _channelName = _appIdInput.channelName; } private bool CheckAppId() { Log = new Logger(LogText); return Log.DebugAssert(_appID.Length > 10, "Please fill in your appId in API-Example/profile/appIdInput.asset"); } private void InitEngine() { RtcEngine = Agora.Rtc.RtcEngine.CreateAgoraRtcEngineEx(); UserEventHandler handler = new UserEventHandler(this); RtcEngineContext context = new RtcEngineContext(_appID, 0, CHANNEL_PROFILE_TYPE.CHANNEL_PROFILE_LIVE_BROADCASTING, AUDIO_SCENARIO_TYPE.AUDIO_SCENARIO_GAME_STREAMING); var ret = RtcEngine.Initialize(context); Debug.Log("Agora: Initialize " + ret); RtcEngine.InitEventHandler(handler); } private void InitMediaPlayer() { MediaPlayer = RtcEngine.CreateMediaPlayer(); if (MediaPlayer == null) { Debug.Log("GetAgoraRtcMediaPlayer failed!"); return; } MpkEventHandler handler = new MpkEventHandler(this); MediaPlayer.InitEventHandler(handler); Debug.Log("playerId id: " + MediaPlayer.GetId()); } private void InitSpatialAudioEngine() { SpatialAudioEngine = RtcEngine.GetLocalSpatialAudioEngine(); var ret = SpatialAudioEngine.Initialize(); Debug.Log("_spatialAudioEngine: Initialize " + ret); SpatialAudioEngine.SetAudioRecvRange(30); } private void JoinChannelEx(string channelName, uint uid) { RtcEngine.EnableAudio(); RtcEngine.EnableVideo(); RtcEngine.EnableSpatialAudio(true); RtcEngine.SetClientRole(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); RtcConnection connection = new RtcConnection(); connection.channelId = channelName; connection.localUid = uid; ChannelMediaOptions options = new ChannelMediaOptions(); options.autoSubscribeAudio.SetValue(true); options.autoSubscribeVideo.SetValue(true); options.publishMicrophoneTrack.SetValue(false); options.publishCameraTrack.SetValue(true); options.enableAudioRecordingOrPlayout.SetValue(true); options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); var ret = RtcEngine.JoinChannelEx("", connection, options); Debug.Log("RtcEngineController JoinChannelEx returns: " + ret); } private void JoinChannelExWithMPK(string channelName, uint uid, int playerId) { RtcConnection connection = new RtcConnection(); connection.channelId = channelName; connection.localUid = uid; ChannelMediaOptions options = new ChannelMediaOptions(); options.autoSubscribeAudio.SetValue(false); options.autoSubscribeVideo.SetValue(true); //options.publishAudioTrack.SetValue(false); options.publishCameraTrack.SetValue(false); options.publishMediaPlayerAudioTrack.SetValue(true); options.publishMediaPlayerVideoTrack.SetValue(true); options.publishMediaPlayerId.SetValue(playerId); options.enableAudioRecordingOrPlayout.SetValue(false); options.clientRoleType.SetValue(CLIENT_ROLE_TYPE.CLIENT_ROLE_BROADCASTER); var ret = RtcEngine.JoinChannelEx("", connection, options); RtcEngine.UpdateChannelMediaOptionsEx(options, connection); Debug.Log("RtcEngineController JoinChannelEx_MPK returns: " + ret); } private void SetUpUI() { _button1 = GameObject.Find("Button1").GetComponent