首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在按住按钮时录制音频,并在释放按钮时播放录制的音频

如何在按住按钮时录制音频,并在释放按钮时播放录制的音频
EN

Stack Overflow用户
提问于 2018-04-03 12:55:32
回答 2查看 6.4K关注 0票数 0

我是unity开发的初学者,我正在尝试做一个android应用程序,在unity中,用户可以录制音频,按下按钮,当他松开按钮时,录制的音频必须循环播放。到目前为止,我学会了如何从麦克风录制音频,以及播放音频的播放功能。

我不能做的事情

1.不能使用按住和释放按钮功能2.在我的工程目录中找不到保存的录音。

我遵循this thread来录制和回放音频,并且实现了相同的代码块。它现在正在做的是录制我的声音,并在我录制的时候回放,只需点击一下按钮。

任何关于这方面的帮助都将不胜感激。

提前谢谢。

这是我尝试过的代码实现:

代码语言:javascript
运行
复制
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();
}

}
EN

回答 2

Stack Overflow用户

发布于 2018-04-03 16:36:56

如果您想在鼠标上下移动时工作,则必须使用UnityEngine.EventSystems名称空间中的IPointDownHandlerIPointerUpHandler接口。要使用系统默认麦克风,应为Microphone.Start函数提供设备名称的空字符串

下面的脚本应该可以工作,但我没有机会测试它,因为我现在没有麦克风。

代码语言:javascript
运行
复制
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;
    }

}
票数 2
EN

Stack Overflow用户

发布于 2021-01-09 23:29:31

SavWav:(您需要将其作为脚本添加到项目中) code

using System.Threading; (用于睡眠功能)

代码语言:javascript
运行
复制
int frequency = 44100; //Wav format frequency
int numberOfSecondsToRecord = 10;

var rec = Microphone.Start("", false, numberOfSecondsToRecord, frequency);
Thread.Sleep(numberOfSecondsToRecord * 1000); //To miliseconds
if(rec != null)
{
  SavWav.Save(Application.persistentDataPath + "/test.wav", rec);
}

上面的代码基本上做的是启动麦克风,如果成功完成,将返回正在录制的现场音频的音频剪辑,然后您必须等待录制完成,然后才能保存它。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49622517

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档