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