facebook twitter hatena line email

「Unity/Agora/voicechat community」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行160: 行160:
 
     UserVideoDict[uid] = videoSurface;
 
     UserVideoDict[uid] = videoSurface;
 
}
 
}
 +
</pre>
 +
===ハンドラ===
 +
<pre>
 +
channel1.ChannelOnJoinChannelSuccess = Channel1OnJoinChannelSuccessHandler; // 自分が接続開始
 +
channel1.ChannelOnLeaveChannel = Channel1OnLeaveChannelHandler; // 自分が退出時
 +
channel1.ChannelOnUserJoined = Channel1OnUserJoinedHandler; // 別ユーザが接続してきたとき
 +
channel1.ChannelOnError = Channel1OnErrorHandler; // エラー時
 +
channel1.ChannelOnUserOffLine = ChannelOnUserOfflineHandler; // 別ユーザーの接続が終了したとき
 +
channel1.ChannelOnScreenShareStarted = screenShareStartedHandler_MC; // 自分が映像開始時
 +
channel1.ChannelOnScreenShareStopped = screenShareStoppedHandler_MC; // 自分が映像停止時
 +
channel1.ChannelOnScreenShareCanceled = screenShareCanceledHandler_MC; // 自分が映像キャンセル時
 +
channel1.ChannelOnVideoSizeChanged = onVideoSizeChanged_MCHandler; // 映像サイズ変更時
 
</pre>
 
</pre>
 
===="PERMISSION_DENIED"エラーが出た場合====
 
===="PERMISSION_DENIED"エラーが出た場合====

2022年11月2日 (水) 12:52時点における版

agoraコミュニティ側

sdkはどれを使っている

unitysdkではなく、websdkを使ってる。

Assets/WebGLTemplates/AgoraTemplate2020/AgoraWebSDK

agoraインストール

  1. https://github.com/AgoraIO-Community/Agora_Unity_WebGL をDL
  2. Assets/AgoraEngine/Demo/SceneHomeを開く
  3. GameControllerのInspectorを開きAppIDを入力する
  4. Unityメインメニュー/File/BuildSettings/PlayerSetttings/WebGL/WebGLTemplateをAgoraTemplete2020
  5. Unityメインメニュー/File/BuildSettings/PlayerSetttings/SceneInBuildのSceneHomeとSceneHellowVideoに、チェックを追加
  6. WebGLでビルドすると、htmlがブラウザで開くのでJoinを確認

agoraSdkCWrapperなエラーが出る場合

詳細

DllNotFoundException: agoraSdkCWrapper assembly:<unknown assembly> type:<unknown type> member:(null)

UnityEditorで表示するとこのエラーが出る。WebGLでビルドしてブラウザで表示すると良い。

デモ版ソース解析

  • AgoraEngine/Demo/ButtonHandler.csに、ボタンの挙動が、記述されてる
  • AgoraEngine/Demo/TestHome.csが、テストロジック処理が、記述されてる
  • TestHome.onJoinButtonClickedが、動画音声処理
  • TestHome.onJoinAudienceが、配信受け取る(Audience)処理
  • TestHome.onJoinAudienceが通信周りロジック
  • AssetsAgoraEngine/Prefabs/AgoraEventHandlerを、ロードSceneに追加しておく。
  • Hostボタンのオンオフ処理は、Assets/AgoraEngine/Demo/TestHome.onJoinAudience.csのGameObject.Find("RoleButton")とAssets/API-Example/tools/ToggleStateButton.csあたり。Audience表示されてる時Broadcastされ、Host表示のときに、Audienceとなっている様子。
  • AgoraEngine/Demo/TestHelloUnityVideo.csが通信ロジック周りの処理が、記述されてる

動画音声オンオフ

  • AgoraEngine/Demo/TestHelloUnityVideo.csの以下部分
AudioVideoState.pubAudio = av.togglePubAudio.isOn;
AudioVideoState.pubVideo = av.togglePubVideo.isOn;
AudioVideoState.subAudio = av.toggleSubAudio.isOn;
AudioVideoState.subVideo = av.toggleSubVideo.isOn;

音声オフだと

こちらの処理になる。

gameController.onJoinButtonClicked(false);

Android時の権限追加

#if (UNITY_2018_3_OR_NEWER && UNITY_ANDROID)
	ArrayList permissionList = new ArrayList();
	permissionList.Add(Permission.Microphone);         
	permissionList.Add(Permission.Camera);   
        foreach(string permission in permissionList)
        {
            if (!Permission.HasUserAuthorizedPermission(permission))
            {                 
				Permission.RequestUserPermission(permission);
			}
        }
#endif

ビルド時の"Pointer_stringify"のエラー表示

以下エラーが出るが、動作に問題ない。

The JavaScript function 'Pointer_stringify(ptrToSomeCString)' is obsoleted and will be removed in a future Unity version. Please call 'UTF8ToString(ptrToSomeCString)' instead.
printErr @ webgl_project_audio.loader.js:1

デモから音声通話サンプル作成

VoiceScene.unityを追加して、VoiceScene.csを作成して、VoiceScene.unityのMainCameraへ追加

using UnityEngine;

