C# 如何在按住按钮时录制音频并在松开按钮时播放录制的音频
我是unity开发的初学者。我正在尝试在unity中开发一个android应用程序,用户可以录制音频,按住按钮,当他释放按钮时,录制的音频必须在循环中播放。 直到现在,我还学习了如何从麦克风录制音频,以及播放音频的播放功能 我不能做的事情 1.不能使用按钮保持和释放功能 2.在我的项目目录中找不到保存的录制 我一直在录制和播放音频 我已经实现了相同的代码块。 它现在正在做的是录制我的声音,并在我录制时播放,按一下按钮 任何有关这方面的帮助都将不胜感激 提前谢谢 这是我尝试过的代码实现:C# 如何在按住按钮时录制音频并在松开按钮时播放录制的音频,c#,unity3d,C#,Unity3d,我是unity开发的初学者。我正在尝试在unity中开发一个android应用程序,用户可以录制音频,按住按钮,当他释放按钮时,录制的音频必须在循环中播放。 直到现在,我还学习了如何从麦克风录制音频,以及播放音频的播放功能 我不能做的事情 1.不能使用按钮保持和释放功能 2.在我的项目目录中找不到保存的录制 我一直在录制和播放音频 我已经实现了相同的代码块。 它现在正在做的是录制我的声音,并在我录制时播放,按一下按钮 任何有关这方面的帮助都将不胜感激 提前谢谢 这是我尝试过的代码实现: usin
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class audiorecorder : MonoBehaviour
{
AudioClip reco;
// Use this for initialization
public void onClick() {
AudioSource aud;
aud = GetComponent<AudioSource>();
foreach(string device in Microphone.devices)
{
Debug.Log(device);
}
reco = Microphone.Start("Built-in Microphone", true, 10, 44100);
aud.clip = reco;
aud.Play();
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共级录音机:单音行为
{
音频剪辑录制;
//用于初始化
公共void onClick(){
音频源aud;
aud=GetComponent();
foreach(麦克风中的字符串设备。设备)
{
调试日志(设备);
}
reco=麦克风启动(“内置麦克风”,正确,1044100);
aud.clip=reco;
aud.Play();
}
}
如果您想上下使用鼠标,您必须使用UnityEngine.EventSystems
命名空间中的IPointDownHandler
和IPointUpHandler
接口。要使用系统默认麦克风,应为麦克风.Start
功能提供一个空字符串作为设备名称
下面的脚本应该可以工作,但我没有机会测试它,因为我现在没有麦克风
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
//Use the PointerDown and PointerUP interfaces to detect a mouse down and up on a ui element
public class AudioRecorder : MonoBehaviour, IPointerDownHandler, IPointerUpHandler{
AudioClip recording;
//Keep this one as a global variable (outside the functions) too and use GetComponent during start to save resources
AudioSource audioSource;
private float startRecordingTime;
//Get the audiosource here to save resources
private void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void OnPointerUp(PointerEventData eventData)
{
//End the recording when the mouse comes back up, then play it
Microphone.End("");
//Trim the audioclip by the length of the recording
AudioClip recordingNew = AudioClip.Create(recording.name, (int)((Time.time - startRecordingTime) * recording.frequency), recording.channels, recording.frequency, false);
float[] data = new float[(int)((Time.time - startRecordingTime) * recording.frequency)];
recording.GetData(data, 0);
recordingNew.SetData(data, 0);
this.recording = recordingNew;
//Play recording
audioSource.clip = recording;
audioSource.Play();
}
public void OnPointerDown(PointerEventData eventData)
{
//Get the max frequency of a microphone, if it's less than 44100 record at the max frequency, else record at 44100
int minFreq;
int maxFreq;
int freq = 44100;
Microphone.GetDeviceCaps("", out minFreq, out maxFreq);
if (maxFreq < 44100)
freq = maxFreq;
//Start the recording, the length of 300 gives it a cap of 5 minutes
recording = Microphone.Start("", false, 300, 44100);
startRecordingTime = Time.time;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
//使用PointerDown和PointerUP接口检测鼠标在ui元素上的上下移动
公共类录音机:MonoBehavior、IPointerDownHandler、IPointerUpHandler{
音频剪辑录制;
//将此变量也保留为全局变量(函数之外),并在启动期间使用GetComponent来保存资源
声源;声源;
私人浮动开始记录时间;
//在此处获取音频源以节省资源
私有void Start()
{
audioSource=GetComponent();
}
POINTERUP上的公共无效(PointerEventData事件数据)
{
//当鼠标返回时结束录制,然后播放
麦克风。结束(“”);
//按录音长度修剪音频剪辑
AudioClip recordingNew=AudioClip.Create(recording.name,(int)((Time.Time-startRecordingTime)*recording.frequency),recording.channels,recording.frequency,false);
浮点[]数据=新浮点[(int)((Time.Time-startRecordingTime)*记录频率)];
记录.GetData(数据,0);
记录新的.SetData(数据,0);
this.recording=recordingNew;
//播放录音
audioSource.clip=录音;
audioSource.Play();
}
POINTERDOWN上的公共无效(PointerEventData事件数据)
{
//获取麦克风的最大频率,如果低于44100,则以最大频率录制,否则以44100录制
int minFreq;
int-maxFreq;
intfreq=44100;
麦克风.GetDeviceCaps(“,out minFreq,out maxFreq);
中频(最大频率<44100)
freq=maxFreq;
//开始录音,长度为300,上限为5分钟
录音=麦克风。开始(“,假,30044100);
startRecordingTime=Time.Time;
}
}
如果您想上下使用鼠标,您必须使用UnityEngine.EventSystems
命名空间中的IPointDownHandler
和IPointUpHandler
接口。要使用系统默认麦克风,应为麦克风.Start
功能提供一个空字符串作为设备名称
下面的脚本应该可以工作,但我没有机会测试它,因为我现在没有麦克风
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
//Use the PointerDown and PointerUP interfaces to detect a mouse down and up on a ui element
public class AudioRecorder : MonoBehaviour, IPointerDownHandler, IPointerUpHandler{
AudioClip recording;
//Keep this one as a global variable (outside the functions) too and use GetComponent during start to save resources
AudioSource audioSource;
private float startRecordingTime;
//Get the audiosource here to save resources
private void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void OnPointerUp(PointerEventData eventData)
{
//End the recording when the mouse comes back up, then play it
Microphone.End("");
//Trim the audioclip by the length of the recording
AudioClip recordingNew = AudioClip.Create(recording.name, (int)((Time.time - startRecordingTime) * recording.frequency), recording.channels, recording.frequency, false);
float[] data = new float[(int)((Time.time - startRecordingTime) * recording.frequency)];
recording.GetData(data, 0);
recordingNew.SetData(data, 0);
this.recording = recordingNew;
//Play recording
audioSource.clip = recording;
audioSource.Play();
}
public void OnPointerDown(PointerEventData eventData)
{
//Get the max frequency of a microphone, if it's less than 44100 record at the max frequency, else record at 44100
int minFreq;
int maxFreq;
int freq = 44100;
Microphone.GetDeviceCaps("", out minFreq, out maxFreq);
if (maxFreq < 44100)
freq = maxFreq;
//Start the recording, the length of 300 gives it a cap of 5 minutes
recording = Microphone.Start("", false, 300, 44100);
startRecordingTime = Time.time;
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用UnityEngine.EventSystems;
//使用PointerDown和PointerUP接口检测鼠标在ui元素上的上下移动
公共类录音机:MonoBehavior、IPointerDownHandler、IPointerUpHandler{
音频剪辑录制;
//将此变量也保留为全局变量(函数之外),并在启动期间使用GetComponent来保存资源
声源;声源;
私人浮动开始记录时间;
//在此处获取音频源以节省资源
私有void Start()
{
audioSource=GetComponent();
}
POINTERUP上的公共无效(PointerEventData事件数据)
{
//当鼠标返回时结束录制,然后播放
麦克风。结束(“”);
//按录音长度修剪音频剪辑
AudioClip recordingNew=AudioClip.Create(recording.name,(int)((Time.Time-startRecordingTime)*recording.frequency),recording.channels,recording.frequency,false);
浮点[]数据=新浮点[(int)((Time.Time-startRecordingTime)*记录频率)];
记录.GetData(数据,0);
记录新的.SetData(数据,0);
this.recording=recordingNew;
//播放录音
audioSource.clip=录音;
audioSource.Play();
}
POINTERDOWN上的公共无效(PointerEventData事件数据)
{
//获取麦克风的最大频率,如果低于44100,则以最大频率录制,否则以44100录制
int minFreq;
int-maxFreq;
intfreq=44100;
麦克风.GetDeviceCaps(“,out minFreq,out maxFreq);
中频(最大频率<44100)
freq=maxFreq;
//开始录音,