Papa De Beau
Papa De Beau

Reputation: 3828

as3 android saving remote sounds on sdcard - accessing later?

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();

    }


}

Upvotes: 0

Views: 3308

Answers (1)

Papa De Beau
Papa De Beau

Reputation: 3828

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! :)

Upvotes: 3

Related Questions