I'm very much a noob at this but I've got my .wav files playing sound effects and even overlapping when I need them to in Safari, Chrome, IE and FireFox but the only problem I still have is I can't set the volume in IE8. It just plays at the same loud level of the original file. Here is the code I have (with things I've tried commented out).
this.load = function( audiofile )
{
var i;
for ( i=0; i<overlapMax; i++ )
{
var object = null;
if ( ie8 )
{
object = document.createElement('div');
var iesound = '';
// adding volume here didn't do anything
// iesound = iesound + '<object id="'+soundID+'track'+i+'" type="audio/x-wav" volume=2 data="'+audiofile+'" width="200" height="16">';
iesound = iesound + '<object id="'+soundID+'track'+i+'" type="audio/x-wav" data="'+audiofile+'" width="200" height="16">';
iesound = iesound + '<param name="src" value="'+audiofile+'" />';
iesound = iesound + '<param name="volume" value="2" />'; // adding volume here didn't do anything iesound = iesound + '<param name="autoplay" value="false" />'; iesound = iesound + '<param name="autostart" value="0" />'; iesound = iesound + '<param name="pluginurl" value="http://www.apple.com/quicktime/download/" />';
iesound = iesound + '</object>';
object.id = soundID+'track'+i+'div';
object.innerHTML = iesound;
object.style.visibility = 'hidden';
object.style.position = 'absolute';
object.style.left = '0px';
object.style.top = '0px';
}
else
{
object = document.createElement('audio');
object.setAttribute('id',soundID+'track'+i);
object.setAttribute('src',audiofile);
}
document.body.appendChild( object );
var newsound = document.getElementById(soundID+'track'+i);
// needs to be handled with a method & params
//
newsound.volume = 0.02; // this sets volume just fine for everything but ie8
tracks.push( newsound );
}
}
Some people will tell you no but you want to know how so this is HOW to best approach the widest cross-browser implementation of playing music and sound in browsers...
ReplyDeleteFirst this is ultimately a Flash question if you want this to work reliably across all browsers.
Secondly never EVER use Microsoft's proprietary JScript innerHTML method. It doesn't register the DOM (which is the browser's interface that people use JavaScript to interact with). What will happen is when you get to the application level of JavaScript you will not be able to interact with anything dumped on to the DOM as a bunch of text, it's not text, it's an application. Use W3C compliant methods such as appendChild, insertBefore, etc. There is going to be a big nasty invisible wall between you and being able to do things reliably that you will never get around until you stop using innerHTML. If anything in life were easy everyone would be happy, do things the right way and you become more valuable than others who can't and to be frank doing things the right way becomes easy if you care enough to make the effort to begin with.
Thirdly you've encountered an Internet Explorer bug. The issue is that unfortunately Internet Explorer requires a param element with the name="movie" attribute="value" with a reiteration of the .swf path. Why? No logical reason other than that is how Internet Explorer works unfortunately.
Your object element should look like this...
<object class="left" data="mplayer.swf" style="min-height: 1px; min-width: 1px;" type="application/x-shockwave-flash">
<param name="movie" value="mplayer.swf" />
<param name="loop" value="false" />
<param name="play" value="false" />
<param name="wmode" value="transparent" />
<p>Alternative content, a link to download the Flash plugin in example.</p>
</object>
Now I notice you're using a wav file so you need to keep in mind that for this to work without Flash Internet Explorer (or really any other browser) is going to be completely dependent on the user having a plugin that is setup to handle this file type. This means something like Quick Time in example. That's going to make the user's experience terribly subjective to what they've already encountered (usually tons of junk if they're not very technical and don't have any (good-willed) technical people around to help them out).
As much as I dislike advocating Flash it's unfortunately necessary to really get a consistent cross-browser experience. Since Microsoft is part of the MPEG-LA license troll group they continue to not support HTML5 audio/video elements (only open formats are acceptable and they must work out-of-box without the user having to do anything to qualify as supporting those elements) otherwise it's a corporation with a profit based agenda. So the audio/video elements simply won't be reliable well in to the 2020's if not the 2030's (mainly in conjunction with Microsoft's excessive length of support for their products). Since Flash has a near-100% market share I recommend using that and I have a follow-up to help you out WITH a working live example...
I recommend you check out John Bezanis's Flash MP3 Player; http://www.bezzmedia.com/swfspot/samples/flash8/Mp3_Player_with_XML_Playlist
You can see a modified version working on my website (see the link in my profile) by entering with music enabled. I essentially use JavaScript to handle everything with minimal Flash scripting used in the original Flash object.
I'll be happy to clarify anything you need.