public class VoiceScene : MonoBehaviour
{
    void Start()
    {
#if !UNITY_EDITOR && UNITY_WEBGL
        string AppID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        TestHelloUnityVideo app = null;
        // create app if nonexistent
        if (ReferenceEquals(app, null))
        {
            app = new TestHelloUnityVideo(); // create app
            app.loadEngine(AppID); // load engine
        }
        string ChannelName = "testchannel";
        bool enableVideo = false;
        bool muted = false;
        app.join(ChannelName, enableVideo, muted);
#endif
    }
}

Assets/AgoraEngine/Demo/AudioVideoState.csのフラグを追加

public class AudioVideoStates
{
    public bool subAudio = true;
    public bool subVideo = false;
    public bool pubAudio = true;
    public bool pubVideo = false;
}
  1. Assets/AgoraEngine/Demo/TestHelloUnityVideo.csのjoin()内の、SetupInitState();を削除
  2. AssetsAgoraEngine/Prefabs/AgoraEventHandlerを、ロードSceneに追加しておく。

切断処理

public void Disconnect()
{
    if (!ReferenceEquals(app, null))
    {
        app.leave(); // leave channel
        app.unloadEngine(); // delete engine
        app = null; // delete app
    }
}

ビルド後html表示時に"NullReferenceException"エラーとなる場合

エラー詳細

webgl_project_audio.framework.js:3 NullReferenceException: Object reference not set to an instance of an object.

Assets/AgoraEngine/Demo/TestHelloUnityVideo.csのjoin()内の、SetupInitState();を削除

AgoraTemplate2020が適用されない

  • Assets/WebGLTemplates/AgoraTemplate2020があるか確認する。
  • PCを再起動する

デモから画面共有サンプル作成

DevDemo側サンプルソース解析

  • SceneFuncTests.unityのApi_testHelperのGameObjectのFunctionalTest/DevDemo/Test/DVC_ShareScreen.csが、画面共有ソース
  • TestHome.csのonJoinButtonClicked()からシーン移動している
  • FunctionalTest/DevDemo/SceneHome2のボタンで、移動できる。

以下"SceneFuncTests"がロードできないというエラーとなる場合

以下エラーとなる場合

'SceneFuncTests' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.

To add a scene to the build settings use the menu File->Build Settings... PlayerSettingのSceneInBuildにSceneFuncTests.unityを追加すると良い。

以下"SceneFuncTests"がロードできないというエラーとなる場合

以下エラーとなる場合

clientmanager.js:862 Note this API should be replaced by startScreenCaptureForWeb instead.

Agora_Unity_WebGL/[build_path]/AgoraWebSDK/libs/clientmanager.js

startScreenCaptureByDisplayId()はNGで、startScreenCaptureForWebに置き換えるようなエラーが出る。

以下の方だとstartScreenCaptureForWebを、実行するサンプルがあるのでそちらを確認する。

  • Assets/FunctionalTest/NewScreenShareClientManager/AgoraClientManager.cs
  • Assets/FunctionalTest/NewScreenShareMChannel/AgoraMultiChannel2.cs

NewScreenShareClientManager側サンプルソース解析

  • StartScreenButtonを押す
  • ClientManagerTestシーンは、背景のぼかしオン・オフができる。配信前に画面確認ができない。
  • MainScreenNewシーンは、背景のぼかしオンができる。オフができない。配信前に画面確認ができる。
  • NewScreenShareClientチェックは、画面共有時に、動画配信とは別のwindowを開く感じ。
  • ドラッグ移動するコード。makeVideoView()とmakeImageSurface()ののどちらかが.AddComponent<UIElementDragger>();されてること。

配信元画面の大きさを変更

videoSurface.GetComponent<RawImage>().rectTransform.sizeDelta = v2 * 2f;

配信先画面の大きさを変更

if (remote)
{
    Vector2 v2 = AgoraUIUtils.GetScaledDimension(640, 360, EnforcingViewLength);
    videoSurface.GetComponent<RawImage>().rectTransform.sizeDelta = v2 * 2f;
    remoteUserDisplays.Add(videoSurface.gameObject);
    UserVideoDict[uid] = videoSurface;
}

ハンドラ

channel1.ChannelOnJoinChannelSuccess = Channel1OnJoinChannelSuccessHandler; // 自分が接続開始
channel1.ChannelOnLeaveChannel = Channel1OnLeaveChannelHandler; // 自分が退出時
channel1.ChannelOnUserJoined = Channel1OnUserJoinedHandler; // 別ユーザが接続してきたとき
channel1.ChannelOnError = Channel1OnErrorHandler; // エラー時
channel1.ChannelOnUserOffLine = ChannelOnUserOfflineHandler; // 別ユーザーの接続が終了したとき
channel1.ChannelOnScreenShareStarted = screenShareStartedHandler_MC; // 自分が映像開始時
channel1.ChannelOnScreenShareStopped = screenShareStoppedHandler_MC; // 自分が映像停止時
channel1.ChannelOnScreenShareCanceled = screenShareCanceledHandler_MC; // 自分が映像キャンセル時
channel1.ChannelOnVideoSizeChanged = onVideoSizeChanged_MCHandler; // 映像サイズ変更時

"PERMISSION_DENIED"エラーが出た場合

エラー詳細

AgoraRTCException: AgoraRTCError PERMISSION_DENIED: NotAllowedError: Permission denied by system
  1. macの場合は、macの設定/セキュリティとプライバシー/プライバシー/画面収録/Chromeをonに