Playing Sound and Music

In Monaca, there are two ways to play sound and music such as:

Using HTML5 <audio> Tag

The HTML5 audio tag may not work properly on iOS devices.

Playing Sound from External Sources

HTML5 <audio> tag can be used to play sound located on external sources by specifying its URL. For example, the following code snippet is using <audio> tag to play an audio file based on a specified source:

...
<body>
  <!-- Play the music when the Play control is pressed -->
  <audio src="External audio URL" controls></audio>
  <!-- The music is played as soon as the application is loaded -->
  <!-- <audio src="External audio URL" autoplay></audio> -->
</body>
...

Playing the Sound Located inside a Monaca Project

HTML5 <audio> tag can also be used to play sound located locally within a Monaca project. For example, the following code snippet is using <audio> tag to play an audio file located under www/ folder:

Using Cordova Media Plugin

With Cordova Media plugin, you have more functions to control over how you play a sound file such as start, pause, stop, resume, set volume and so on.

Before getting started, you are required to enable Media plugin in Monaca Cloud IDE.

Playing Sound from External Sources

In the following example, we will show you how use a sound file from an external source. You can play, pause and stop the music.

Playing the Sound Located inside a Monaca Project

In the following example, we will show you how use a local sound file located under www/ folder. You can play, pause and stop the music.

Last updated

Was this helpful?