用于Unity的javascript到jslib转换插件

fdx2calv  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(302)

由于Unity在webgl中不支持麦克风,我尝试创建一个jslib来代替C#脚本(我在Unity论坛中搜索了很多,我认为这可能有效,但不确定)。
我找到了一个可以录制音频并播放的JavaScript脚本,但我试图将其转换为Unity Manual所说的jslib(https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html),但它不起作用。我很确定问题出在async/wait上(我只知道JavaScript的基本知识)。
如果有人能帮我解决这个问题,或者给予我一个可以帮助我的教程链接(我搜索了一下,但找不到任何有用的东西),我将不胜感激。
这是我找到的javascript(它的工作时,使用html):

const recordAudio = () =>
  new Promise(async resolve => {
    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
    const mediaRecorder = new MediaRecorder(stream);
    const audioChunks = [];

    mediaRecorder.addEventListener("dataavailable", event => {
      audioChunks.push(event.data);
    });

    const start = () => mediaRecorder.start();

    const stop = () =>
      new Promise(resolve => {
        mediaRecorder.addEventListener("stop", () => {
          const audioBlob = new Blob(audioChunks);
          const audioUrl = URL.createObjectURL(audioBlob);
          const audio = new Audio(audioUrl);
          const play = () => audio.play();
          resolve({ audioBlob, audioUrl, play });
        });

        mediaRecorder.stop();
      });

    resolve({ start, stop });
  });

let recorder = null;
let audio = null;

const recordStop = async () => {
  if (recorder) {
    audio = await recorder.stop();
    recorder = null;
  } else {
    recorder = await recordAudio();
    recorder.start();
  }
};

const playAudio = () => {
  if (audio && typeof audio.play === "function") {
    audio.play();
  }
};
eufgjt7s

eufgjt7s1#

要使其作为可从c#调用的插件工作,需要将其添加到已编译的JS库中。
有点像

const audioRecorder =
{
    $recorder : null,
    $audio : null,

    recordStop : async function() 
    {
        if (recorder) 
        {
            audio = await recorder.stop();
            recorder = null;
        }
        else 
        {
            const recordAudio = () =>
            {
                new Promise(async resolve => 
                {
                    const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
                    const mediaRecorder = new MediaRecorder(stream);
                    const audioChunks = [];

                   mediaRecorder.addEventListener("dataavailable", event => 
                    {
                        audioChunks.push(event.data);
                    });

                    const start = () => mediaRecorder.start();

                    const stop = () => new Promise(resolve => 
                    {
                        mediaRecorder.addEventListener("stop", () =>
                        {
                            const audioBlob = new Blob(audioChunks);
                            const audioUrl = URL.createObjectURL(audioBlob);
                            const audio = new Audio(audioUrl);
                            const play = () => audio.play();
                            resolve({ audioBlob, audioUrl, play });
                    });

                    mediaRecorder.stop();
                });

                resolve({ start, stop });
            });

            recorder = await recordAudio();
            recorder.start();
        }
    },

    playAudio : function()
    {
        if (audio && typeof audio.play === "function") 
        {
            audio.play();
        }
    }
};

autoAddDeps(audioRecorder, '$recorder');
autoAddDeps(audioRecorder, '$audio');

mergeInto(LibraryManager.library, audioRecorder);

然后在c#

[DllImport("__Internal")]
private static extern void recordAudio();

[DllImport("__Internal")]
private static extern void recordStop();

[DllImport("__Internal")]
private static extern void playAudio();

在构建WebGL应用程序时,编译器基本上会将jslib编译到Framework.js文件中,该文件看起来可能与您现在所拥有的类似。

相关问题