Sunday, February 19, 2012

Play mp3 in HTML5 audio tag

Code to embed HTML5 audio tag to play mp3:
Play mp3 in HTML5 audio tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mobile-Web-App: HTML5 Video</title>
</head>
<body>

<p>
<button id="play">PLAY</button>
<button id="pause">PAUSE</button>
</p>
<audio id="audio" controls="controls">
<source src="samplemusic.mp3" type="audio/mp3" />
</audio>


<script>

var myAudio = document.getElementById("audio");
var btnPlay = document.getElementById("play");
var btnPause = document.getElementById("pause");

btnPlay.addEventListener("click", function(){myAudio.play();}, false);
btnPause.addEventListener("click", function(){myAudio.pause();}, false);

</script>

</body>
</html>


Related:
- Control HTML5 video using Javascript

1 comment:

  1. Excelente el post.. gracias por compartir este sencillo pero funcional ejemplo..saludos

    ReplyDelete