Record some problems and experiences encountered by Unity when accessing Netease Yunxin SDK

Written in the front: I am a novice program, many problems may be caused by my too good, here is only a personal record. If there is a big guy who has a better solution, I hope to help me out.

1. Unity crashes

It may be because I didn't catch the exception, but after the Yunxin SDK is initialized, as long as the program fails to exit normally due to Unity reporting an error or other problems, Unity will directly freeze.

2. What should be paid attention to when using the SDK?

The interfaces provided by the SDK are all asynchronous interfaces. The execution result is received through the callback function. The UI object cannot be directly manipulated in the callback function. The result can be saved and updated in the update function, or updated in other ways. (The Loom class is provided in the demo to implement this function.)

3. Network message unboxing problem

The base class of network messages in Yunxin SDK is NIMIMMessage, and other types of network messages are basically derived from it, such as NIMAudioMessage, NIMTextMessage, NIMImageMessage, NIMTipMessage and so on. However, what is returned in the callback parameter accepted by the message is an object of type NIMIMMessage.

// 消息接受的回调
    public void OnMessageReceived( object sender, NIMReceiveMessageEventArgs args )
// 这是消息回调中,回调回来的参数 NIMReceiveMessageEventArgs 类的定义。
    public class NIMReceiveMessageEventArgs : EventArgs
    {
    
    
        public NIMReceiveMessageEventArgs(NIMReceivedMessage msg)
        {
    
    
            Message = msg;
        }
        public NIMReceivedMessage Message {
    
     get; set; }
    }
// 这是 NIMReceivedMessage 类的定义
    public class NIMReceivedMessage : NimJsonObject<NIMReceivedMessage>
    {
    
    
        internal const string MessageContentPath = "content";
        ...

        [JsonProperty(MessageContentPath)]
        public NIMIMMessage MessageContent {
    
     get; set; }
        ...
// 安卓工程 manifests 权限
    <!-- Enable internet for app!!!. -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- Getting the state of internet for app!!!. -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- write or read user data file for app!!!. -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_LOGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.CAMERA" />

4. Android permission acquisition problem

When it was first packaged into an Android project for real-device testing, an error was reported when initializing the SDK, indicating that it could not create_path. Then I changed a test machine and found that it can be initialized normally. Then go to check the problem of permission acquisition.

5. Pitfalls in Android Manifests

正常来说 Unity 导出安卓工程后,工程中的 manifests.xml 中 作为应用程序启动入口的 Activity 应该是 Unity 生成的 UnityPlayerActivity,然而接入了云信SDK后的 导出的安卓工程中的 manifests 应该是修改了原本 Unity 生成的
// 以下是接入云信SDK后的安卓工程的 manifests.xml 中活动的声明
       <activity
       // 主要是 name 这里的区别 
            **android:name="com.netease.nimlib.MainActivity"** 
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
            android:hardwareAccelerated="false"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="fullSensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
            <meta-data
                android:name="unityplayer.ForwardNativeEventsToDalvik"
                android:value="false" />
        </activity>
        <activity
            android:name="com.netease.nimlib.MainActivity"
            android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="fullSensor">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="unityplayer.UnityActivity"
                android:value="true" />
            <meta-data
                android:name="unityplayer.ForwardNativeEventsToDalvik"
                android:value="false" />
        </activity>

6. Record audio path and send local audio path

I/Unity: 音频信息:StopCapture rescode:200
file_path:/sdcard/com.netease.nim_unity_android_demo/NIM/audio/181366001.aac
file_ext:aac

The above is part of the audio information input after the recording is completed. file_path is the path of the audio.

//如果是发送本地文件,请给LocalFilePath赋值
msg.LocalFilePath="c:\\audio.aac";

Sending audio network messages requires the absolute path of the file, as shown in the figure above. The specific reason may be that Unity cannot recognize the file path of "/".

7. The local path of the attachment in the message

This interface is not mentioned in the Unity integration document. If it is the first time to connect to the SDK and connect to Unity first, it is easy to not know where the attachment is downloaded.

        /// <summary>
        /// 从消息的中获取附件(图片、语音、视频等)的本地路径
        /// </summary>
        /// <param name="msg">消息对象</param>
        /// <returns>消息如果有附件,不管是否已下载,返回附件的本地路径;消息如果没有附件,返回空字符串""</returns>
        public static string GetAttachmentPathFromMsg(NIMIMMessage msg)
        {
    
    
            var jsonMsg = msg.Serialize();
            var ptr = TalkNativeMethods.nim_talk_get_attachment_path_from_msg(jsonMsg);
            NimUtility.Utf8StringMarshaler marshaler = new NimUtility.Utf8StringMarshaler();
            var path = marshaler.MarshalNativeToManaged(ptr) as string;
            GlobalAPI.FreeBuffer(ptr);
            return path;
        }

8. Yunxin server API problem

The creation of a chat room needs to call the API of the Yunxin server. The Yunxin server only accepts Http Post requests. I downloaded the server-side API call demo example provided by Yunxin. I found that the C# demo does not seem to write how to call Add parameters, only Java version. So I used BestHttp, a third-party tool referenced by our project, and I can send requests with Yunxin server normally.

Not finished, because the company needs to change an instant messaging product, so this article has come to an end temporarily, if there is time in the future, it will continue to be updated and improved.

Guess you like

Origin blog.csdn.net/a525324105/article/details/106518085