How to use the audio tag in PhoneGap?

3

I'm trying to play an .mp3 file as follows:

<audio src="sound/som.mp3" autoplay></audio>

When I open the .html file on my PC, the audio plays normally, but when I compile using phonegap, and I open the android, nothing happens. Any light? Thank you in advance.

    
asked by anonymous 04.05.2015 / 20:49

2 answers

1

For Android, you must set permissions in addition to manipulating the media object with the appropriate plugin.

Permissions on Android

 (in app/res/xml/config.xml)
<feature name="Media">
    <param name="android-package" value="org.apache.cordova.media.AudioHandler" />
</feature>


(in app/AndroidManifest.xml)
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Media Object

// Audio player
//
var meu_objeto_media = new Media(src, onSuccess, onError);

'src' is your file path. The other parameters are callbacks .

After instantiating your Media object execute the play method:

// Play audio
meu_objeto_media.play();

See an example here .

    
04.05.2015 / 20:54
1

The <audio src="sound/som.mp3" autoplay></audio> tag only works on android 5.x by using the webview as a separate app, in the previous versions it has to do the way it was answered above

    
08.05.2015 / 04:53