Search Unity

Audio SOLVED: Voice Recording DoNotDestroy

Discussion in 'Audio & Video' started by nathanhawthorne12, Jan 17, 2019.

  1. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    Evening folks

    Pretty new to Unity in general and I have a little c# experience.

    Anyway.... I have 3 Scenes.
    1st Scene is the menu
    2nd Scene is the voice recording
    3rd scene is the game

    I would like the voicerecording to be available to other scenes, which I think i need is the DoNotDestroy script.

    The code I have for this script is

    Code (CSharp):
    1.  
    2. void Awake()
    3.     {
    4.         GameObject[] objs = GameObject.FindGameObjectsWithTag("EffectAudio");
    5.  
    6.         if (objs.Length > 1)
    7.         {
    8.             Destroy(this.gameObject);
    9.         }
    10.  
    11.         DontDestroyOnLoad(this.gameObject);
    12.     }
    For the recording of the sound I have a UI Button with an AudioSource attached, the code I have for the recording is below
    Code (CSharp):
    1.  
    2. public class RecordStart : MonoBehaviour
    3. {
    4.     static public AudioSource recordedAudio;
    5.     public bool looping = false; //Microphone.start looping true/false
    6.     public int recordingduration = 3; //microphone.start recording duration
    7.     public int recordingfrequency = 44100; //microphone.start recording frequency#
    8.  
    9.     public void Start()
    10.     {
    11.         recordedAudio = GetComponent<AudioSource>();
    12.     }
    13.  
    14.  
    15.     public void RecordAudio()
    16.     {
    17.         string device = Microphone.devices[0]; //putting first device in a var called device
    18.         recordedAudio.clip = Microphone.Start(device, looping, recordingduration, recordingfrequency); //Recording audio
    19.     }
    20.  
    21. }
    How would I go about keeping the recording so It is available throughout the game?
     
  2. nathanhawthorne12

    nathanhawthorne12

    Joined:
    Jan 2, 2019
    Posts:
    29
    Will mark this as solved

    I created a new Audio object
    On the record button I set the audiosource to this new object and then attached the script, the sound can be called on any scene now.

    Code (CSharp):
    1. public class RecordStart : MonoBehaviour
    2. {
    3.     static public AudioSource recordedAudio;  //defining audio source
    4.     public bool looping = false; //Microphone.start looping true/false
    5.     public int recordingduration = 3; //microphone.start recording duration
    6.     public int recordingfrequency = 44100; //microphone.start recording frequency#
    7.  
    8.     public void Start()
    9.     {
    10.         recordedAudio = GameObject.Find("EffectAudio").GetComponent<AudioSource>(); //defining audiosource
    11.     }
    12.  
    13.  
    14.     public void RecordAudio()
    15.     {
    16.         string device = Microphone.devices[0]; //putting first device in a var called device
    17.         recordedAudio.clip = Microphone.Start(device, looping, recordingduration, recordingfrequency); //Recording audio
    18.     }
    19.  
    20. }