Search Unity

Recording In-Game Sound And Saving on Android Devices?

Discussion in 'Scripting' started by --Brad--, Jun 14, 2014.

  1. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Hey all, I've been stuck on this problem for ages, I posted a question on unity answers but no-one could seem to help: http://answers.unity3d.com/questions/723394/audio-recorder-script-how-do-i-use-it.html

    Basically I just want to record everything happening in game when a user hits the key (eg Input.GetKeyDown etc), right now I can record the sounds just fine in unity and it saves it to my pc folder that contains my source files for my app, but I need it to save to a folder on android instead.

    What I have so far is this:

    Code (CSharp):
    1. #pragma strict
    2. import System.IO; // for FileStream
    3. import System; // for BitConverter and Byte Type
    4. public var moveRecord : GUITexture;
    5. private var bufferSize : int;
    6. private var numBuffers : int;
    7. private var outputRate : int = 44100;
    8. private var fileName = String.Format("My_App_{0}.wav", Guid.NewGuid().ToString());
    9. private var headerSize : int = 44; //default for uncompressed wav
    10. private var recOutput : boolean;
    11. private var fileStream : FileStream;
    12. function Awake()
    13. {
    14.     AudioSettings.outputSampleRate = outputRate;
    15. }
    16. function Start()
    17. {
    18.     AudioSettings.GetDSPBufferSize(bufferSize,numBuffers);
    19. }
    20. function Update()
    21. {
    22.     for (var touch: Touch in Input.touches) {
    23.                 if (moveRecord.HitTest (touch.position))
    24.                     {
    25.     print("rec");
    26.         if(recOutput == false)
    27.         {
    28.             StartWriting(fileName);
    29.             recOutput = true;
    30.         }
    31.         }else{
    32.             recOutput = false;
    33.             WriteHeader();    
    34.             print("rec stop");
    35.         }
    36.     }
    37. }
    38. function StartWriting(name : String)
    39. {
    40.     fileStream = new FileStream(name, FileMode.Create);
    41.     var emptyByte : byte = new byte();
    42.  
    43.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    44.     {
    45.         fileStream.WriteByte(emptyByte);
    46.     }
    47. }
    48. function OnAudioFilterRead(data : float[], channels : int)
    49. {
    50.     if(recOutput)
    51.     {
    52.         ConvertAndWrite(data); //audio data is interlaced
    53.     }
    54. }
    55. function ConvertAndWrite(dataSource : float[])
    56. {
    57.  
    58.     var intData : Int16[] = new Int16[dataSource.length];
    59. //converting in 2 steps : float[] to Int16[], //then Int16[] to Byte[]
    60.  
    61.     var bytesData : Byte[] = new Byte[dataSource.length*2];
    62. //bytesData array is twice the size of
    63. //dataSource array because a float converted in Int16 is 2 bytes.
    64.  
    65.     var rescaleFactor : int = 32767; //to convert float to Int16
    66.  
    67.     for (var i : int = 0; i<dataSource.length;i++)
    68.     {
    69.         intData = dataSource*rescaleFactor;
    70.         var byteArr : Byte[] = new Byte[2];
    71.         byteArr = BitConverter.GetBytes(intData);
    72.         byteArr.CopyTo(bytesData,i*2);
    73.     }
    74.  
    75.     fileStream.Write(bytesData,0,bytesData.length);
    76. }
    77. function WriteHeader()
    78. {
    79.  
    80.     fileStream.Seek(0,SeekOrigin.Begin);
    81.  
    82.     var riff : Byte[] = System.Text.Encoding.UTF8.GetBytes("RIFF");
    83.     fileStream.Write(riff,0,4);
    84.  
    85.     var chunkSize : Byte[] = BitConverter.GetBytes(fileStream.Length-8);
    86.     fileStream.Write(chunkSize,0,4);
    87.  
    88.     var wave : Byte[] = System.Text.Encoding.UTF8.GetBytes("WAVE");
    89.     fileStream.Write(wave,0,4);
    90.  
    91.     var fmt : Byte[] = System.Text.Encoding.UTF8.GetBytes("fmt ");
    92.     fileStream.Write(fmt,0,4);
    93.  
    94.     var subChunk1 : Byte[] = BitConverter.GetBytes(16);
    95.     fileStream.Write(subChunk1,0,4);
    96.  
    97.     var two : UInt16 = 2;
    98.     var one : UInt16 = 1;
    99.  
    100.     var audioFormat : Byte[] = BitConverter.GetBytes(one);
    101.     fileStream.Write(audioFormat,0,2);
    102.  
    103.     var numChannels : Byte[] = BitConverter.GetBytes(two);
    104.     fileStream.Write(numChannels,0,2);
    105.  
    106.     var sampleRate : Byte[] = BitConverter.GetBytes(outputRate);
    107.     fileStream.Write(sampleRate,0,4);
    108.  
    109.     var byteRate : Byte[] = BitConverter.GetBytes(outputRate*4);
    110. // sampleRate * bytesPerSample*number of channels, here 44100*2*2
    111.     fileStream.Write(byteRate,0,4);
    112.  
    113.     var four : UInt16 = 4;
    114.     var blockAlign : Byte[] = BitConverter.GetBytes(four);
    115.     fileStream.Write(blockAlign,0,2);
    116.  
    117.     var sixteen : UInt16 = 16;
    118.     var bitsPerSample : Byte[] = BitConverter.GetBytes(sixteen);
    119.     fileStream.Write(bitsPerSample,0,2);
    120.  
    121.     var dataString : Byte[] = System.Text.Encoding.UTF8.GetBytes("data");
    122.     fileStream.Write(dataString,0,4);
    123.  
    124.     var subChunk2 : Byte[] = BitConverter.GetBytes(fileStream.Length-headerSize);
    125.     fileStream.Write(subChunk2,0,4);
    126.  
    127.     fileStream.Close();
    128. }

    Any help is greatly appreciated!
     
    Last edited: Oct 2, 2014
  2. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi Brad,

    Do format code, it's so much nicer to read! Click Insert, code, and paste your code in there.

    That aside, the code you posted seems to be heavily inspired by some old code of mine. I must warn you, it's not the most elegant / optimized / safe way to write wav files!

    My audio framework, G-Audio, boasts a modular I/O system that you can setup in the inspector to write wav files safely and asynchronously ( no extra burden on the audio thread ). It can write mic input, audio listener, filtered or unfiltered, mono or multi channel wav files.

    Do have a look!

    Cheers,

    Gregzo
     
  3. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Oh alright, does that asset work with android though? Like, all I need to do is let the user record and then be able to use that recording outside of the program (for example, use it as a ringtone).

    And you are right, it was based off your code, all I changed was instead of saving the file as a pre-determined name that kept being over-written, I used Guid.NewGuid().ToString() to give it a unique name.
     
  4. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi Brad,

    No problem for using my code, I shared it for that very purpose at the time!

    G-Audio's I/O classes should work on Android, no problem. If all you need is to write AudioListener output, G-Audio's overkill, but if you plan on doing more lower level audio, I'll of course encourage you to try it out!

    About your issue: on mobile, Application.persistentDataPath gives you the documents path. When you open the file stream, it needs more than a file name, it needs a full path. Build it by appending your file name to persistentDataPath and you should be good to go.

    Cheers,

    Gregzo
     
  5. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Alright thanks man, great tip, I've researched it though, found ways it works, but the System.IO; was commonly used in the same line of code, not the top, so it's kind of thrown me off..

    Any suggestions as to where in the script it could go?
     
  6. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hum, not sure I understand what you mean.
    Both in UnityScript and in C#, imported namespaces all go at the top of script. Outside of the class' scope in C#.

    Clarify?
     
  7. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Well while I was trying to work out how to use it I found this code:

    Code (JavaScript):
    1. bool creatingFile = false;
    2. string fileName = "Screenshot.png"
    3. function Update() {
    4.     if(Input.GetMouseButtonDown(0)) {
    5.         Application.CaptureScreenshot(fileName);
    6.         creatingFile = true;
    7.     }
    8.     if (creatingFile) {
    9.         string origin = System.IO.Path.Combine(Application.persistentDataPath, fileName);
    10.         string destination = "/sdcard/ScreenCapture/" + fileName; // could be anything
    11.         if (System.IO.File.Exists(origin)) {
    12.             System.IO.File.Move(origin, destination);
    13.             creatingFile = false;
    14.         }
    15.     }
    16. }
    from here: http://stackoverflow.com/questions/...he-pictures-with-an-application-for-android-a

    But I tried ignoring that and just doing this in mine:

    Code (JavaScript):
    1. function StartWriting(name : String)
    2.  
    3. {
    4.  
    5.     fileStream = new FileStream(Application.persistentDataPath, name, FileMode.Create);
    6.  
    7.     var emptyByte : byte = new byte();
    8.  
    9.    
    10.  
    11.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    12.  
    13.     {
    but I got this error: Assets/SoundRecording.js(92,22): BCE0024: The type 'System.IO.FileStream' does not have a visible constructor that matches the argument list '(String, String, System.IO.FileMode)'.

    Which I knew it was going to be wrong, but I just had a stab in the dark..
     
  8. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    The FileStream constructor you want to use takes just 2 parameters, a path and a FileMode. You're trying to pass it 2 strings...

    Just build your path before:

    Code (CSharp):
    1. function StartWriting( relativePath : String)
    2. {
    3.       var fullPath : String = Path.Combine( Application.PersistentDataPath, relativePath );
    4.       fileStream = new FileStream( fullPath, FileMode.Create);
    5.       //Etc...
    6. }
    If your relative path contains sub-directories ( not just the file name ), you should make sure they exist and create them if they don't, like the example you quote does.

    Cheers,

    G
     
    Last edited: Jun 20, 2014
  9. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Ok thanks man, but I've just tried it like so:

    Code (JavaScript):
    1. function StartWriting(name : String)
    2.  
    3. {
    4.     string fullPath = Path.Combine (Application.PersistentDataPath, relativePath);
    5.  
    6.     fileStream = new FileStream(fullPath, FileMode.Create);
    7.  
    8.     var emptyByte : byte = new byte();
    And I'm getting a semi-colon error, but I know the colon is there..

    Assets/SoundRecording.js(91,11): UCE0001: ';' expected. Insert a semicolon at the end.

    I even tried removing it and it didn't change anything..

    Edit: that line is the string fullPath line
     
  10. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Sorry, was writing in C#, forgot my UnityScript...

    Edited.
     
  11. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Alright awesome, no worries man.

    I just need to work out how to get the relativePath now, I've looked it up but not much has been said about it, not even on the unity scripting page..

    Code (JavaScript):
    1. var fullPath : String = Path.Combine(Application.persistentDataPath, relativePath);
    that returns me this error: Assets/SoundRecording.js(89,74): BCE0005: Unknown identifier: 'relativePath'.

    so is that function also apart of only C# or does it work with JS too?
     
  12. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    relativePath is the name of the parameter in the function I posted. It replaces name, as you don't necessarily want to write to the persistentDataPath folder directly, but maybe in a sub folder.

    Don't take this badly, but you really need to do a few scripting tutorials...

    Cheers,

    Gregzo
     
  13. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Oh, I see now, sorry for that I was confused back then and couldn't figure it out, I've done what you've suggested but I'm not sure if/or where it's saving to on the device.. is there a way I can make my app create it's own folder called, say "Music Game" and then store all of the recordings a user made in that?
     
  14. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
  15. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Thank you,

    While I'm perfectly capable of Googling things, I didn't realize the method was called Directory for this language, I was repeatedly searching for persistent data path, folders, etc.

    So I've been able to create a folder now, but I'm still unable to get the file to save there instead of the main path, any ideas?:

    Code (JavaScript):
    1. #pragma strict
    2. import System.IO; // for FileStream
    3. import System; // for BitConverter and Byte Type
    4.  
    5. private var bufferSize : int;
    6. private var numBuffers : int;
    7. private var outputRate : int = 44100;
    8. private var fileName = String.Format("Music_File_{0}.wav", Guid.NewGuid().ToString());
    9. private var headerSize : int = 44; //default for uncompressed wav
    10. private var folderName = "Music Files";
    11.  
    12. private var recOutput : boolean;
    13. private var fileStream : FileStream;
    14. function Awake()
    15. {
    16.     AudioSettings.outputSampleRate = outputRate;
    17. }
    18. function Start()
    19. {
    20.     AudioSettings.GetDSPBufferSize(bufferSize,numBuffers);
    21. }
    22. function Update()
    23. {
    24.     if(moveRecord.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
    25.     //if(Input.GetKeyDown("r"))
    26.     {
    27.     print("rec");
    28.         if(recOutput == false)
    29.         {
    30.             StartWriting(fileName);
    31.             recOutput = true;
    32.         }
    33.         else
    34.         {
    35.             recOutput = false;
    36.             WriteHeader();    
    37.             print("rec stop");
    38.         }
    39.     }
    40. }
    41.  
    42. if (!Directory.Exists(folderName))
    43.     Directory.CreateDirectory(folderName);
    44. function StartWriting(folderName : String)
    45. {
    46.     var fullPath : String = Path.Combine(Application.persistentDataPath, folderName);
    47.     fileStream = new FileStream(folderName, FileMode.Create);
    48.     var emptyByte : byte = new byte();
    49.  
    50.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    51.     {
    52.         fileStream.WriteByte(emptyByte);
    53.     }
    54. }
    55. //The rest of the file stream
    56. }
    I know I'm being a pain in the ass, but I'd really appreciate some help
     
  16. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi,

    Your code is quite messy: fileName, folderName, StartWriting taking folderName as parameter but not using the filename...

    Step by step:

    1)You have a folder name. In Awake or Start, build the folder path by combining persistent data path and your folder name.
    2) Create the folder if it doesn't exist.
    3) When you record, build the file path by combining the folder path and the file name.
    4) Create your file stream with file path.

    Summing up:
    -folderName is constant and up to you.
    -folderPath is persistentDataPath/folderName. Build in Awake.
    -filePath is folderPath/fileName. Build on rec.
    -FileStream is built with filePath.

    Stir and enjoy.

    Cheers,

    Gregzo
     
  17. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Hey thanks man, I really appreciate that! I understand how it works now!

    But when trying to do what you suggested, I wrote the code I thought would work but I get errors, I feel like it's soooo close to working, here's what I've got now:

    Code (JavaScript):
    1. #pragma strict
    2. import System.IO; // for FileStream
    3. import System; // for BitConverter and Byte Type
    4. public var moveRecord : GUITexture;
    5. private var bufferSize : int;
    6. private var numBuffers : int;
    7. private var outputRate : int = 44100;
    8. private var fileName = String.Format("Music_File_{0}.wav", Guid.NewGuid().ToString());
    9. private var headerSize : int = 44; //default for uncompressed wav
    10. private var folderName = "Music Files";
    11. private var folderPath : String;
    12. private var filePath : String;
    13.  
    14. private var recOutput : boolean;
    15. private var fileStream : FileStream;
    16. function Awake()
    17. {
    18.     if (!Directory.Exists(folderName))
    19.     Directory.CreateDirectory(folderName);
    20.     AudioSettings.outputSampleRate = outputRate;
    21.     var folderPath : String = Path.Combine(Application.persistentDataPath/folderName);
    22.     var filePath : String = Path.Combine(folderPath/fileName);
    23. }
    24. function Start()
    25. {
    26.     AudioSettings.GetDSPBufferSize(bufferSize,numBuffers);
    27. }
    28. function Update()
    29. {
    30.     if(moveRecord.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
    31.     //if(Input.GetKeyDown("r"))
    32.     {
    33.     print("rec");
    34.         if(recOutput == false)
    35.         {
    36.             StartWriting(fileName);
    37.             recOutput = true;
    38.         }
    39.         else
    40.         {
    41.             recOutput = false;
    42.             WriteHeader();    
    43.             print("rec stop");
    44.         }
    45.     }
    46. }
    47. function StartWriting(fileName : String)
    48. {
    49.    
    50.     fileStream = new FileStream(filePath, FileMode.Create);
    51.     var emptyByte : byte = new byte();
    52.  
    53.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    54.     {
    55.         fileStream.WriteByte(emptyByte);
    56.     }
    57. }
    58. function OnAudioFilterRead(data : float[], channels : int)
    59. {
    60.     if(recOutput)
    61.     {
    62.         ConvertAndWrite(data); //audio data is interlaced
    63.     }
    64. }
    65.  
    66. //the rest.
    The errors I get are: "SoundRecording.js(26,74): BCE0051: Operator '/' cannot be used with a left hand side of type 'String' and a right hand side of type 'String'." (same error for line (27, 52))

    and I get an "EOF expecting }" error when trying it a different way
     
  18. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    Hi,

    I wrote '/' as shorthand, not meant to be code!
    Path.Combine takes 2 arguments and adds the path separator for you.

    Just replace '/' with ',' and you should be good to go.

    Cheers,

    Gregzo
     
  19. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Ok thanks man, I just tried that but got this error:

    ArgumentNullException: Argument cannot be null.
    Parameter name: path
    System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:205)
    System.IO.FileStream..ctor (System.String path, FileMode mode)
    (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
    SoundRecording.StartWriting (System.String fileName) (at Assets/SoundRecording.js:50)
    SoundRecording.Update () (at Assets/SoundRecording.js:36)

    For this line:

    Code (JavaScript):
    1. fileStream = new FileStream(filePath, FileMode.Create);
    I can't figure out what I've done wrong there exactly..

    Edit: That only happens when I hit the record button
     
  20. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    You're re-declaring filePath as a local variable in Awake(), ditch var.

    Once more, you will not get very far if you don't even know the basics of scripting.

    No one is expecting you to just know things, it's just the way most knowledge works: start with the basics. You'll run into error after error and keep posting about them if you don't, trust me.

    And since you're a beginner, do yourself a favor and learn C# instead of UnityScript. It's barely harder, but much more useful.

    Cheers,

    Gregzo
     
  21. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Hmmm, I tried ditching the var but got errors before even pressing play,

    This is the only version I tried that didn't give me errors until I pressed rec:

    Code (JavaScript):
    1. #pragma strict
    2. import System.IO; // for FileStream
    3. import System; // for BitConverter and Byte Type
    4. public var moveRecord : GUITexture;
    5. private var bufferSize : int;
    6. private var numBuffers : int;
    7. private var outputRate : int = 44100;
    8. private var fileName = String.Format("Music_File_{0}.wav", Guid.NewGuid().ToString());
    9. private var headerSize : int = 44; //default for uncompressed wav
    10. private var folderName = "Music Files";
    11. private var folderPath : String;
    12. private var filePath : String;
    13. private var recOutput : boolean;
    14. private var fileStream : FileStream;
    15. function Awake()
    16. {
    17.     if (!Directory.Exists(folderName))
    18.     Directory.CreateDirectory(folderName);
    19.     AudioSettings.outputSampleRate = outputRate;
    20.     var folderPath : String = Path.Combine(Application.persistentDataPath,folderName);
    21.     filePath = Path.Combine(folderPath,fileName);
    22. }
    and it returns this error:

    DirectoryNotFoundException: Could not find a part of the path "C:\Users\Brad\AppData\LocalLow\Games\MusicGame\Music Files\Music_File_537fdb4d-d4e7-4a1f-bdc1-8f058d67bc65.wav".
    System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/FileStream.cs:292)
    System.IO.FileStream..ctor (System.String path, FileMode mode)
    (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode)
    SoundRecording.StartWriting (System.String fileName) (at Assets/SoundRecording.js:50)
    SoundRecording.Update () (at Assets/SoundRecording.js:36)

    Also I couldn't agree more, I just don't know where to start, I went through all the tutorials on unity for c# I went on youtube and followed along with a bunch of tutorials, but they don't cover everything, especially not stuff like this, if you could point me in the right direction that would be awesome (maybe how you managed to learn?)

    Also yeah, I much prefer C#, I'm only using JS for this one script because your original code was in JS and I couldn't figure out how to translate it.

    PS thank you for all your help, I really do appreciate it all
     
  22. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    When you write var in UnityScript, you are declaring a variable.
    If the declaration is in the scope of a method, it's a local var, otherwise it's an instance variable. Never use the same name for both!

    Now, your error: DirectoryNotFoundException means that the directory wasn't... found!
    If it isn't found, it's that it doesn't exist.

    Check your code: why are you checking Directory.Exists on folderName? It should be folderPath, which you should build, of course, before checking if it point to an existing directory.

    So:
    1) Build folderPath with persistentDataPath and folderName
    2) Check if it exists and create if it doesn't
    3) Build filePath with folderPath and fileName
    4) Deep fry and enjoy

    Tutorials:

    I really like CatlikeCoding.
    Video tutorials for coding are pretty much useless: much better to read at your own pace.

    Cheers,

    Gregzo
     
  23. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Alright thanks, I'm trying to understand what you mean exactly, I just tried a few different ways and the following one is the only version that doesn't give me errors at all, not even when pressing record.

    However, I can't find where it saved the file to (if it ever did, and it's certainly not in the music files folder)

    Code (JavaScript):
    1. import System.IO; // for FileStream
    2. import System; // for BitConverter and Byte Type
    3. public var moveRecord : GUITexture;
    4. private var bufferSize : int;
    5. private var numBuffers : int;
    6. private var outputRate : int = 44100;
    7. private var fileName = String.Format("Music_File_{0}.wav", Guid.NewGuid().ToString());
    8. private var headerSize : int = 44; //default for uncompressed wav
    9. private var folderName = "Music Files";
    10. private var folderPath : String;
    11. private var filePath : String;
    12. private var recOutput : boolean;
    13. private var fileStream : FileStream;
    14. function Awake()
    15. {
    16.     AudioSettings.outputSampleRate = outputRate;
    17.     var folderPath : String = Path.Combine(Application.persistentDataPath,folderName);
    18.     if (!Directory.Exists(folderPath))
    19.     Directory.CreateDirectory(folderPath);
    20.     filePath = Path.Combine(folderPath,fileName);
    21. }
    22. function Start()
    23. {
    24.     AudioSettings.GetDSPBufferSize(bufferSize,numBuffers);
    25. }
    26. function Update()
    27. {
    28.     if(moveRecord.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
    29.     //if(Input.GetKeyDown("r"))
    30.     {
    31.     print("rec");
    32.         if(recOutput == false)
    33.         {
    34.             StartWriting(fileName);
    35.             recOutput = true;
    36.         }
    37.         else
    38.         {
    39.             recOutput = false;
    40.             WriteHeader();  
    41.             print("rec stop");
    42.         }
    43.     }
    44. }
    45. function StartWriting(fileName : String)
    46. {
    47.  
    48.     fileStream = new FileStream(filePath, FileMode.Create);
    49.     var emptyByte : byte = new byte();
    50.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    51.     {
    52.         fileStream.WriteByte(emptyByte);
    53.     }
    54. }
    Thanks for the tutorial site, I'll check them out! Btw, how did you learn file encoding like this?
     
  24. gregzo

    gregzo

    Joined:
    Dec 17, 2011
    Posts:
    795
    In Awake, you are declaring folderPath again ( keyword var ), this results in your folderPath field ( which you declare at the top of the file, outside of a method's scope ) to never be assigned... I'm surprised you're not getting a warning.

    Code (CSharp):
    1. //var folderPath : String = Path.Combine( Application.persistentDataPath, folderName ); No no no
    2. folderPath = Path.Combine( Application.persistentDataPath, folderName ); //yes indeed
     
  25. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Alright cheers man, I get what you meant now, but it's still not saving to the folder it makes.. I think it has to do with what I'm doing here in the update:

    Code (JavaScript):
    1. function Update()
    2. {
    3.     if(moveRecord.HitTest(Input.mousePosition) && Input.GetMouseButtonDown(0))
    4.     //if(Input.GetKeyDown("r"))
    5.     {
    6.     print("rec");
    7.         if(recOutput == false)
    8.         {
    9.             StartWriting(fileName);
    10.             recOutput = true;
    11.         }
    12.         else
    13.         {
    14.             recOutput = false;
    15.             WriteHeader();  
    16.             print("rec stop");
    17.         }
    18.     }
    19. }
    20. function StartWriting(fileName : String)
    21. {
    22.  
    23.     fileStream = new FileStream(filePath, FileMode.Create);
    24.     var emptyByte : byte = new byte();
    25.     for(var i : int = 0; i<headerSize; i++) //preparing the header
    26.     {
    27.         fileStream.WriteByte(emptyByte);
    28.     }
    29. }
    I have a feeling I'm not using the names correctly or I'm missing something..
     
  26. --Brad--

    --Brad--

    Joined:
    Jun 8, 2014
    Posts:
    15
    Hmm, actually it's not even making the folder, I just deleted the folder and it didn't make one, so I added another create.directory like this:

    Code (JavaScript):
    1. function Awake()
    2. {
    3.     if (!Directory.Exists(folderName))
    4.     Directory.CreateDirectory(folderName);
    5.     folderPath = Path.Combine( Application.persistentDataPath, folderName );
    6.     AudioSettings.outputSampleRate = outputRate;
    7.     if (!Directory.Exists(folderPath))
    8.     Directory.CreateDirectory(folderPath);
    9.     filePath = Path.Combine(folderPath,fileName);
    10. }
    and it makes the folder fine, but the file still doesn't go in there..