“ AudioClip”不采用带有0个参数的构造函数

时间:2018-11-01 16:22:19

标签: c#

在这行(即第10行)中的

表示“ AudioClip”没有采用带有0个参数的构造函数。如何解决?我知道AudioClip是Unity中的默认类。我应该在那里传递什么参数或如何解决这个问题?

AudioClip _clipRecord = new AudioClip()
int _sampleWindow = 128;

如何解决?

public class MicInput : MonoBehaviour {

    public static float MicLoudness;

    private string _device;

    //mic initialization
    void InitMic(){
        if(_device == null) _device = Microphone.devices[0];
        _clipRecord = Microphone.Start(_device, true, 999, 44100);
    }

    void StopMicrophone()
    {
        Microphone.End(_device);
    }


    AudioClip _clipRecord = new AudioClip();
    int _sampleWindow = 128;

    //get data from microphone into audioclip
    float  LevelMax()
    {
        float levelMax = 0;
        float[] waveData = new float[_sampleWindow];
        int micPosition = Microphone.GetPosition(null)-(_sampleWindow+1); // null means the first microphone
        if (micPosition < 0) return 0;
        _clipRecord.GetData(waveData, micPosition);
        // Getting a peak on the last 128 samples
        for (int i = 0; i < _sampleWindow; i++) {
            float wavePeak = waveData[i] * waveData[i];
            if (levelMax < wavePeak) {
                levelMax = wavePeak;
            }
        }
        return levelMax;
    }



    void Update()
    {
        // levelMax equals to the highest normalized value power 2, a small number because < 1
        // pass the value to a static var so we can access it from anywhere
        MicLoudness = LevelMax ();
    }

    bool _isInitialized;
    // start mic when scene starts
    void OnEnable()
    {
        InitMic();
        _isInitialized=true;
    }

    //stop mic when loading a new level or quit application
    void OnDisable()
    {
        StopMicrophone();
    }

    void OnDestroy()
    {
        StopMicrophone();
    }


    // make sure the mic gets started & stopped when application gets focused
    void OnApplicationFocus(bool focus) {
        if (focus)
        {
            //Debug.Log("Focus");

            if(!_isInitialized){
                //Debug.Log("Init Mic");
                InitMic();
                _isInitialized=true;
            }
        }    
        if (!focus)
        {
            //Debug.Log("Pause");
            StopMicrophone();
            //Debug.Log("Stop Mic");
            _isInitialized=false;

        }
    }
}

在这行(即第10行)中,它说“ AudioClip”没有采用带有0个参数的构造函数。如何解决? ---- AudioClip _clipRecord =新的AudioClip()----        --- int _sampleWindow = 128; ----

如何解决?

2 个答案:

答案 0 :(得分:0)

在AudioClip类中添加以下内容:

public AudioClip(){}

之所以会出现此问题,是因为尽管您最初在创建类时确实会得到一个空的ctor,但是一旦创建了一个非空的ctor,您就失去了它,因此,如果要使用它,则必须添加一个空的ctor。

答案 1 :(得分:0)

在新的统一版本中,只需更改new AudioClip();

AudioClip _clipRecord = null;

希望有帮助