
function initCustomSound(uniqueId,assistCommand) {
    $(document).ready(function () {
        var file = "";
        if (sessionStorage.getItem("playstyle") !== "EasyVideo") {
            setAcceptAttribute(".g711u",uniqueId);
            $("#" + uniqueId + "voiceText").hide();
        } else {
            setAcceptAttribute(".g711u,.wav", uniqueId);
            $("#" + uniqueId + "voiceText").show();
        }
        $("#" + uniqueId + "myTab a").click(function (e) {
            e.preventDefault();
            $(this).tab("show");
            if ($(this).attr("aria-controls") == config.tabid + "voiceTexteHerf") {
                $("#" + uniqueId + "femen").prop("checked", true);
                $("#" + uniqueId + "voiceTextInput").val("");
            }
        });
        //语音转文字试听
        $("#" + uniqueId + "listenVideo").click(function () {
            $("#" + uniqueId + "listenVideo").attr("disabled", true);
            if ($("#" + uniqueId + "voiceTextInput").val() == "") {
                $("#" + uniqueId + "listenVideo").attr("disabled", false);
                return $.optionBootoast($.i18n.prop("i18n-voice-text"), "danger");
            } else {
                config.voiceFileResultType = 2;
                var text = $("#" + uniqueId + "voiceTextInput").val().replace(/\s/g, '');
                // 检查文本长度是否超过 500
                if (text.length > 500) {
                    $("#" + uniqueId + "listenVideo").attr("disabled", false);
                    return $.optionBootoast($.i18n.prop("i18n-voice-textMaxLength"), "danger");
                }
                $.MaskUtil.mask("");
                // var property = $("input[name='isChecked']:checked").val();
                var id="#" + uniqueId + "voiceTexteHerf";
                var div = document.querySelector(id);
                var property = div.querySelector('input[type="radio"]:checked').value;
                var vioceType = "mp3";
                textToVioce(text, vioceType, property, false,uniqueId,assistCommand);
            }
        });
        //语音转文字上传
        $("#" + uniqueId + "UploadVideo").click(function () {
            $("#" + uniqueId + "UploadVideo").attr("disabled", true);
            if ($("#" + uniqueId + "voiceTextInput").val() == "") {
                $("#" + uniqueId + "UploadVideo").attr("disabled", false);
                return $.optionBootoast($.i18n.prop("i18n-voice-text"), "danger");
            } else {
                config.voiceFileResultType = 1;
                var text = $("#" + uniqueId + "voiceTextInput").val().replace(/\s/g, '');;
                if (text.length > 500) {
                    $("#" + uniqueId + "UploadVideo").attr("disabled", false);
                    return $.optionBootoast($.i18n.prop("i18n-voice-textMaxLength"), "danger");
                }
                $.MaskUtil.mask($.i18n.prop("i18n-upload-btning"));
                var id="#" + uniqueId + "voiceTexteHerf";
                var div = document.querySelector(id);
                var property = div.querySelector('input[type="radio"]:checked').value;
                //var property = $("input[name='isChecked']:checked").val();
                var vioceType = "wav";
                textToVioce(text, vioceType, property, true,uniqueId,assistCommand);
            }
        });

        $("#" + uniqueId + "videoFileBtn").click(function () {
            document.getElementById(uniqueId + "getVideoFilePath").click();
        });
        $("#" + uniqueId + "getVideoFilePath").change(function (e) {
            var fileButton = document.getElementById(uniqueId + "getVideoFilePath");
            var fileExtension = fileButton.files[0].name.split(".").pop().toLowerCase();
            if (sessionStorage.getItem("playstyle") === "EasyVideo" && fileExtension !== "wav" && fileExtension !== "g711u") {
                return $.optionBootoast($.i18n.prop("i18n-upload-typeError"), "danger");
            }
            if (sessionStorage.getItem("playstyle") !== "EasyVideo" && fileExtension !== "g711u") {
                return $.optionBootoast($.i18n.prop("i18n-upload-typeError"), "danger");
            }
            //读取图片
            file = fileButton.files[0];
            $("#" + uniqueId + "fileName").text(file.name);
            $("#" + uniqueId + "hasSelectVideo").show();
            $("#" + uniqueId + "saveVideo").show();
            $("#" + uniqueId + "videoFileBtn").hide();
        });
        //删除已选取的视频文件
        $("#" + uniqueId + "delVideoBtn").click(function () {
            $("#" + uniqueId + "hasSelectVideo").hide();
            $("#" + uniqueId + "saveVideo").hide();
            $("#" + uniqueId + "videoFileBtn").show();
            $("#" + uniqueId + "getVideoFilePath").val("");
            file = "";
        });

        $("#" + uniqueId + "saveUploadVideo").click(function (e) {
            // 阻止事件冒泡
            e.stopPropagation();
            // 阻止表单默认提交行为
            e.preventDefault();
            $.MaskUtil.mask($.i18n.prop("i18n-upload-btning"));
            // $("#saveUploadVideo").attr("disabled", true);
            //wav文件要经过插件转换
            if (file.name.split(".").pop().toLowerCase() === "wav") {
                // if(file.size>128){
                //     $.MaskUtil.unmask();
                //     return $.optionBootoast($.i18n.prop("i18n-upload-fileSize"), "danger");
                // }
                var reader = new FileReader();
                // 读取文件内容并转换为 Data URL（Base64 编码）
                reader.readAsDataURL(file);
                reader.onload = function (e) {
                    var base64String = e.target.result.split(",")[1]; // 去除 Data URL 前缀
                    // 可以在这里使用 base64String，比如存储到全局变量
                    // console.log('Base64 编码内容:', base64String);
                    wavToG711U(base64String,uniqueId,assistCommand);
                };
                reader.onerror = function (error) {
                    console.error("读取文件时出错:", error);
                    $.MaskUtil.unmask();
                    $.optionBootoast($.i18n.prop("i18n-upload-typeError"), "danger");
                };
            } else {
                var url;
                if (sessionStorage.getItem("xystyle") == "p2p"){
                    url= config.baseUrl + "/mixinPut?url=" +  "/System/DeviceCustomFunction" + "&id=" + config.p2pId;
                }else{
                    url = config.baseUrl + "/System/DeviceCustomFunction";
                }
                var xhr = new XMLHttpRequest();
                xhr.open("PUT", url, true);
                xhr.setRequestHeader("Content-Type", "text/plain; charset=iso-8859-1");
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (xhr.status >= 200) {
                            if (sessionStorage.getItem("xystyle") == "p2p"){
                                if(xhr && xhr.responseText) {
                                    var responseData = JSON.parse(xhr.responseText);
                                    if (responseData[0].error == 0) {
                                        var xmlDoc = xmlStr2XmlObj(Base64.decode(responseData[0].attach.response));
                                        var statusCode = $(xmlDoc).find("statusCode").text();
                                        if (0 == statusCode) {
                                            $.optionBootoast($.i18n.prop("i18n-upload-success"), "success");
                                        } else {
                                            $.optionBootoast($.i18n.prop("i18n-upload-fail"), "danger");
                                        }
                                    } else {
                                        $.optionBootoast($.i18n.prop("i18n-upload-fail"), "danger");
                                    }
                                }else{
                                    $.optionBootoast($.i18n.prop("i18n-upload-fail"), "danger");
                                }
                            }else{
                                var xmlDoc = new DOMParser().parseFromString(xhr.responseText, "text/xml");
                                var statusCode = $(xmlDoc).find("statusCode").text();
                                if (statusCode == 0) {
                                    $.optionBootoast($.i18n.prop("i18n-upload-success"), "success");
                                } else {
                                    $.optionBootoast($.i18n.prop("i18n-upload-fail"), "danger");
                                }
                            }
                        } else {
                            $.optionBootoast($.i18n.prop("i18n-upload-fail"), "danger");
                        }
                        $.MaskUtil.unmask();
                        $("#" + uniqueId + "saveUploadVideo").attr("disabled", false);
                    }
                };

                // 构建自定义协议头部
                var header = "Main-Command:\"1\"\r\n" +
                    "Assist-Command:\"" + assistCommand + "\"\r\n" +
                    "Payload-Length:\"" + file.size + "\"\r\n" +
                    "Content:\r\n\r\n";

                var headerBytes = encodeToISO88591(header);

                // 拼接 header + 文件，生成完整请求体
                var blob = new Blob([headerBytes, file], {type: "application/octet-stream"});

                // 判断 Blob 对象的字节大小是否超过 383kb
                if (blob.size > config.maxTotal) {
                    $.MaskUtil.unmask();
                    $.optionBootoast($.i18n.prop("i18n-voice-textMax"), "danger");
                    $("#" + uniqueId + "saveUploadVideo").attr("disabled", false);
                    return;
                }

                xhr.send(blob);
            }

        });
    });
}

// 动态设置 accept 属性的函数
function setAcceptAttribute(newAccept, uniqueId) {
    var fileInput = document.getElementById(uniqueId + "getVideoFilePath");
    if (fileInput) {
        fileInput.accept = newAccept;
    }
}
