Here is the code
index.js
var canvas, frame, zimStage, anim_container, dom_overlay_container, fnStartAnimation;
var imagePath_Str;
var asset_arr;
var jsVars = null;
var totalLib_Int = 0;
var libLoaded_Int = 0;
var libId_arr = ["74D00A8DE4EC4F76AEA9E5C999BBCAED"];
var alib; var blib; var uilib;
var libName_Arr = ["uilib"];
function displayMessage(evt)
{
console.log("I got " + evt.data + " from " + evt.origin);
if (evt.data.split)
{
var arr = evt.data.split(",");
window.loadFileToAudioPlayer2(arr[0], arr[1]);
}
}
if (window.addEventListener)
{
window.addEventListener("message", displayMessage, false);
}
else
{
window.attachEvent("onmessage", displayMessage);
}
function init()
{
totalLib_Int = libId_arr.length;
loadLib(libId_arr[libLoaded_Int]);
}
function loadLib(compId, s)
{
var comp = AdobeAn.getComposition(compId);
var ldr = new createjs.LoadQueue(false);
var templib = comp.getLibrary();
window[libName_Arr[libLoaded_Int]] = templib;
ldr.addEventListener("fileload", function(evt) { handleFileLoad(evt, comp) });
ldr.addEventListener("complete", function(evt) { handleComplete(evt, comp, templib) });
ldr.loadManifest(templib.properties.manifest);
}
function handleFileLoad(evt, comp)
{
var images = comp.getImages();
if (evt && (evt.item.type == "image")) {
images[evt.item.id] = evt.result;
}
}
function handleComplete(evt, comp, lib)
{
var ss = comp.getSpriteSheet();
var queue = evt.target;
var ssMetadata = lib.ssMetadata;
for (i = 0; i < ssMetadata.length; i++) {
ss[ssMetadata[i].name] = new createjs.SpriteSheet({
"images": [queue.getResult(ssMetadata[i].name)],
"frames": ssMetadata[i].frames
});
}
libLoaded_Int += 1;
if (libLoaded_Int < totalLib_Int)
{
loadLib(libId_arr[libLoaded_Int], "x");
}
if (libLoaded_Int == totalLib_Int)
{
misc.makeResponsive(true, 'both', true, 1, window[libName_Arr[0]]);
var zon = true;
var zns = false;
var ap = "./audio/";
var ip = "./assets/images/";
var fp = "./assets/fonts/";
var assets =
[
{ font: "CourierNewPS-BoldMT", src: fp + "CourierNewPS-BoldMT.ttf" },
ap + "ballad_of_legend_city.mp3",
ap + "goodbye_my_lc_baby.mp3",
];
var frame = new Frame(FIT, 370, 90, "#ffffff", "#ffffff", ready, "image.png", "assets/", new Waiter());
createjs.Sound.registerPlugins([createjs.HTMLAudioPlugin]);
function ready()
{
zimStage = frame.stage;
var audioPlayer = new AudioPlayer(frame, uilib, ap);
zimStage.addChild(audioPlayer.getView());
window.loadFileToAudioPlayer2 = function(fileName_str, songTitle_str) {
audioPlayer.loadFile(fileName_str, songTitle_str);
};
audioPlayer.loadFile("ballad_of_legend_city", "test test test test");
zimStage.update();
}
}
}
AudioPlayer-class.js
class AudioPlayer {
constructor(frame, uilib, ap) {
this.frame = frame;
this.zimstage = frame.stage;
this.ap = ap;
this.playing_Bool = false;
this.song = null;
this.currentAudioName_Str = "";
this.currentAudioTitle_Str = "";
this.defaultStatus = "Click any audio below to play the music.";
this.bars_Zcnt = new zim.Container();
this.soundWave = null;
this._tickerListener = null;
var view_Mc = new uilib.view_Mc();
this.view_Mc = view_Mc;
this.screen_Mc = view_Mc.screen_Mc;
this.playBtn_Mc = view_Mc.playBtn_Mc;
this.playBtn_Mc.name = "playBtn_Mc";
this.replayBtn_Mc = view_Mc.replayBtn_Mc;
this.replayBtn_Mc.name = "replayBtn_Mc";
this.stopBtn_Mc = view_Mc.stopBtn_Mc;
this.stopBtn_Mc.name = "stopBtn_Mc";
this.title_Tf = view_Mc.title_Tf;
this.status_Tf = view_Mc.status_Tf;
this.time_Tf = view_Mc.time_Tf;
this.status_Tf.text = this.defaultStatus;
this._bindEvents();
}
_bindEvents() {
/*
this.playBtn_Mc.on('mousedown', () => {
zog("play button mousedown");
this._playBtn_mousedown()
}
);
this.playBtn_Mc.on('pressup', () => {
zog("play button pressup");
this._playBtn_pressup()
}
);
this.replayBtn_Mc.on('mousedown', () => this._replayBtn_mousedown());
this.replayBtn_Mc.on('pressup', () => this._replayBtn_pressup());
this.stopBtn_Mc.on('mousedown', () => this._stopBtn_mousedown());
this.stopBtn_Mc.on('pressup', () => this._stopBtn_pressup());
*/
this.zimstage.on("stagemouseup", ()=>
{
if (!this.validate()) {
return;
}
const t = this.zimstage.getObjectUnderPoint(this.zimstage.mouseX, this.zimstage.mouseY, 1);
//zog("stage mouse up" + t.parent);
if(t.parent && t.parent.name == "playBtn_Mc") {
this._playBtn_pressup();
}
if(t.parent && t.parent.name == "replayBtn_Mc") {
this._replayBtn_pressup();
}
if(t.parent && t.parent.name == "stopBtn_Mc") {
this._stopBtn_pressup();
}
}
);
this.zimstage.on("stagemousedown", ()=>
{
if (!this.validate()) {
return;
}
const t = this.zimstage.getObjectUnderPoint(this.zimstage.mouseX, this.zimstage.mouseY, 1);
//zog("stage mouse down" + t.parent);
if(t.parent && t.parent.name == "playBtn_Mc") {
this._playBtn_mousedown();
}
if(t.parent && t.parent.name == "replayBtn_Mc") {
this._replayBtn_mousedown();
}
if(t.parent && t.parent.name == "stopBtn_Mc") {
this._stopBtn_mousedown();
}
}
);
}
loadFile(pfileName_str, psongTitle_str)
{
this.fileName_Str = pfileName_str;
this.songTitle_Str = psongTitle_str;
this.cleanup();
if (!this.validate()) {
return;
}
this.currentAudioTitle_Str = this.songTitle_Str || this.fileName_Str;
this.playBtn_Mc.gotoAndStop(0);
this.currentAudioName_Str = this.fileName_Str + '.mp3';
this._audInstance = new Aud(this.ap + this.currentAudioName_Str);
this._audReady = false;
this._audInstance.on("ready", () => {
this._audReady = true;
this.playBtnMouseDown_Bool = true;
this._playBtn_pressup();
});
// If already ready (preloaded), trigger manually
if (this._audInstance.ready) {
this._audReady = true;
this.playBtnMouseDown_Bool = true;
_playBtn_pressup();
}
}
validate() {
// Validation: check if fileName_str is valid
if (!this.fileName_Str || typeof this.fileName_Str !== 'string' || this.fileName_Str.trim() === '') {
this.status_Tf.text = "Invalid audio file.";
return false;
}
return true;
}
cleanup() {
if(this.bars_Zcnt) {
if(this.view_Mc.contains(this.bars_Zcnt)) {
this.view_Mc.removeChild(this.bars_Zcnt);
}
while (this.bars_Zcnt.numChildren > 0) {
this.bars_Zcnt.removeChildAt(0);
}
}
// Unload previous audio, soundWave, and ticker if exists
if (this.song) {
try {
if (typeof this.song.stop === 'function') this.song.stop();
this.song = null;
} catch (e) {
console.error('Error while unloading previous song:', e);
}
}
this.dispose_sound_wave();
if (this._tickerListener) {
zim.Ticker.remove(this._tickerListener);
this._tickerListener = null;
}
}
dispose_sound_wave() {
if (this.soundWave && typeof this.soundWave.dispose === 'function') {
try { this.soundWave.dispose(); } catch (e) { console.error('Error disposing soundWave:', e); }
this.soundWave = null;
}
}
_playBtn_mousedown()
{
if (!this._audReady) {
this.status_Tf.text = "Loading audio wait...";
this.zimstage.update();
return;
}
this.playBtnMouseDown_Bool = true;
this.playBtn_Mc.gotoAndStop(this.playBtn_Mc.currentFrame + 1);
this.zimstage.update();
}
_playBtn_pressup() {
if(this.playBtnMouseDown_Bool == false) return;
this.playBtnMouseDown_Bool = false;
if(this.song==null)
{
this.song = this._audInstance.play();
this.playBtn_Mc.gotoAndStop(2);
this.status_Tf.text = "Playing";
this.title_Tf.text = this.currentAudioTitle_Str;
}
else
//if (this.playBtn_Mc.currentFrame == 3 || this.playBtn_Mc.currentFrame == 2)
if(this.song.paused === false)
{
this.playBtn_Mc.gotoAndStop(0);
this.status_Tf.text = "Paused";
if (this.song) {
this.song.paused = true;
}
this.zimstage.update();
} else
if(this.song.paused === true)
//if (this.playBtn_Mc.currentFrame == 0 || this.playBtn_Mc.currentFrame == 1)
{
this.playBtn_Mc.gotoAndStop(2);
this.status_Tf.text = "Playing";
this.title_Tf.text = this.currentAudioTitle_Str;
this.zimstage.update();
this.song.paused = false;
}
try {
//this.dispose_sound_wave();
this.soundWave = new zim.SoundWave(50, this.song);
this.soundWave.on("ready", () => this._soundWaveReady());
} catch (e) {
console.error('Failed to create soundWave:', e);
}
}
_replayBtn_mousedown()
{
this.replayBtn_Mc.gotoAndStop(this.replayBtn_Mc.currentFrame + 1);
this.zimstage.update();
}
_replayBtn_pressup()
{
this.replayBtn_Mc.gotoAndStop(0);
this.playBtn_Mc.gotoAndStop(0);
this.status_Tf.text = "Playing";
this.song.position = 0;
this.song.paused = true;
this.playBtnMouseDown_Bool = true;
this._playBtn_pressup();
}
_stopBtn_mousedown() {
this.stopBtn_Mc.gotoAndStop(this.stopBtn_Mc.currentFrame + 1);
this.zimstage.update();
}
_stopBtn_pressup() {
this.stopBtn_Mc.gotoAndStop(0);
this.playBtn_Mc.gotoAndStop(0);
this.status_Tf.text = "Stopped";
this.song.position = 0;
this.song.paused = true;
if (this._tickerListener) {
zim.Ticker.remove(this._tickerListener);
this._tickerListener = null;
}
this.zimstage.update();
}
_soundWaveReady() {
this.bars_Zcnt.alpha = 0.25;
var width = this.screen_Mc.nominalBounds.width * 0.9;
var gap = 1;
zim.loop(this.soundWave.num, (i, total) => {
var bar = new zim.Rectangle(width / total - gap, 300, this.frame.dark);
bar.addTo(this.bars_Zcnt).mov(i * width / total).reg(0, 300);
});
this.bars_Zcnt.x = this.screen_Mc.x;
this.bars_Zcnt.y = this.screen_Mc.y;
this.view_Mc.addChild(this.bars_Zcnt);
// Remove previous ticker if exists
if (this._tickerListener) {
zim.Ticker.remove(this._tickerListener);
}
this._tickerListener = this._updateTicker.bind(this);
zim.Ticker.add(this._tickerListener);
this.zimstage.update();
}
_updateTicker()
{
if (this.song) {
this.time_Tf.text = this._secToMinSecFormat(
(this.song.position / this.song.duration).toFixed(2) * 100
);
}
if(!this.soundWave) return;
var data = this.soundWave.calculate();
zim.loop(this.bars_Zcnt, (bar, i) => {
bar.heightOnly = data[i]*40;// / 10;
});
}
_secToMinSecFormat(pTimeElapsedInSec_int) {
var minute_int = Math.floor(pTimeElapsedInSec_int / 60);
var second_int = Math.floor(pTimeElapsedInSec_int % 60);
var prefix0_str = minute_int < 10 ? "0" : "";
var prefix1_str = second_int < 10 ? "0" : "";
return prefix0_str + minute_int + ":" + prefix1_str + second_int;
}
getView() {
return this.view_Mc;
}
}