using UnityEngine;
using System.Collections;
public class MicPhoneScripts : MonoBehaviour
{
private AudioSource audioSource;
AudioClip clip;
void Awake()
{
audioSource = GetComponent<AudioSource>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
StartRecord();
}
if (Input.GetKeyUp(KeyCode.Space))
{
StopRecord();
}
}
/// <summary>
/// 開始錄音
/// </summary>
public void StartRecord()
{
Microphone.End(null);
clip = Microphone.Start(null, false, 20, 8000);
}
/// <summary>
/// 結束錄音
/// </summary>
public void StopRecord()
{
if (Microphone.IsRecording(null))
{
Microphone.End(null);
audioSource.clip = clip;
audioSource.Play();
}
}
}
注:UI中綁定StartRecord()和StopRecord()方法 打包到手機即可