content/hifi-content/rebecca/Today/jessicaAudioApp.js
2022-02-14 02:04:11 +01:00

96 lines
3.1 KiB
JavaScript

//
// jessicaAudioApp.js
//
// Created by Rebecca Stankus on 06/26/18
// Copyright 2018 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
(function() {
var TABLET_BUTTON_IMAGE = Script.resolvePath('svg/musicNotes-i.svg');
var TABLET_BUTTON_PRESSED = Script.resolvePath('svg/musicNotes-a.svg');
var CLIP_1 = SoundCache.getSound("http://hifi-content.s3-us-west-1.amazonaws.com/DomainContent/Hub-staging/radio/reggae-ska-playlist-1.wav");
var ONE_HUNDRED = 100;
var SEARCH_RADIUS = 100;
var audioVolume = 0.7;
var tablet = Tablet.getTablet('com.highfidelity.interface.tablet.system');
var appPage = Script.resolvePath('jessicaAudioApp.html?004');
var button = tablet.addButton({
text: 'REGGAE',
icon: TABLET_BUTTON_IMAGE,
activeIcon: TABLET_BUTTON_PRESSED
});
var open = false;
var injector;
var soundPosition;
function onClicked() {
if (open) {
tablet.gotoHomeScreen();
} else {
tablet.gotoWebScreen(appPage);
}
}
function onWebEventReceived(event) {
if (typeof event === 'string') {
event = JSON.parse(event);
if (event.type === 'audio1') {
playSound(CLIP_1);
} else if (event.type === 'stop') {
if (injector) {
injector.stop();
}
} else if (event.type === 'volumeSlider') {
if (injector) {
injector.setOptions({
volume: event.volume / ONE_HUNDRED,
position: soundPosition
});
}
}
}
}
function onScreenChanged(type, url) {
open = (url === appPage);
button.editProperties({isActive: open});
}
function appEnding() {
if (injector) {
injector.stop();
}
button.clicked.disconnect(onClicked);
tablet.removeButton(button);
tablet.screenChanged.disconnect(onScreenChanged);
tablet.webEventReceived.disconnect(onWebEventReceived);
}
function playSound(sound) {
Entities.findEntities(MyAvatar.position, SEARCH_RADIUS).forEach(function(element) {
var properties = Entities.getEntityProperties(element, ['name', 'position']);
if ((properties.name === "Amphitheater")) {
soundPosition = properties.position;
}
});
if (sound.downloaded) {
if (injector) {
injector.stop();
}
injector = Audio.playSound(sound, {
position: soundPosition,
volume: audioVolume
});
}
}
button.clicked.connect(onClicked);
tablet.screenChanged.connect(onScreenChanged);
tablet.webEventReceived.connect(onWebEventReceived);
Script.scriptEnding.connect(appEnding);
}());