Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by fil · Nov 26, 2012 at 10:46 PM · audiosound

Save audio to a file [Solved]

Question

Hi, I need to record sound from micro and save it to a file. for now I have this:

  void OnGUI()
  	{
  	  if (GUI.Button(new Rect(10,10,60,50),"Record"))
  	  { 
  	    audio.clip= Microphone.Start ( null, false, 3, 44100 ); 
  	  }
  	  if (GUI.Button(new Rect(10,70,60,50),"Play"))
  	  { 
  	    audio.Play();
  	  }
      }

but it only save in buffer.

unity documents have this: EditorUtility.ExtractOggFile (http://docs.unity3d.com/Documentation/ScriptReference/EditorUtility.ExtractOggFile.html) but I don't understand how it works.

Could someone help me, please?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fil · Nov 27, 2012 at 11:54 AM 0
Share

It's me again :P

I found a solution on this post: http://forum.unity3d.com/threads/119295-Writing-AudioListener.GetOutputData-to-wav-problem?p=859871&viewfull=1#post859871

I'm using the C# example and it's working very good.

avatar image Paulius-Liekis · Nov 27, 2012 at 02:12 PM 0
Share

Please post your own answer as a proper answer and mark it as correct, so other can see it that it is solved. Welcome to Unity Answers.

avatar image fil · Nov 27, 2012 at 02:31 PM 0
Share

and how i do that? i've written the answer on the question and put [solved] in title

avatar image Paulius-Liekis · Nov 27, 2012 at 03:33 PM 0
Share

Ins$$anonymous$$d of pressing "add new comment" fill the text box bellow and press "Post your answer". Once your answer is posted you can mark it as correct. Then this question becomes "green" and gets removed from "Unanswered" list.

avatar image kbaumann2013 · Jun 10, 2013 at 01:06 AM 0
Share

This is awesome! Does it work for mobile as well?

I assume it has a different folder structure for saving files.

6 Replies

· Add your reply
  • Sort: 
avatar image
17
Best Answer

Answer by fil · Nov 27, 2012 at 03:44 PM

Answer

add this C# script to your project: https://gist.github.com/2317063 (credits to Dark Table)

on other C# script call SavWav.Save("myfile", myAudioClip);

for example:

 public class audioRec : MonoBehaviour {
 
     AudioClip myAudioClip; 
 
     void Start() {}
     void Update () {}
     
    void OnGUI()
    {
         if (GUI.Button(new Rect(10,10,60,50),"Record"))
     { 
         myAudioClip = Microphone.Start ( null, false, 10, 44100 );
     }
     if (GUI.Button(new Rect(10,70,60,50),"Save"))
     {
         SavWav.Save("myfile", myAudioClip);
 
 //        audio.Play();
         }
    }
 }

It saves the .Wav file to some strange folder. to change that, open SavWav.cs file and change line 41 (var filepath = Path.Combine(Application.persistentDataPath, filename);) to:

 var filepath = Path.Combine(Application.dataPath, filename);
 

hope it helps. credits to people on this post: http://forum.unity3d.com/threads/119295-Writing-AudioListener.GetOutputData-to-wav-problem

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tohmjudson · Mar 03, 2013 at 09:10 PM 0
Share

this is brilliant! this works perfect! is there away to save the files incrementally? ie myfile001, myfile002...

I can get through javascript, but I have only a slight idea about c#

avatar image fil · Mar 03, 2013 at 09:51 PM 0
Share

add a variable and increment it every time you record and save the file with "filename" + variable

avatar image tohmjudson · Mar 04, 2013 at 12:59 AM 0
Share

Thank you for replying...

I am so close

 using UnityEngine;
 public class audioRec_variable : $$anonymous$$onoBehaviour 
 {
  
 AudioClip myAudioClip; 
  
 void Start() {}
 void Update () {}
 void OnGUI()
            {
         if (GUI.Button(new Rect(10,10,60,50),"Record"))
             { 
             myAudioClip = $$anonymous$$icrophone.Start ( null, false, 10, 44100 );
             }
 
         if (GUI.Button(new Rect(10,70,60,50),"Save"))
         {
             
             for (int i = 1; ;i++)
                 if (System.IO.File.Exists("myfile"))
                       {
                     SavWav.Save("myfile" + i, myAudioClip);    
                     }        
                 else
                     {
                     SavWav.Save("myfile", myAudioClip); 
 //                      audio.Play();
                     }
            }
     }
 }
avatar image tohmjudson · Mar 04, 2013 at 03:51 AM 0
Share

I got it by changing the original SavWav.cs file

in line 40 to: filename += System.DateTime.Now.ToString("$$anonymous$$$$anonymous$$-dd-yy_hh-mm-ss") +".wav";

avatar image 5argon · Oct 10, 2013 at 06:54 AM 0
Share

The output from that script is, strangely, lossy. To fix that I replaced the

int rescaleFactor = 32767; //to convert float to Int16

with

float rescaleFactor = 32767;

And now it sounds lossless!

Show more comments
avatar image
1

Answer by mrtylmz · Aug 02, 2017 at 03:05 PM

Hi everyone, I came from the future :) I have an issue with save audio file. I can record and play audio, yet I cannot save the file. I get this error. Hope you help me. NullReferenceException: Object reference not set to an instance of an object Recorder.OnGUI () (at Assets/Recorder.cs:20)

 // Following code recorder code.
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(AudioSource))]
 public class Recorder : MonoBehaviour
 {
     public AudioClip myAudioClip;
     private SavWav savwav;
 
     void Start() { }
     void Update() { }
 
     void OnGUI()
     {
         if (GUI.Button(new Rect(10, 10, 60, 50), "Record")) {
             myAudioClip = Microphone.Start(null, false, 10, 44100);
         }
 
         if (GUI.Button(new Rect(10, 70, 60, 50), "Save")) {
 20)       -----------     savwav.Save("myfile", myAudioClip); // error
         }
 
         if (GUI.Button(new Rect(10, 130, 60, 50), "Play")) {
             AudioSource audio = GetComponent<AudioSource>();
             audio.clip = myAudioClip;
             audio.Play();
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 5argon · Aug 02, 2017 at 03:52 PM 0
Share

Hi I came from the past. You declared private SavWav savwav; and do nothing to it and call that of course it is currently null. The class is static, and all of the methods are static. You are not supposed to instantiate an instance of that class. Use the class name to call into the method directly. Also I thought your code would not compile because .Save is static you probably can't call from an instance.

avatar image mrtylmz 5argon · Aug 03, 2017 at 06:54 AM 0
Share

If I call not using private SavWav savwav, I get this error.

 An object reference is required to access non-static member `SavWav.Save(string, UnityEngine.AudioClip)'
 
 SavWav.Save("myfile", myAudioClip);

Edit: I solved the problem. I created gameobject and dragged recorder script into gameobject. But I forgot dragging SavWav script into game object. Thanks for interest.

avatar image agha502 · Jul 24, 2019 at 10:38 AM 0
Share

make the class of "SaveLoadWav" and all its functions static.

avatar image
0

Answer by Alexander21 · Feb 07, 2020 at 11:20 AM

[QUOTE]solved the problem. I created gameobject and dragged recorder script into gameobject. But I forgot dragging SavWav script into game object. Thanks for interest. [/QUOTE]

i have done like you. But i cant dragging a SavWav script because there is no monobehaviour class involved. So i dont know how to save the file.

Could you briefly explain how did you done this?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by danishsshaikh · Feb 11, 2020 at 03:20 PM

is there a way to save the Audio to mp3 and not wav? Thanks, Dan

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Remjie · Apr 23, 2020 at 07:29 PM

@Alexander21 : since SavWav is a class and the save function is static, you can just call it as you do for unity native class, like that :

     public void SaveMyFile()
         {
             SavWav.Save("myfile", myAudioClip);
         }

@danishsshaikh : you need a class to encode to mp3, like this one, but i've not tested it. https://github.com/BeatUpir/Unity3D-save-audioClip-to-MP3

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to create a random Audiosource loop? 2 Answers

Gun Audio reverbs off of Terrian- sounds horrible- What can I do? 0 Answers

Play sound on animation event? 3 Answers

3D Audio Source plays choppy/poping in web player (Video) 1 Answer

I need help about audio.PlayOneShot!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges