Skip to content

Instantly share code, notes, and snippets.

@benvium
Last active December 16, 2015 02:39
Show Gist options
  • Select an option

  • Save benvium/5363867 to your computer and use it in GitHub Desktop.

Select an option

Save benvium/5363867 to your computer and use it in GitHub Desktop.
// GPS Audio Player Example v1
init();
/////////////////////////////////////////////////////////////////////////////////////
// Audio Channel Setup
/////////////////////////////////////////////////////////////////////////////////////
// When app starts, create two channels and setup 'watches' (so we know what the channels are doing)
function init() {
af.audioChannel.init(2);
af.audioChannel.watchStatus(0, updateUI); // Channel 0 is our 'story' channel
af.audioChannel.watchStatus(1, statusUpdated); // Channel 1 is our 'atmosphere' channel
// Update UI now
updateUI();
}
// Called when the story channel updates.
// If it's playing a story, show the name on screen
// and change the size of the 'progress' bar to show how much has been played.
function updateUI(url, elapsed, amount, paused) {
if (url) {
ui.title.text(url);
ui.progress.width( amount*280 );
ui.progress.hidden(false);
} else {
ui.progress.hidden(true);
ui.title.text("(nothing playing)");
}
}
// Called when the atmosphere channel updates. Prints out log information.
function statusUpdated(url, elapsed, amount, paused) {
if (url) {
log("Playing " + url + " " + Math.floor(amount * 100) + "%");
}
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions used by zones in the Location tab
/////////////////////////////////////////////////////////////////////////////////////
// For each story zone in the Location tab, make sure Move In / Function is set to enterStory
function enterStory() {
var a = audioFromFeature(this);
log("add story " + a);
af.audioChannel.add(0, a);
}
// For each story zone in the Location tab, make sure Move In / Function is set to leaveStory
function leaveStory() {
clearIfNeeded(0, this);
}
// For each atmosphere zone in the Location tab, make sure Move In / Function is set to enterAtmosphere.
function enterAtmosphere() {
var a = audioFromFeature(this);
log("add atmosphere " + a);
af.audioChannel.add(1, a, {loop:true});
}
// For each atmosphere zone in the Location tab, make sure Move Out / Function is set to leaveAtmosphere.
function leaveAtmosphere() {
clearIfNeeded(1, this);
}
/////////////////////////////////////////////////////////////////////////////////////
// Functions used by buttons in the Layout tab
/////////////////////////////////////////////////////////////////////////////////////
// Make sure your play button has code name ui.play, and tapped function set to play
function play() {
ui.play.hidden(true);
ui.pause.hidden(false);
af.audioChannel.resume(0);
af.audioChannel.resume(1);
}
// Make sure your pause button has code name ui.pause, and tapped function set to pause
function pause() {
ui.play.hidden(false);
ui.pause.hidden(true);
af.audioChannel.pause(0);
af.audioChannel.pause(1);
}
/////////////////////////////////////////////////////////////////////////////////////
// Utility functions
/////////////////////////////////////////////////////////////////////////////////////
// A handy function to work out the name of the mp3 file to play from the name of the zone.
function audioFromFeature(feature) {
return "audio/" + feature.getName() + ".mp3";
}
// If you have overlapping zones, it's possible leaving a zone will stop audio started by another zone
// This function helps by only clearing if the zone's audio is still playing.
// Perhaps a future AppFurnace version will make this simpler..
function clearIfNeeded(channel, feature) {
af.audioChannel.getStatus(channel,
function(url, elapsed, amount, isPaused) {
if (url === audioFromFeature(feature)) {
log("clearing " + channel);
af.audioChannel.clear(channel);
} else {
log("not clearing " + channel);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment