Reputation: 13
I'm developing an android app using adobe flash cs5.5 AIR. I have a simple sound button that playing a sound clip. I want to save this music1.mp3
file into phone or tab SD card memory using Button called save. I don't know how to do that. Can anyone have answer..?
This is a example script that im using:
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; }
Upvotes: 0
Views: 954
Reputation: 461
You can save to an andriod sd card in adobe AIR through the File class.
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();
where ba is a ByteArray containing the music data. If you are not sure how to do that I include it below.
Using the extract function in the Sound class we can extract a sound file to a byteArray. Check out the link for the documentation:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/media/Sound.html#extract()
ByteArray ba;
sound.extract(ba, 4096);
You can then compress this byteArray using different compression algorithms (it may save some space since you are going for mobile), but if you do so, you will have to decompress them in your code when you want to play them.
Upvotes: 0
Reputation: 8159
Use the File
and FileStream
classes. File
represents a file or directory on the system and is available in AIR only. FileStream
allows you to open a connection with that file, create it, write to it, read it, and delete it.
So something like this would work.
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
}
That's untested, so it may require a few tweaks but that is the general gist of how you would do this. I'm not entirely sure Sound#extract()
writes MP3 data to the ByteArray. I was always under the impression that AS3 stuck to uncompressed WAV data when writing audio data, but I could be wrong.
You also need to make sure this permission is in the Android Manifest section of your app.xml.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
As always, please read the documentation. Adobe's AS3, AIR, and Flex <= 4.6 documentation is among the best I have found for any language. It is truly helpful.
Upvotes: 2