as3 android saving remote sounds on sdcard - accessing later? - android

This code is way cool! Try it. It creates a folder in your user called .007 and saves an mp3 called yahoo.mp3.
It will also do it on an android device if permissions are set correctly.
Once it creates a folder and saves the mp3 it will loop thru the folder and get the file path and the file name.
MY QUESTION IS: How do you access it and play the sound on Android? It works on the desktop as you can see if you test it But not on the droid. Any ideas why?
import flash.filesystem.*;
var urlString:String = "http://YourWebsite.com/YourSound.mp3";
var urlReq:URLRequest = new URLRequest(urlString);
var urlStream:URLStream = new URLStream();
var fileData:ByteArray = new ByteArray();
urlStream.addEventListener(Event.COMPLETE, loaded);
urlStream.load(urlReq);
function loaded(event:Event):void
{
urlStream.readBytes(fileData, 0, urlStream.bytesAvailable);
writeAirFile();
}
function writeAirFile():void
{
var file:File = File.userDirectory.resolvePath(".007/Yahoo.mp3");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
fileStream.writeBytes(fileData, 0, fileData.length);
fileStream.close();
trace("The file is written.");
var desktop:File = File.userDirectory.resolvePath(".0");
var files:Array = desktop.getDirectoryListing();
for (var i:uint = 0; i < files.length; i++)
{
trace(files[i].nativePath);// gets the path of the files
trace(files[i].name);// gets the name
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest(files[0].nativePath));
myChannel = mySound.play();
}
}

file:// <-------The Answer!
Got it! The file path needed to access the "sdcard" on a Android phone must begin with: file://
If you want to load a sound from your phone it would look like this. Here is the one important line needed. I will post full code below.
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));
Since I was using a loop above getting all the files in a particular folder this is the code that worked for me in the example above.
mySound.load(new URLRequest("file://"+files[0].nativePath));
FULL CODE TO LOAD AND PLAY SOUND ON DROID
var mySound:Sound = new Sound();
var myChannel:SoundChannel = new SoundChannel();
var lastPosition:Number = 0;
mySound.load(new URLRequest("file://mnt/sdcard/AnyFolder/YourSound.mp3"));
myChannel = mySound.play();
I worked very hard on to find this answer. I hope this helps many. Please remember to +1 if this code has helped you. :)
I love StackOverflow! :)

Related

How do I code in actionscript the exact file location for several swf I have included for my apk?

