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 () { window.onload = function () {
document.getElementById('input').addEventListener('change', getFile) document.getElementById('input').addEventListener('change', playFile)
} }
function getFile(){ function playFile() {
const input = event.target const input = event.target.files[0]
if ('files' in input && input.files.length > 0) {
placeFileContent(document.getElementById('content'), input.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) { function playData(array){
readFileContent(file).then(content => { var data = new Uint8Array(array)
target.value = content console.log("playing file")
}).catch(error => console.log(error)) var player = new PCMPlayer({
} encoding: '16bitInt',
channels: 1,
function readFileContent(file) { sampleRate: 48000,
const reader = new FileReader() flushingTime: 2000
return new Promise((resolve, reject) => { });
reader.onload = event => resolve(event.target.result) player.feed(data)
reader.onerror = error => reject(error)
reader.readAsText(file)
})
} }