javascript - How to load base64 audio locally on cordova mobile? -
i've been trying load audio file encoded in base64 no avail. can load encoded images easily, can't audio. example found cordova audio passing url link asset folder, tried , didn't work.
is there way play audio file encoded in base64 directly?
i tried cordova-plugin-media seems work files , not base64 code.
finally found solutions:
solution 1:
var src = "data:audio/mp3;base64," + your_base64_code; document.write("<audio preload='auto' autoplay><source src='" + src + "' /></audio>"); solution 2:
1) convert base64 code audio files, saving in cache folder. can read article it: how create audio file mp3 base64 string on device cordova
you can use target folder: cordova.file.cachedirectory
for example:
savebase64asaudiofile(cordova.file.cachedirectory, "audiofile.mp3", your_base64_code, "audio/mp3"); 2) use cordova-plugin-media play created file:
settimeout(function () { var src = cordova.file.cachedirectory + "audiofile.mp3"; var media = new media(src); media.play(); }, 2000); - use settimeout sure files created.
Comments
Post a Comment