Edge Animate & PhoneGap Build: part 2

In a previous post I wrote on how to convert the audio playback that Edge Animate uses into some that PhoneGap is able to use.

It that post I had this function:

function getPhoneGapPath() {
   var path = window.location.pathname;
   path = path.substr( path, path.length - 10 );
   return 'file://' + path;
};

There a first a slight error that I introduced. When finding the path name, I stripped out 10 characters. This was based on the test case being based on index.html. So if you happen to bring this function into another page with a different file name it would break. Rookie mistake. Actually more like too much prototyping and not enough ‘real’ coding…

Also we actually don’t need the entire path, so based on the platform we can locate our ‘root’ directory and go from there. Here is a revised version of the function:

function getPhoneGapPath() {
  var devicePlatform = device.platform;
  if (devicePlatform === "iOS") {
    path = "";
  } else if (devicePlatform === "Android" || devicePlatform === 'android') {
    path = "/android_asset/www/";
  }
return path;
};

Now two things to note here. 1) I am ignoring other platforms, so if you need to support one of the other platforms that PhoneGap does support, add in the proper if clause. 2) Every time this function is called, I query the device.platform. Now using your phone is actually a Transformer, this value is not going to change. I would recommend making this query once, say upon deviceReady() and storing the value in a variable and referencing it within this function. Also note that you will need to make sure you also include the device plugin in your PhoneGap app as well.

 

Advertisements

2 comments

  1. Why not do this?

    function getPhoneGapPath() {
    var devicePlatform = device.platform.toLowerCase();
    if (devicePlatform === “ios”) {
    path = “”;
    } else if (devicePlatform === “android” ) {
    path = “/android_asset/www/”;
    }
    return path;
    };

    this way you have normalized the text and no longer need to use the OR in your if statement.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s