In Air for Android Setting's 'Include files' part, I added test1.swf and test2.swf to the main file automatically included. Would the following be correct then:
myLoader.load(new URLRequest("test1.swf")
Or, is there any other folder I should include? Would the following be correct?
myLoader.load(new URLRequest("\..\..test1.swf")
I'm not in a position to test this right now on Android, but I believe included files are put in the application directory, which you can access as follows:
var path:String = File.applicationDirectory.resolvePath("test1.swf").nativePath;
myLoader.load(new URLRequest(path));
You can also use the shorthand url of:
myLoader.load(new URLRequest("app://test1.swf"));
You could also load the swf with the FileStream class, which gives you more control over things. That would look like this:
var file:File = File.applicationDirectory.resolvePath("test1.swf");
if (file.exists) {
var swfData:ByteArray = new ByteArray();
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
stream.readBytes(swfData);
stream.close();
var myLoader:Loader = new Loader();
var loaderContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
myLoader.loadBytes(swfData, loaderContext);
addChild(myLoader);
}else {
trace("file doesn't exist");
}

as3 show loader while saving image to gallery

Am making an android app using AIR for Android and as3. Following is my code to save image into device gallery. The image gets saved but I want to show a loader while the image is getting saved. This code does not work approprietely. Please point out if I am making any mistake here.
mysaveBtn.addEventListener(MouseEvent.CLICK, ftnSave);
function ftnSave():void
{
mysaveBtn.removeEventListener(MouseEvent.CLICK, ftnSave);
mysaveBtn.visible = false;
var jpgSource:BitmapData = new BitmapData(1080, 1920);
jpgSource.draw(myTheme);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var imgByteData:ByteArray = jpgEncoder.encode(jpgSource);
var fs : FileStream = new FileStream();
var targetFile : File = File.desktopDirectory.resolvePath('/sdcard/DCIM/Camera/image.jpg');
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(imgByteData);
fs.close();
if( CameraRoll.supportsAddBitmapData ){
new CameraRoll().addBitmapData(jpgSource);
mysaveBtn.visible = true;
mysaveBtn.addEventListener(MouseEvent.CLICK, ftnSave);
}
}
You can use the class "Asynchronous Image Encoders"
This class can also save large images to prevent freezing your application .

How to save a jpg image to android phone with as3

I'm trying to save an image to my android phone is a specific directory.
Somehow I spent hours but couldn't get it to work. I hope someone can help me understand why my code isn't working or if there is another way to do it.
public function SaveTheImage(me:MouseEvent):void
{
ImageSaverBMD.draw(ImageHolder);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var jpgBytes:ByteArray = jpgEncoder.encode(ImageSaverBMD);
var myFile:File = File.documentsDirectory.resolvePath("/sdcard / DCIM / Camera/testingimage.jpg");
var fs:FileStream = new FileStream();
fs.open(myFile, FileMode.WRITE);
fs.writeBytes(jpgBytes, 0, jpgBytes.length);
fs.close();
}
There are many steps in your algorithm that could cause the bug, with the code you provided I could make a few guesses, but there is a better way.
First of all try this code:
var s:Shape = new Shape();
s.graphics.beginFill(0);
s.graphics.drawCircle( 20, 20, 20);
s.graphics.endFill();
var bd:BitmapData = new BitmapData(40, 40, false);
bd.draw(s);
var jpgEncoder:JPGEncoder = new JPGEncoder(100);
var bytes:ByteArray = jpgEncoder.encode(bd);
var f:File = File.applicationDirectory;
var fs:FileStream = new FileStream();
fs.open(f, FileMode.WRITE);
fs.writeBytes(bytes);
fs.close();
This should save a 80x80 pixels black circle in your provided directory. See if it works. If not: check permissions to have external storage.
If it works, start removing the code block-by-block from top to bottom:
Remove the top two blocks and put your ImageSaverBMD.draw(ImageHolder) line. Maybe the problem is with that class? It could be that the bitmap data size was wrong or the matrix for drawing had incorrect translation.
The JPGEncoder is probably OK, it's a common used framework, but sometimes when I copy it into my source files I need to change the package in the .as file. Is your package correct?
Your URL in the File object has spaces, could that be your problem, try using a simple URL first and see if that works. It could be the problem that you try to navigate to an absolute URL with a relative URL pattern (I could be wrong on this, usually I have different approach of navigating to a folder).
Hope that helps!
private function export():void
{
var today_date:Date = new Date();
var thismonth:uint = today_date.getMonth();
var today_time;
var currentTime:Date = new Date();
var minutes = currentTime.getMinutes();
var seconds = currentTime.getSeconds();
var hours = currentTime.getHours() * 30 + currentTime.getMinutes() / 2;
var mnth:Array = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var fileName:String = (today_date.getDate()+mnth[thismonth]+today_date.getFullYear()+"_"+currentTime.hours + currentTime.minutes + currentTime.seconds+".png");
//trace(fileName);// displays current date in United States date format
var bmd:BitmapData = new BitmapData(board.width, board.height);//(600, 290);
bmd.draw(board);
var ba:ByteArray = PNGEncoder.encode(bmd);
// var file:File = File.applicationDirectory;
//var file:FileReference = new FileReference();
var fs : FileStream = new FileStream();
var targetFile : File = File.documentsDirectory.resolvePath(fileName);
//var targetFile : File = File.applicationDirectory.resolvePath(fileName);
fs.open(targetFile, FileMode.WRITE);
fs.writeBytes(ba);
fs.close();
//file.addEventListener(Event.COMPLETE, saveSuccessful);
saveDialog = new SaveDialog();
addChild(saveDialog);
test = setInterval(showMessage,2000);
//var test = setInterval(showMessage,3000);
saveDialog.closeBtn.addEventListener(MouseEvent.MOUSE_UP, closeSaveDialog);
//file.save(ba, fileName);
}

how to extract obb file on flash builder?

var zip:FZip = new FZip();
zip.load(new URLRequest("xx.obb"));
this codes does not work. It throws "Unknown record signature:" error.
how did you extract .obb file ?
that way you need to read it? if it is to load a game I think it is a good way to load it so it would be better to load it from a specific route
for example: sdcard/android/obb/filename.obb
If your obb file is like any other zip file then you can treat it as such. First you will want a list of files to extract (could be all your files who knows).
Here is one way to get the files out :
private function obbFileUnpackingRoutine(event:Event):void{
addChild(obbUnpackScreen);
obbUnpackScreen.obbProgress.scaleX = zipCount/filesToUnpack.length;
setChildIndex(obbUnpackScreen, numChildren -1);
for (var i:uint = 0; i<24; i++){
if (zipCount == filesToUnpack.length){
zipDone = true;
removeEventListener(Event.ENTER_FRAME, obbFileUnpackingRoutine);
removeChild(obbUnpackScreen);
var fr:FileStream=new FileStream();
var str:String = File.applicationStorageDirectory.nativePath;
var cacheDir= new File(str +"/YourSettings");
fr.open(cacheDir.resolvePath("isObbUnpacked.pnr"),FileMode.WRITE);
fr.writeObject([zip.getFileCount(),zipCount]);
fr.close();
return
}
var file:FZipFile = zip.getFileByName(filesToUnpack[zipCount])
var cacheDir:File= null;
var str:String = File.applicationStorageDirectory.nativePath;
cacheDir= new File(str);
var fileContents = file.content
var fr:FileStream=new FileStream();
fr.open(cacheDir.resolvePath(file.filename),FileMode.WRITE);
fr.writeBytes(fileContents);
fr.close();
zipCount++;
}
}
If memory permits you may want to load your files directly from the zip file so you don't have to unpack them.
Any file that can only accessed with a urlRequest will of course have to be extracted
But you can load png files, mp3 files, and your own custom files with byteArrays
My obb file has thousands of tiny mp3 files and it can take upto 3 minutes to extract on an allWinner A10 processor.
Direct loading means it can get the files instantly. And there is almost no delay at all.

AIR 3 Download mp3

I want to download mp3 file, on mobile device iOs or android, because i dont want to download every time from server.
I write simple code, but not work correctly. File size is too big, because is not encode and after save I cant play it again.
Please help and sorry for bad English.
private var sound:Sound;
public function loadSound(tmpUrl:String):void
{
sound = new Sound();
sound.addEventListener(Event.COMPLETE,onLoadSuccessSound, false, 0, true);
sound.load(new URLRequest(tmpUrl));
}
protected function onLoadSuccessSound(event:Event):void
{
var fileData:ByteArray = new ByteArray();
sound.extract(fileData, sound.bytesTotal);
var file:File = new File(File.applicationStorageDirectory + "\\ppp.mp3");
var fs:FileStream = new FileStream();
fs.open(file, FileMode.WRITE);
fs.writeBytes(fileData,0,fileData.length);
fs.close();
}
Basically answer is to use URLStream and FileStream.
Question is pretty widespread and covered, for example, here Download a file with Adobe AIR and AS3: URLStream saving files to desktop?

Categories

Resources