{"id":2168,"date":"2017-08-12T14:31:48","date_gmt":"2017-08-12T14:31:48","guid":{"rendered":"http:\/\/javascript-tutor.net\/?page_id=2168"},"modified":"2017-08-12T14:34:31","modified_gmt":"2017-08-12T14:34:31","slug":"playing-music-using-javascript","status":"publish","type":"page","link":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/","title":{"rendered":"Playing Music using JavaScript"},"content":{"rendered":"<p>You can create a JavaScript that allows the user to play a music or an audio file.<\/p>\n<p>In order to play an audio file in a browser, you need to use the &lt;audio&gt; tag. The audio object is new in HTML5.<\/p>\n<p>The &lt;audio&gt; element has a number of properties, one of them is<strong> controls<\/strong>. The controls property sets or returns whether an audio should have controls displayed like play, pause etc. \u00a0View<a href=\"https:\/\/www.w3schools.com\/Jsref\/dom_obj_audio.asp\"> here<\/a> for more properties of the audio object.<\/p>\n<p>We also need to use the &lt;source&gt; tag to reference the source of an audio file, \u00a0the syntax is as follows:<\/p>\n<pre>\r\n&lt;audio\u00a0controls&gt;\r\n&lt;source\u00a0src=\"music.ogg\"\u00a0type=\"audio\/ogg\"&gt;\r\n&lt;source\u00a0src=\"music.mp3\"\u00a0type=\"audio\/mpeg\"&gt;\r\n\r\n&lt;\/audio&gt;\r\n<\/pre>\n<p>The &lt;source&gt; tag has to be used together with the &lt;audio&gt; tag<\/p>\n<p>Next, you can create a Play button using the<strong> play()<\/strong> method to start playing the music or audio file. The syntax is<\/p>\n<pre>\r\nmyAudioObject.play()\r\n<\/pre>\n<p>It is often used together with the <strong>pause()<\/strong> method.<\/p>\n<p>The following is a sample script that can play a music file.<\/p>\n<pre>\r\n&lt;audio controls id=\"music\"&gt;\r\n&lt;source src=\"mj.mp3\" type=\"audio\/mpeg\"&gt;\r\nYour browser does not support the audio element.\r\n&lt;\/audio&gt;\r\n\r\n&lt;p&gt;Click the buttons to play or pause the music.&lt;\/p&gt;\r\n\r\n&lt;button onclick=\"play()\" type=\"button\"&gt;Play &lt;\/button&gt;\r\n&lt;button onclick=\"pause()\" type=\"button\"&gt;Pause&lt;\/button&gt;\r\n\r\n&lt;script&gt;\r\nvar myMusic= document.getElementById(\"music\");\r\nfunction play() {\r\nmyMusic.play();\r\n}\r\n\r\nfunction pause() {\r\nmyMusic.pause();\r\n}\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>*You can use your own audio file instead of mj.mp3 file in this example.<\/p>\n<p>View<a href=\"http:\/\/javascript-tutor.net\/Javascript_Samples\/music.htm\"> Play Music<\/a> to see the output.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can create a JavaScript that allows the user to play a music or an audio file. In order to play an audio file in a browser, you need to use the &lt;audio&gt; tag. The audio object is new in HTML5. The &lt;audio&gt; element has a number of properties, one of them is controls. The &hellip; <a href=\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Playing Music using JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":35895,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2168","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"og:description\" content=\"You can create a JavaScript that allows the user to play a music or an audio file. In order to play an audio file in a browser, you need to use the &lt;audio&gt; tag. The audio object is new in HTML5. The &lt;audio&gt; element has a number of properties, one of them is controls. The &hellip; Continue reading &quot;Playing Music using JavaScript&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\" \/>\n<meta property=\"article:modified_time\" content=\"2017-08-12T14:34:31+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\",\"url\":\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\",\"name\":\"Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/javascript-tutor.net\/#website\"},\"datePublished\":\"2017-08-12T14:31:48+00:00\",\"dateModified\":\"2017-08-12T14:34:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/javascript-tutor.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Playing Music using JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/javascript-tutor.net\/#website\",\"url\":\"https:\/\/javascript-tutor.net\/\",\"name\":\"Learn JavaScript Online | Free Interactive JavaScript Tutorials\",\"description\":\"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","og_description":"You can create a JavaScript that allows the user to play a music or an audio file. In order to play an audio file in a browser, you need to use the &lt;audio&gt; tag. The audio object is new in HTML5. The &lt;audio&gt; element has a number of properties, one of them is controls. The &hellip; Continue reading \"Playing Music using JavaScript\"","og_url":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/","og_site_name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","article_modified_time":"2017-08-12T14:34:31+00:00","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/","url":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/","name":"Playing Music using JavaScript - Learn JavaScript Online | Free Interactive JavaScript Tutorials","isPartOf":{"@id":"https:\/\/javascript-tutor.net\/#website"},"datePublished":"2017-08-12T14:31:48+00:00","dateModified":"2017-08-12T14:34:31+00:00","breadcrumb":{"@id":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/javascript-tutor.net\/index.php\/playing-music-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/javascript-tutor.net\/"},{"@type":"ListItem","position":2,"name":"Playing Music using JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/javascript-tutor.net\/#website","url":"https:\/\/javascript-tutor.net\/","name":"Learn JavaScript Online | Free Interactive JavaScript Tutorials","description":"Master JavaScript with free, interactive tutorials for beginners and experienced coders. Practice live coding and visualize how JavaScript works step by step.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/users\/35895"}],"replies":[{"embeddable":true,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/comments?post=2168"}],"version-history":[{"count":8,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2168\/revisions"}],"predecessor-version":[{"id":2176,"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/pages\/2168\/revisions\/2176"}],"wp:attachment":[{"href":"https:\/\/javascript-tutor.net\/index.php\/wp-json\/wp\/v2\/media?parent=2168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}