How to use the <audio> tag in Phonegap?

Asked

Viewed 377 times

3

I am trying to play a. mp3 file as follows:

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

When I open the file . html on the PC the audio is played normally,but when I compile using phonegap,and open on android,nothing happens. Any light? Thanks in advance.

  • You have set up permission to run audio on your Android project?

  • I do it in HTML itself?

  • No, in your Android project when it matters the phonegap project. In the Manifest file.

2 answers

1

For Android, you need to set permissions in addition to handling 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 run the play method:

// Play audio
meu_objeto_media.play();

Take an example here.

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.