1

Let me preface this by stating that I am not terribly familiar with ActionScript, so forgive any seemingly obvious things that I may be missing.

I current have a very simple function with an AS3 application that will output a file when a button is clicked using a FileReference object as seen below :

//Example download event
public function download(event:MouseEvent):void
{
      //Build a simple file to store the current file
      var outputFile:FileReference = new FileReference();

      //Perform a function to build a .wav file from the existing file
      //this returns a ByteArray (buffer)
      downloadBuffer = PrepareAudioFile();

      //Attempt to build the filename (using the length of bytes as the file name)
      var fileName:String = downloadBuffer.length.toString() + ".wav";

      //Save the file   
      audioFile.save(downloadBuffer, fileName);
}

There appears to be an error occurring somewhere within here that is resulting in the File not being outputted at all when I attempt to concatenate the file name as seen above. However, if I replace the fileName variable with a hard-coded option similar to the following, it works just fine :

audioFile.save(downloadBuffer, "Audio.wav");

Ideally, I would love to derive the duration of the file based on the length of the byteArray using the following :

//Get the duration (in seconds) as it is an audio file encoded in 44.1k
var durationInSeconds:Number = downloadBuffer.length / 44100;

//Grab the minutes and seconds
var m:Number = Math.floor(durationInSeconds / 60);
var s:Number = Math.floor(durationInSeconds % 60);

//Create the file name using those values
audioFile.save(downloadBuffer, m.toString() + "_" + s.toString() + ".wav");

Any ideas would be greatly appreciated.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • If this is the actual code you're using I can't see why it wouldn't work. Some characters are not allowed (/\:*?"<>|%@) but your filename should be just fine... – imcg Jan 14 '14 at 21:29

2 Answers2

1

Where is the problem other than missing the parentheses in m.toString()?

Aren't you missing a .lenght before the division of downloadBuffer as well?

Fygo
  • 4,555
  • 6
  • 33
  • 47
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Sinkingpoint Jan 14 '14 at 20:22
  • Sorry Fygo, I neglected to add those two missing sections in the example however they are both included in the actual code itself. I have since updated the post. – Rion Williams Jan 14 '14 at 20:34
  • @Quirliom I always find it funny when somebody explains my words in her own manner. There was not a slightest intention of critique in my answer - by writing 'where is the problem other than...' I am assuming the compiler must throw something or that the author sees the output in some manner - I am just asking what it shows/says. Thanks for voting to delete that post. That will SURE help the author. – Fygo Jan 14 '14 at 20:38
  • @RionWilliams Np. What does the tracing of the final string show you? trace(m.toString() + "_" + s.toString() + ".wav"); – Fygo Jan 14 '14 at 20:41
  • I'll add a trace into there and check it out. The application itself has heavy use of ExternalInterfaces for access through Javascript so I typically don't get much in the way of actual error messages or output. Is there a way that I am possible unaware of for doing this using a hosted .swf file? – Rion Williams Jan 14 '14 at 20:43
  • Hm, yes, that may be a problem. You should always check whether the ExtInt is .available and then you try/catch to call it. If the swf is too reliant on the ExtInt (=you cannot get to the "saving" point without it), call console.log('+finalString+') from within ExternalInterface.call(); – Fygo Jan 14 '14 at 20:45
1

I was finally able to come up with a viable solution that required explicit typing of all of the variables (including using a separate variable for the .toString() operations) as seen below :

public function download(event:MouseEvent):void
{
      //Build a simple file to store the current file
      var outputFile:FileReference = new FileReference();

      //Perform a function to build a .wav file from the existing file
      //this returns a ByteArray (buffer)
      downloadBuffer = PrepareAudioFile();

      //When accessing the actual length, this needed to be performed separately (and strongly typed)
      var bufferLength:uint = downloadBuffer.length;

      //The string process also needed to be stored in a separate variable
      var stringLength:String = bufferLength.toString();

      //Use the variables to properly concatenate a file name
      var fileName:String = dstringLength + ".wav";

      //Save the file   
      audioFile.save(downloadBuffer, fileName);
}

It's bizarre that these had to explicitly be stored within separate values and couldn't simply be used in-line as demonstrated in the other examples.

Rion Williams
  • 74,820
  • 37
  • 200
  • 327