【问题标题】:Flash as3 Air save this mp3 file in to mobile phone/tab sd card memoryFlash as3 Air 将此 mp3 文件保存到手机/tab sd 卡内存中
【发布时间】:2014-04-08 14:45:20
【问题描述】:

我正在使用 adobe flash cs5.5 AIR 开发一个 android 应用程序。我有一个播放声音剪辑的简单声音按钮。我想使用名为保存的按钮将这个 music1.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; }

【问题讨论】:

    标签: android actionscript-3 flash air


    【解决方案1】:

    使用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

    【讨论】:

    • 啊,是的。我忘记在清单中包含权限。干得好。
    • 如果您在完全加载之前不需要播放声音,那么您可以在BINARY 模式下使用URLLoader 加载它。完成后,您可以保存生成的ByteArray,它应该代表您的 MP3。然后,您可以在新的 Sound 对象上使用 loadCompressedDataFromByteArray() 在您的 AIR 应用程序中播放它。 extract() 将(据我所知)提取原始的、未压缩的声音,因此它的文件大小将比您的 MP3 更大
    • @divillysausages 是的,这是真的。我想提出这个建议,但我想我会让答案依赖于提问者已经列出的内容。不过,您最后关于extract() 的声明肯定是使用URLLoader 的理由。我什至没有考虑使用它来节省存储空间。好电话
    【解决方案2】:

    您可以通过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()

    【讨论】:

      猜你喜欢
      • 2012-06-16
      • 2014-03-13
      • 2011-06-08
      • 2015-06-15
      • 2012-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-25
      相关资源
      最近更新 更多