2014-04-08 73 views
0

我正在开发一个使用Adobe Flash CS5.5 AIR的Android应用程序。我有一个播放声音剪辑的简单声音按钮。我想使用保存按钮将此music1.mp3文件保存到手机或标签SD卡内存中。我不知道该怎么做。任何人都可以回答..?Flash as3 Air将此mp3文件保存到手机/标签页SD卡内存

这是即时通讯使用的示例脚本:

musicbtn.addEventListener(MouseEvent.CLICK, fl_ClickToPlayStopSound); 

var fl_SC:SoundChannel; 

var fl_ToPlay:Boolean = true; 

function fl_ClickToPlayStopSound(evt:MouseEvent):void 

{ 
if (fl_ToPlay) 

{ 
    var s:Sound = new Sound(new URLRequest("sound/music1.mp3")); 
    fl_SC = s.play(); 
} 
else 
{ 
    fl_SC.stop(); 
} 
fl_ToPlay = !fl_ToPlay; } 

回答

0

您可以保存通过File类在Adobe AIR的Andriod的SD卡。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/filesystem/File.html

var soundfile:File = File.applicationStorageDirectory.resolvePath("music1.mp3"); 
var fstream:FileStream = new FileStream(); 
fstream.open(soundfile, FileMode.WRITE); 
fstream.writeBytes(ba, 0, ba.length); 
fstream.close(); 

其中BA是包含音乐数据的ByteArray。如果你不确定如何做到这一点,我将它包含在下面。


使用Sound类中的提取函数,我们可以将一个声音文件提取到一个byteArray。退房的链接的文档:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html#extract()

ByteArray ba; 
sound.extract(ba, 4096); 

然后,您可以压缩使用不同的压缩算法(它可以节省一些空间,因为你要移动)此ByteArray,但如果你这样做,当你想播放它们时,你将不得不在你的代码中解压缩它们。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/ByteArray.html#compress()

2

使用FileFileStream类。 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文档是我发现的最好的语言之一。这真的很有帮助。

+0

啊,是的。我忘了在清单中包含权限。好工作。 – Travis

+1

如果在完全加载之前不需要播放声音,则可以在'BINARY'模式下使用'URLLoader'加载它。完成后,您可以保存生成的“ByteArray”,它应该是MP3的表示。然后,您可以在新的'Sound'对象上使用'loadCompressedDataFromByteArray()'在AIR应用程序中播放它。 'extract()'会(根据我所知道的)提取原始的未压缩声音,所以它会比MP3更大的文件大小。 – divillysausages

+0

@divillysausages是的,那是真的。我想提出这个建议,但我认为我会留下答案,依靠提问者已经布置的内容。不过,您最后一次关于'extract()'的说法肯定会成为使用'URLLoader'的原因。我甚至没有考虑过使用它来节省存储空间。好决定 –