使用File
和FileStream
类。 File
表示系统上的文件或目录,仅在AIR中可用。 FileStream
允许您打开与该文件的连接,创建,写入,读取并删除它。
所以像这样的东西会工作。
var s:Sound = new Sound();
s.addEventListener(Event.COMPLETE, completeHandler); // not entirely sure this is the correct event, but it should be. I haven't played with the Sound class in a while
s.load(new URLRequest("sound/music1.mp3"));
function completeHandler(e:Event):void {
var fs:FileStream = new FileStream();
var f:File = File.applicationStorageDirectory.resolvePath("music1.mp3"); // selects file in the sandboxed dir for your app
var bytes = new ByteArray();
s.extract(bytes, 4096); // load file into ByteArray. Unsure length argument is correct, may need tweaking
fs.open(f, FileMode.WRITE); // opens stream to file, sets mode to WRITE which will create and truncate the file
fs.writeBytes(bytes); // write ByteArray to file
fs.close(); // closes link to file. ALWAYS make sure you do this. Failing to do so can have consequences
}
这是未经测试,因此它可能需要一些调整,但是这是你将如何做到这一点的一般要点。我不完全确定Sound#extract()
将MP3数据写入ByteArray。我一直认为AS3在写入音频数据时卡住了未压缩的WAV数据,但我可能是错的。
您还需要确保此权限位于app.xml的Android Manifest部分。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
与往常一样,请阅读文档。 Adobe的AS3,AIR和Flex < = 4.6文档是我发现的最好的语言之一。这真的很有帮助。
啊,是的。我忘了在清单中包含权限。好工作。 – Travis
如果在完全加载之前不需要播放声音,则可以在'BINARY'模式下使用'URLLoader'加载它。完成后,您可以保存生成的“ByteArray”,它应该是MP3的表示。然后,您可以在新的'Sound'对象上使用'loadCompressedDataFromByteArray()'在AIR应用程序中播放它。 'extract()'会(根据我所知道的)提取原始的未压缩声音,所以它会比MP3更大的文件大小。 – divillysausages
@divillysausages是的,那是真的。我想提出这个建议,但我认为我会留下答案,依靠提问者已经布置的内容。不过,您最后一次关于'extract()'的说法肯定会成为使用'URLLoader'的原因。我甚至没有考虑过使用它来节省存储空间。好决定 –