名称'麦克风'在当前上下文中不存在。打开统一(版本5.6.0f3)项目时出现此错误的是Visual Studio 2017在窗口8中。
[RequireComponent(typeof(AudioSource))] 公共类SingleMicrophoneCapture:MonoBehaviour {
//A boolean that flags whether there's a connected microphone
private bool micConnected = false;
//The maximum and minimum available recording frequencies
private int minFreq;
private int maxFreq;
//A handle to the attached AudioSource
public AudioSource goAudioSource;
public AudioClip recordedAudioClip;
[HideInInspector]
public AudioClip myAudioClip;
//public Text fileExist;
bool startRecording = false;
public Sprite[] recordingSprites;
public int count =0;
//int recordedFileCount =0;
public bool isDefaultAudioPlaying = false;
[SerializeField]
public Sprite[] playSprites;
public GameObject forwardButton;
public GameObject backwardButton;
public GameObject playButton;
public GameObject replayButton;
//Use this for initialization
public AudioClip[] allAudioClips;
public string storyName;
float[] samples;
public Dictionary<int,float> recordedClipDict;
void Start()
{
//ReplayButtonClicked ();
//Check if there is at least one microphone connected
recordedAudioClip= null;
if(Microphone.devices.Length <= 0)
{
//Throw a warning message at the console if there isn't
Debug.LogWarning("Microphone not connected!");
}
else //At least one microphone is present
{
//Set 'micConnected' to true
micConnected = true;
//Get the default microphone recording capabilities
Microphone.GetDeviceCaps(null, out minFreq, out maxFreq);
//According to the documentation, if minFreq and maxFreq are zero, the microphone supports any frequency...
if(minFreq == 0 && maxFreq == 0)
{
//...meaning 44100 Hz can be used as the recording sampling rate
maxFreq = 44100;
}
//Get the attached AudioSource component
goAudioSource = this.GetComponent<AudioSource>();
// mainAudioSource = Camera.main.GetComponent<AudioSource> ();
}
}
如何解决这个问题。
答案 0 :(得分:2)
您收到此错误是因为您使用的平台不支持Microphone
API。其中一个不支持Microphone
API的平台是WebGL。除了WebGL之外,可能还有其他平台没有麦克风支持。
从构建设置切换到支持Microphone
API的平台。
您还可以使用Unity的preprocessor directives来保护它,并确保在使用不支持它或未实现它的平台时使用Microphone
API 不
#if !UNITY_WEBGL
//YOUR Microphone CODE HERE
#endif
如果您确实需要使用Unity的WebGL中的麦克风,请制作插件或使用this一个(非免费)。
答案 1 :(得分:0)
我们看不到你的使用陈述。
但似乎你错过了
using UnityEngine.AudioModule;