Tejas Achar
Tejas Achar

Reputation: 21

How to record audio on button press hold and play the recorded audio on button release

I'm a beginner in unity development.I'm trying to do an android app in unity where the user can record audio press holds a button and when he releases it, the recorded audio must play on the loop. Until now I learned how o record audio from the microphone, and also the play function to play back the audio.

Things that I am unable to do

1.Cannot use button press hold and release function 2.Cannot locate the saved recording in my project directory.

I have followed this thread to record and playback audio And I have implemented the same block of code. What it's doing right now is recording my voice and playing it back as I record, on button click.

Any help regarding this will be appreciated.

Thanks in advance.

This is the code implementation i have tried:

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();
}

}

Upvotes: 0

Views: 9315

Answers (2)

Trake Vital
Trake Vital

Reputation: 1167

SavWav: (you'll need to add it as a script in your project) code

using System.Threading; (for sleep function)

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);
}

What the above code basically does is it starts the microphone which, if done successfully, returns the audioclip of the live audio being recorded, you must then wait until the recording finishes and only then save it.

Upvotes: 2

Steffan Venema
Steffan Venema

Reputation: 176

If you want to work with a mouse up and down you have to use the IPointDownHandler and the IPointerUpHandler interfaces that are in the UnityEngine.EventSystems namespace. To use your systems default microphone you should give the Microphone.Start function an empty string for the device name

The script below should work, but I didn't get a chance to test it because I have no microphone right now.

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;
    }

}

Upvotes: 2

Related Questions