audio-player: playing pcm in browser

This commit is contained in:
Trek H 2019-07-18 16:04:50 +09:30
parent f7e877eb0c
commit 99acedb343
1 changed files with 19 additions and 20 deletions

View File

@ -1,25 +1,24 @@
window.onload = function () {
document.getElementById('input').addEventListener('change', getFile)
document.getElementById('input').addEventListener('change', playFile)
}
function getFile(){
const input = event.target
if ('files' in input && input.files.length > 0) {
placeFileContent(document.getElementById('content'), input.files[0])
}
function playFile() {
const input = event.target.files[0]
const reader = new FileReader()
reader.onload = event => playData(event.target.result) // desired file content
reader.onerror = error => reject(error)
reader.readAsArrayBuffer(input) // you could also read images and other binaries
}
function placeFileContent(target, file) {
readFileContent(file).then(content => {
target.value = content
}).catch(error => console.log(error))
}
function readFileContent(file) {
const reader = new FileReader()
return new Promise((resolve, reject) => {
reader.onload = event => resolve(event.target.result)
reader.onerror = error => reject(error)
reader.readAsText(file)
})
}
function playData(array){
var data = new Uint8Array(array)
console.log("playing file")
var player = new PCMPlayer({
encoding: '16bitInt',
channels: 1,
sampleRate: 48000,
flushingTime: 2000
});
player.feed(data)
}