I am trying to implement background music on a website using the audio object, which is controlled by a JavaScript, I have posted a question about this before, @LazyOne explained that IIS will not serve unknown MIME types, and neither will the VS development server.
I have now uploaded the site to an IIS 7 server, the MIME type for ogg is in place and when the site in accessed the server returns a 206 partial content (mozilla.org mentions that 206 responses are normal and expected for ogg files) and assessing the file directly works fine in Firefox (i.e. if you go to domain.com/music/file.ogg, it plays in the browser). and the file plays fine in the background if the autoplay attribute is set, but it does not play via JavaScript.
Here is the JavaScript in question and relevant html:
audioMusic.volume = 0.15;
function playMusic(){
audioMusic.play();
}
function stopMusic(){
audioMusic.pause();
}
Relevant html:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<a href="javascript:void(0)" onclick="playMusic()">on</a>
<a href="javascript:void(0)" onclick="stopMusic()">off</a>
<audio id="audioMusic" loop="loop" preload="auto">
<source src="/Music/music.ogg" type="audio/ogg" />
<source src="/Music/music.mp3" type="audio/mp3" />
</audio>
<script type="text/javascript" src="/Scripts/music.js"></script>
</body>
</html>
More interesting facts:
- the playMusic() function is called in firefox, if you add an alert at the beginning it does off, but it is not able to execute: audioMusic.play()
- I have also tried document.getElementById("audioMusic").play();
- Both document.getElementById("audioMusic").play() and audioMusic.play() work in Chrome and IE, but not in firefox
- I am using Firefox 5.0.1 (the latest)
Question: how to control the audio element via JavaScript in Firefox?
<html xmlns="http://www.w3.org/1999/xhtml">
(the one you have) and<!DOCTYPE HTML><html lang="en-GB">
(the one generated by PhpStorm for HTML5 documents) -- no difference in behaviour -- able to play and stop.audioMusic.play()
would never work in Firefox because Firefox (at least up to version 4, and I suspect ver 5) does not map elements with theid
attribute to properties in the window object, as other browsers seem to do. So you can't access any elements in the 'global scope' like this (using the Browser Object Model - BOM) in Firefox. For cross browser compatibility you would need to use the DOM...document.getElementById()
as @LazyOne suggests in the comments below.