1.获取设备麦克风设备信息
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class Lesson20 : MonoBehaviour
{
void Start ( )
{
string [ ] strs = Microphone. devices;
for ( int i = 0 ; i < strs. Length; i++ )
{
print ( strs[ i] ) ;
}
}
}
2.开始录制、结束录制
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class Lesson20 : MonoBehaviour
{
public AudioClip clip;
void Update ( )
{
if ( Input. GetKeyDown ( KeyCode. Space) )
{
clip = Microphone. Start ( null , false , 10 , 44100 ) ;
}
if ( Input. GetKeyUp ( KeyCode. Space) )
{
Microphone. End ( null ) ;
AudioSource s = this . GetComponent < AudioSource> ( ) ;
if ( s == null )
{
s = this . gameObject. AddComponent < AudioSource> ( ) ;
}
s. clip = clip;
s. Play ( ) ;
}
}
}
3.获取音频数据用于存储或传输
using System. Collections ;
using System. Collections. Generic ;
using UnityEngine ;
public class Lesson20 : MonoBehaviour
{
public AudioClip clip;
void Update ( )
{
if ( Input. GetKeyDown ( KeyCode. Space) )
{
clip = Microphone. Start ( null , false , 10 , 44100 ) ;
}
if ( Input. GetKeyUp ( KeyCode. Space) )
{
Microphone. End ( null ) ;
float [ ] f = new float [ clip. channels * clip. samples] ;
clip. GetData ( f, 0 ) ;
}
}
}