@genkinaoko (元気なお香)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

【Unity】WebGL出力でデバイスマイクを使用したい。

解決したいこと

Unityでゲームを作成しています。(デバイスのマイクを使って遊ぶゲーム)
しかしながらWebGLで出力した所、
下記のエラーが発生し上手くビルドすることができません。
解決方法を教えてください。

発生している問題・エラー

Assets\Scripts\MicVolumeSample.cs(13,13): error CS0103: The name 'Microphone' does not exist in the current context
Error building Player because scripts had compiler errors
Build completed with a result of 'Failed' in 11 seconds (10627 ms)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
UnityEditor.BuildPlayerWindow+BuildMethodException: 19 errors
  at UnityEditor.BuildPlayerWindow+DefaultBuildMethods.BuildPlayer (UnityEditor.BuildPlayerOptions options) [0x002ca] in <68089899e4c84456bfc1de3436accf4a>:0 
  at UnityEditor.BuildPlayerWindow.CallBuildMethods (System.Boolean askForBuildLocation, UnityEditor.BuildOptions defaultBuildOptions) [0x00080] in <68089899e4c84456bfc1de3436accf4a>:0 
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)


該当するソースコード

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MicVolumeSample : MonoBehaviour {
    [SerializeField, Range(0f, 10f)] float m_gain = 1f; // 音量に掛ける倍率
    float m_volumeRate; // 音量(0-1)
    // Use this for initialization
    void Start () {
        AudioSource aud = GetComponent<AudioSource>();
        if ((aud != null)&&(Microphone.devices.Length>0)) // オーディオソースとマイクがある
        {
            string devName = Microphone.devices[0]; // 複数見つかってもとりあえず0番目のマイクを使用
            int minFreq, maxFreq;
            Microphone.GetDeviceCaps(devName, out minFreq, out maxFreq); // 最大最小サンプリング数を得る
            aud.clip = Microphone.Start(devName, true, 2, minFreq); // 音の大きさを取るだけなので最小サンプリングで十分
            aud.Play(); //マイクをオーディオソースとして実行(Play)開始
        }
    }

    // Update is called once per frame
    void Update () {
        Debug.Log(m_volumeRate);
    }

    // オーディオが読まれるたびに実行される
    private void OnAudioFilterRead(float[] data, int channels)
    {
        float sum = 0f;
        for (int i = 0; i < data.Length; ++i)
        {
            sum += Mathf.Abs(data[i]); // データ(波形)の絶対値を足す
        }
        // データ数で割ったものに倍率をかけて音量とする
        m_volumeRate = Mathf.Clamp01(sum * m_gain / (float)data.Length);
    }
}
0 likes

1Answer

image.png

そもそもMicrophoneクラスはWebGL未対応ですね、WebGLは制約がおおいので事前に公式ドキュメントを熟読なさることをおすすめします。

こういった回避策があるのでご検討ください。

0Like

Your answer might help someone💌