{"id":84,"date":"2012-10-04T14:29:29","date_gmt":"2012-10-04T18:29:29","guid":{"rendered":"https:\/\/havalite.com\/?p=84"},"modified":"2022-11-22T14:38:08","modified_gmt":"2022-11-22T19:38:08","slug":"play-audio-onhover-with-jquery","status":"publish","type":"post","link":"https:\/\/havalite.com\/?p=84","title":{"rendered":"Play audio onHover with jQuery"},"content":{"rendered":"<p>The simplest way to play audio files in your page without the need of flash is the usage of HTML5 tag &lt;audio&gt; as follow:<\/p>\n<pre class=\"html\">&lt;audio autoplay=\"autoplay\" controls=\"controls\" loop=\"loop\" preload=\"preload\"&gt;\r\n        &lt;source src=\"beep.ogg\" type=\"audio\/ogg\"&gt;\r\n        &lt;source src=\"beep.mp3\" type=\"audio\/mpeg\"&gt;Your Browser doesn't support audio\r\n&lt;\/audio&gt;<\/pre>\n<p>For our example we only need the attributes &#8220;autoplay&#8221; and the two tags &#8220;source&#8221; to define the url of the audio files. Just like this:<\/p>\n<pre class=\"html\">&lt;audio autoplay=\"autoplay\"&gt;\r\n        &lt;source src=\"beep.ogg\" type=\"audio\/ogg\"&gt;\r\n        &lt;source src=\"beep.mp3\" type=\"audio\/mpeg\"&gt;\r\n&lt;\/audio&gt;<\/pre>\n<p>Now we let jQuery take over by embedding the html code for all &lt;a&gt; tags whenever onMouseOver is done:<\/p>\n<pre class=\"js\">$(document).ready(function(){\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0var audioFileName = \"beep\";\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0var mp3File = audioFileName + \".mp3\";\r\n\u00a0\u00a0 \u00a0var oggFile = audioFileName + \".ogg\";\r\n\r\n\u00a0\u00a0 \u00a0$('body').append('&lt;div id=\"audio\" style=\"position:absolute; top:-200px;\"&gt;&lt;\/div&gt;');\r\n\u00a0\u00a0 \u00a0$('a').hover(function(){\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0$('#audio').html('&lt;audio autoplay=\"autoplay\"&gt;'+\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0'&lt;source src=\"' + oggFile + '\" type=\"audio\/ogg\"&gt;'+\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0'&lt;source src=\"' + mp3File + '\" type=\"audio\/mpeg\"&gt;&lt;\/audio&gt;');\r\n\u00a0\u00a0 \u00a0},\r\n\u00a0\u00a0 \u00a0function(){ });\r\n});\r\n<\/pre>\n<p>Thats it!<\/p>\n<p><span style=\"color: #3399ff;\"><a href=\"\/download\/jsound\">DEMO<\/a>:<\/span> Check the upper menu of this page to hear how it works<\/p>\n<p><span class=\"installation\">Useful Links:<\/span><\/p>\n<p><a href=\"http:\/\/audio.online-convert.com\/convert-to-ogg\">Convert mp3 to ogg online<\/a><\/p>\n<p><a href=\"https:\/\/www.google.de\/#hl=de&amp;safe=off&amp;sclient=psy-ab&amp;q=free+sound+effects&amp;oq=free+sound+effects&amp;gs_l=hp.12..0l10.6225.7045.3.12262.5.5.0.0.0.0.101.443.4j1.5.0...0.0...1c.1.J_cwzeIxJXU&amp;psj=1&amp;bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&amp;fp=65c4ceb73042ed56&amp;biw=1920&amp;bih=960\">Free Mp3 sound effects<\/a><\/p>\n<p><a href=\"http:\/\/html5css3box.com\/audio-tag-generator\/\">HTML5 audio tag generator<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The simplest way to play audio files in your page without the need of flash is the usage of HTML5 tag &lt;audio&gt; as follow: &lt;audio autoplay=&#8221;autoplay&#8221; controls=&#8221;controls&#8221; loop=&#8221;loop&#8221; preload=&#8221;preload&#8221;&gt; &lt;source src=&#8221;beep.ogg&#8221; type=&#8221;audio\/ogg&#8221;&gt; &lt;source src=&#8221;beep.mp3&#8243; type=&#8221;audio\/mpeg&#8221;&gt;Your Browser doesn&#8217;t support audio &lt;\/audio&gt; For our example we only need the attributes &#8220;autoplay&#8221; and the two tags &#8220;source&#8221; to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[28,33,31,17,7,29,30,32,34],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-jquery-plugins","tag-audio","tag-css3","tag-hover","tag-html5","tag-jquery","tag-mp3","tag-ogg","tag-onhover","tag-onmouseover"],"_links":{"self":[{"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/havalite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=84"}],"version-history":[{"count":1,"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/havalite.com\/index.php?rest_route=\/wp\/v2\/posts\/84\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/havalite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/havalite.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/havalite.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}