I'm getting a Content Security Policy directive violation when trying to play an mp3 from a base64 encoded string.
Context: I'm developing a Chrome App that I convert to an Android App using Apache Cordova. When running the Chrome App on my computer everything works like a charm, but when running the App under Android I see the following error in the console:
Refused to load media from 'data:audio/mp3;base64,//tQxAAAAA…AAAAD/' because it violates the following Content Security Policy directive: "media-src *".
The code is pretty simple:
var sound = 'data:audio/mp3;base64,//tQxAAAAAAAAAAAAAA…AAD/';
new Audio(sound).play();
As far as I can see I can not loosen the restriction "media-src *". I added this to my index.html:
<meta http-equiv="Content-Security-Policy" content="media-src *">
And these (just for testing) to my config.xml:
<allow-navigation href="*"/>
<allow-intent href="*"/>
But no success...
I also tried what was described here.
function onSuccess() {
console.log('success', arguments);
}
function onError() {
console.log('error', arguments);
}
function onStatus() {
console.log('status', arguments);
}
var player = new Media(sound, onSuccess, onError, onStatus);
console.log('Using cca Media');
player.play()
Output:
Using cca Media
status { 0: 1 }
error { 0: { code: 1 } }
So this does not seem to be suitable for playing music from a base64 string because I see this when checking logcat:
I/MediaPlayerService(28744): [setDataSource] setDataSource(/storage/sdcard0/data:audio/mp3;base64,//tQxAAAAAA…bR+0Ne
D/MediaPlayerFactory(28744): getPlayerType(): using url, check for DRM protected midi.
D/DrmMtkUtil/DrmUtil(28744): checkDcf ----> path [/storage/sdcard0/data:audio/mp3;base64,//tQxAAAAAA…t5A2r/
V/DrmMtkUtil/DrmUtil(28744): checkExistence ----> [/storage/sdcard0/data:audio/mp3;base64,//tQxAAAAAA…Vt5A2r
E/DrmMtkUtil/DrmUtil(28744): checkExistence failed, reason [File name too long]
E/DrmMtkUtil/DrmUtil(28744): checkDcf: file does not exist.
D/MediaPlayerService(28744): player type = 4
E/DrmMtkUtil(28744): [ERROR]isDcf() : failed to dup fd, reason [File name too long]
I/MediaPlayerService(28744): [setDataSource] setDataSource(/storage/sdcard0/data:audio/mp3;base64,//tQxAAAAAAA…txIbR+0Ne
I/MediaPlayerService(28744): [prepareAsync] [45] prepareAsync
D/NuPlayer(28744): kWhatPrepare, source type = 0
E/ (28744): Failed to open file '/storage/sdcard0/data:audio/mp3;base64,//tQxAAAAA…5A2r/
It assumes the source is a filename. I also did not find anything in the documentation to force it to interpret it as a data URI.
In fact this question was my hottest lead, but the resolution is not really clear to me since it looks he is (eventually) successfully doing it just the way I have unsuccessfully tried it...
So, what is the proper way to play audio from a base64 encoded string in Android?