2014-04-08 69 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'的原因。我甚至沒有考慮過使用它來節省存儲空間。好決定 –