unity游戏开发入门-声音控制游戏

声音控制游戏中人物的移动,关键在于声音的传入,这里关机键在于使用了Microphone
怎么使用呢?下面给一段我编写的声音传入的代码:(关键处已被标识)

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

public class vioce : MonoBehaviour {
public static float volumm;//得到的传入后的音量
AudioClip aud;//录制的信息
string device;//设备名字
// Use this for initialization
void Start () {
    device = Microphone.devices[0];//获取录制设备的名字,默认为0
    aud = Microphone.Start(device, true,999, 44100);//true表示循环、999表示录的长度、44100为默认采样率。
}

// Update is called once per frame
void Update () {
    volumm = getMaxVoice();
	
}


float getMaxVoice()//获取最大音量
{
    float getvolue = 0f;
    float[] volue = new float[128];//储存声音数据
    int offset = Microphone.GetPosition(device) - 128 + 1;//获取data开始的位置,GetPosition获取麦克风的录取终止位置
    if (offset < 0)
    {
        return 0;
    }
    aud.GetData(volue, offset);//录制的声音获取数据的方法
    for (int i = 0; i < 128; i++)//保持数组中存储的都是最大音量
    {
        float temp = volue[i];
        if (temp > getvolue)
        {
            getvolue = temp;
        }
    }
    return getvolue;
}

}

猜你喜欢

转载自blog.csdn.net/tfboys00/article/details/83001304