I am making a simple web app that needs to play a some audio files, using howler.js. howler.js accepts base64 URI as input, so I wanted to try that out. To test it, I took a sample audio file and used an online audio-to-base64 encoder to get the base64 URI. I added the data description ("data:audio/wav;base64,") the front of the base64 string and copy and pasted into the following JS function...:
function playSound() {
var data = "";
var sound = new Howl({
src: [data],
loop: false
});
sound.play();
}
...and it worked perfectly. Since I would be dealing with a fair number of audio files, I figured I'd use a short python script to convert them all to the base64. To test, I converted the same audio to a base64 string with the following python code:
import base64
with open("0.wav", "rb") as f1,open("b64.txt", "w") as f2:
encoded_f1 = base64.b64encode(f1.read())
f2.write("data:audio/wav;base64,")
f2.write(str(encoded_f1))
I noticed the base64 string was different front the one I got from the website earlier. I pasted this into the JS function shown earlier, but when I attempt to play the sound, I get the following error:
Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
There seems to be some sort of difference in the way python is encoding to base64. What could the cause for this be?
atob()
called? – Arnstatob()
is called). – Paramentdata URI
created atpython
? – Arnstbase64
encoder use the same (default) alphabet (+/
for last 2 chars). The only difference is the service wraps its output to 76 characters per line, but that shouldn't matter. There something you're not telling us. – Caresse