0

I am writing a Cordova application, and I am not able to play sounds on a 4.2.2 Android device.

I have tried the 2 things mentioned here, that is PhoneGap's Media class (not working) and this syntax (not working):

var audio = new Audio('coins.mp3');
audio.play();

I have also tried this plugin but without success.

Also the workaround mentioned in code.google.com (using Video instead of Audio) does work when I am in debug, but not after deploying the app to the store and then downloading it:

var success = document.createElement("video");
success.setAttribute("src", "success.mp3");
success.play();

So it is not possible to play audio with javascript on older Android devices?

Sean
  • 923
  • 3
  • 14
  • 36
  • Have you tried it on a newer device? The webview of API17 (4.2.2) may not support HTML5 `audio` and `video` elements. – David Sep 05 '17 at 12:59
  • @David yes in newer devices audio.play() works, but my question is about how to play sounds with javascript on older devices. – Sean Sep 05 '17 at 13:39
  • You can package a newer version of the chrome webview into your `.apk` using a plugin like crosswalk (https://github.com/crosswalk-project/cordova-plugin-crosswalk-webview). Please be aware that this will increase your bundle size by around 20MB. – David Sep 05 '17 at 13:42
  • @David ok I will try, thank you. – Sean Sep 05 '17 at 13:45
  • One more thing: Crosswalk has currently trouble with its servers. So please make sure you add the workaround linked in this answer (https://stackoverflow.com/a/46023415/6184203) to build your project with crosswalk. – David Sep 05 '17 at 13:46
  • @David: well, due to these problems and the size of the bundle maybe I will leave the app mute for old Android versions. Thank you. – Sean Sep 05 '17 at 15:06
  • Or use native audio, but I'll leave the decision to you! – David Sep 05 '17 at 15:07
  • @David: I had already triied the native audio plugin, but it didn't work. Today I retried and I found out that it only does not work with ripple emulators. BUT: this plugin uses the READ_PHONE_STATE permission, so I need a privacy policy for the app, that I would like to avoid because it's very generic and maybe can discourage the user. Even if I comment the READ_PHONE_STATE line in plugin.xml, when uploading the sdk to the console it keeps telling me that the app needs that permission. – Sean Sep 09 '17 at 04:43
  • The `READ_PHONE_STATE ` permission is usually used to pause your media when a call comes in, it is quite common. There are other (probably better) solutions to do so but you will have to fork and modify the plugin to make use of them. – David Sep 09 '17 at 12:30
  • @David Yes I had to remove the reference to READ_PHONE_STATE also in android.json, after this the apk was accepted in the store. Just a little problem of sound latency in old Android versions, but it's ok. Thank you. – Sean Sep 09 '17 at 20:23

0 Answers0