av/cmd/audio-player/main.js

143 lines
3.8 KiB
JavaScript

/*
NAME
main.js
AUTHOR
Trek Hopton <trek@ausocean.org>
Alan Noble <alan@ausocean.org>
LICENSE
This file is Copyright (C) 2018 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License in gpl.txt.
If not, see [GNU licenses](http://www.gnu.org/licenses).
*/
window.onload = function () {
document.getElementById('fileinput').addEventListener('change', processData);
}
function processData() {
const input = event.target.files[0]
const reader = new FileReader()
reader.onload = event => {
bytes = new Uint8Array(event.target.result)
// decode adpcm to pcm
var decoded = decode(Array.from(bytes))
// convert raw pcm to wav
var wav = pcmToWav(decoded, 48000, 1, 16);
// play wav data in player
const blob = new Blob([Uint8Array.from(wav)], {
type: 'audio/wav'
});
const url = URL.createObjectURL(blob);
const audio = document.getElementById('audio');
const source = document.getElementById('source');
source.src = url;
audio.load();
audio.play();
}
reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input)
}
// getQuery gets everything after the question mark in the URL.
function getQuery() {
var regex = new RegExp("\\?(.*)");
var match = regex.exec(window.location.href);
if (match == null) {
return '';
} else {
return decodeURIComponent(match[1].replace(/\+/g, " "));
}
}
function load() {
var url = document.getElementById('url').value;
if (url == "") {
url = getQuery()
document.getElementById('url').value = url;
}
if (url[0] == '/') {
url = window.location.protocol + '//' + window.location.host + url;
}
if (url == "") {
return;
}
var video = document.getElementById('video');
if (Hls.isSupported()) {
console.log("play: browser can play HLS.")
var hls = new Hls({
autoStartLoad: true,
debug: true,
startFragPrefetch: true
});
hls.loadSource(url);
hls.attachMedia(video);
hls.on(Hls.Events.ERROR, function (event, data) {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
console.log("play: fatal network error encountered, trying to recover.");
hls.startLoad();
break;
case Hls.ErrorTypes.MEDIA_ERROR:
console.log("play: fatal media error encountered, trying to recover.");
hls.recoverMediaError();
break;
default:
console.log("play: fatal error, cannot recover.");
hls.destroy();
break;
}
} else {
console.log("play: " + data.type + "error, " + data.details);
}
});
hls.on(Hls.Events.MANIFEST_PARSED, function () {
playPromise = video.play();
if (playPromise !== undefined) {
playPromise.then(_ => {
console.log("play: autoplay started.")
document.getElementById('msg').innerHTML = "";
}).catch(error => {
console.log("play: autoplay was prevented.")
document.getElementById('msg').innerHTML = "Autoplay prevented. Hit play button to start."
// Remove the message once playing starts.
video.addEventListener('playing', function (e) {
document.getElementById('msg').innerHTML = "";
});
});
}
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
console.log("play: browser can handle vnd.apple.mpegurl.")
video.src = url;
video.addEventListener('loadedmetadata', function () {
video.play();
});
} else {
console.log("play: HLS not supported by browser.")
}
}