Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Как повторять звуки с микрофона | How to repeat sounds from microphone

Discussion in 'Audio & Video' started by Tumpirer, May 11, 2022.

  1. Tumpirer

    Tumpirer

    Joined:
    Dec 15, 2021
    Posts:
    2
    Я хочу сделать игру похожую на "Говорящий Том". В процессе разработки я столкнулся с проблемой не хватки опыта. Я искал информацию на многих форумах и видео. Прошу помочь мне сделать повтор звуков с микрофона.
    I want to make game like "Talking Tom'. But i don't know how to make repeat microphone sounds system.
    Please help me.
     
  2. FileThirteen

    FileThirteen

    Joined:
    Oct 23, 2012
    Posts:
    40
    I've recently been looking into working on an audio solution so I might have just the thing for you:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(AudioSource))]
    4. public class MicrophonePlayback : MonoBehaviour {
    5.  
    6.     AudioSource audioSource;
    7.  
    8.     [SerializeField]
    9.     int audioClipLength = 10;
    10.  
    11.     private void Awake() {
    12.         audioSource = GetComponent<AudioSource>();
    13.         audioSource.loop = true;
    14.     }
    15.  
    16.     private void Start() {
    17.         Microphone.GetDeviceCaps(null, out int minFreq, out int maxFreq);
    18.         audioSource.clip = Microphone.Start(null, true, audioClipLength, maxFreq);
    19.         audioSource.Play();
    20.     }
    21.  
    22. }
    You can put this on an object with an audio source and then hit play and this will play back your audio as you talk.

    The basics of it are Microphone.GetDeviceCaps() will get you the minimum and maximum frequencies your microphone can use.
    https://docs.unity3d.com/ScriptReference/Microphone.GetDeviceCaps.html
    Then Microphone.Start() will create your audio clip. It's important that audioSource.loop is set to true on the AudioSource.
    https://docs.unity3d.com/ScriptReference/Microphone.Start.html
    Unless you want to record for some time then playback the sound. You can adapt this script to do something like that where it records, then plays back. Good luck.