• 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
-1
Question by Ghanbari · Dec 31, 2013 at 07:31 AM ·

How to save an audio clip to mp3 file

Hi

I'm trying to record all sounds in my scene that are playing by code not an external tool. I could record sounds and save them to WAV but it doesn't work on Android.I think I should save sounds to MP3 to make it working on Android.

what should I do??????

help plzzzz

here's my code to save WAV :

    #pragma strict
      
     import System.IO; // for FileStream
     import System; // for BitConverter and Byte Type
      
      
      
 private var bufferSize : int;
 private var numBuffers : int;
 private var outputRate : int = 44100;
 private var fileName : String = "recTest.wav";
 private var headerSize : int = 40; //default for uncompressed wav
      
 private var recOutput : boolean;
      
 private var fileStream : FileStream;
      
 function Awake()
 {
     AudioSettings.outputSampleRate = outputRate;
 }
      
 function Start()
 {
     AudioSettings.GetDSPBufferSize(bufferSize,numBuffers);
 }
      
 function OnGUI()
 {
     if(Input.GetKeyDown("r") || GUILayout.Button("Record!"))
     {
         //print("rec");
         if(recOutput == false)
         {
             StartWriting(fileName);
             recOutput = true;
         }
         else
         {
             recOutput = false;
             WriteHeader();  
             print("rec stop");
             
         }
     }   
 }
 
 function StartWriting(name : String)
     {
         fileStream = new FileStream(name, FileMode.Create);
         var emptyByte : byte = new byte();
        
         for(var i : int = 0; i<headerSize; i++) //preparing the header
         {
             fileStream.WriteByte(emptyByte);
         }
 }
      
 function OnAudioFilterRead(data : float[], channels : int)
     {
         if(recOutput)
         {
             ConvertAndWrite(data); //audio data is interlaced
         }
     }
      
     function ConvertAndWrite(dataSource : float[])
         {
        
             var intData : Int16[] = new Int16[dataSource.length];
             //converting in 2 steps : float[] to Int16[], //then Int16[] to Byte[]
        
             var bytesData : Byte[] = new Byte[dataSource.length*2];
             //bytesData array is twice the size of
             //dataSource array because a float converted in Int16 is 2 bytes.
        
             var rescaleFactor : int = 32767; //to convert float to Int16
        
             for (var i : int = 0; i<dataSource.length;i++)
             {
                 intData[i] = dataSource[i]*rescaleFactor;
                 var byteArr : Byte[] = new Byte[2];
                 byteArr = BitConverter.GetBytes(intData[i]);
                 byteArr.CopyTo(bytesData,i*2);
             }
        
         fileStream.Write(bytesData,0,bytesData.length);
     }
      
     function WriteHeader()
     {
        
         fileStream.Seek(0,SeekOrigin.Begin);
        
         var riff : Byte[] = System.Text.Encoding.UTF8.GetBytes("RIFF");
         fileStream.Write(riff, 0, 4);
        
         var chunkSize : Byte[] = BitConverter.GetBytes(fileStream.Length-8);
         fileStream.Write(chunkSize,0,4);
        
         var wave : Byte[] = System.Text.Encoding.UTF8.GetBytes("WAVE");
         fileStream.Write(wave,0,4);
        
         var fmt : Byte[] = System.Text.Encoding.UTF8.GetBytes("fmt ");
         fileStream.Write(fmt,0,4);
        
         var subChunk1 : Byte[] = BitConverter.GetBytes(16);
         fileStream.Write(subChunk1,0,4);
        
         var two : UInt16 = 2;
         var one : UInt16 = 1;
        
         var audioFormat : Byte[] = BitConverter.GetBytes(one);
         fileStream.Write(audioFormat,0,2);
        
         var numChannels : Byte[] = BitConverter.GetBytes(two);
         fileStream.Write(numChannels,0,2);
        
         var sampleRate : Byte[] = BitConverter.GetBytes(outputRate);
         fileStream.Write(sampleRate,0,4);
        
         var byteRate : Byte[] = BitConverter.GetBytes(outputRate*4);
         // sampleRate * bytesPerSample*number of channels, here 44100*2*2
      
         fileStream.Write(byteRate,0,4);
        
         var four : UInt16 = 4;
         var blockAlign : Byte[] = BitConverter.GetBytes(four);
         fileStream.Write(blockAlign,0,2);
        
         var sixteen : UInt16 = 16;
         var bitsPerSample : Byte[] = BitConverter.GetBytes(sixteen);
         fileStream.Write(bitsPerSample,0,2);
        
         var dataString : Byte[] = System.Text.Encoding.UTF8.GetBytes("data");
         fileStream.Write(dataString,0,4);
        
         var subChunk2 : Byte[] = BitConverter.GetBytes(fileStream.Length-headerSize);
         fileStream.Write(subChunk2,0,4);
        
         fileStream.Close();
     }
Comment
Add comment
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

2 Replies

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

Answer by sath · Dec 31, 2013 at 09:50 AM

recently I used this script on an Android project and it worked for me (for max 30 sec recording)

https://gist.github.com/darktable/2317063

it is not converter but save .wav that is working on Android

Comment
Add comment · Show 2 · 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 Ghanbari · Dec 31, 2013 at 11:06 AM 0
Share

tnx. but how do I use this script???

how do I record scene sounds by this code??? can you help me??

avatar image S_Byrnes · Jun 02, 2014 at 11:23 PM 0
Share

Hey Ghanbari, how did you end up getting this to work? I can't find where to have a record button such as if (Input.Get$$anonymous$$eyDown ("r"){ etc

avatar image
-2

Answer by tanoshimi · Dec 31, 2013 at 07:51 AM

Try Audacity: http://audacity.sourceforge.net/

Comment
Add comment · Show 1 · 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 DaveA · Dec 31, 2013 at 08:15 AM 0
Share

No, that's not something he can access at runtime on Android

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Audio Manager 1 Answer

I need multiple audio listeners but Unity is complaining. 0 Answers


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