在ActionScript 3中将int / Number值输出为String

时间:2014-01-14 19:24:31

标签: actionscript-3 flash filereference

让我先说明我对ActionScript并不十分熟悉,所以请原谅我可能遗漏的任何看似明显的事情。

我当前有一个非常简单的功能,AS3应用程序将在使用FileReference对象单击按钮时输出文件,如下所示:

//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);
}

这里似乎发生了一个错误,当我尝试连接文件名时,导致文件根本没有输出,如上所示。但是,如果我用类似于以下的硬编码选项替换fileName变量,它可以正常工作:

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

理想情况下,我希望使用以下内容根据byteArray的长度派生文件的持续时间:

//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");

非常感谢任何想法。

2 个答案:

答案 0 :(得分:1)

除了错过m.toString()中的括号外,问题出在哪里?

在下载downloadBuffer之前,你是不是错过了.lenght?

答案 1 :(得分:1)

我终于能够找到一个可行的解决方案,需要显式输入所有变量(包括使用.toString()操作的单独变量),如下所示:

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);
}

奇怪的是,这些必须明确地存储在单独的值中,并且不能简单地在线使用,如其他示例中所示。