spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column20


Audio Control Example Code

Developer News
Google Going Native With Chrome
Mozilla Fixes Firefox Flaws as 3.5 Release Nears
Microsoft and Novell Still Bosom Buddies

Here is the code for the example on the previous page:

<SCRIPT LANGUAGE="JavaScript">
<!--

var NSsound = navigator.plugins && navigator.plugins["LiveAudio"];
var IEsound = navigator.plugins && document.all;
var audioEnabled = NSsound || IEsound;

if (!audioEnabled)
  document.write("Your browser does not support
   JavaScript-enabled audio control"); // join with previous line
if (!navigator.javaEnabled()) alert("Your browser is not Java-enabled.
Please edit preferences.");

onload = init;
timerID = null;

function init() {
  if (!navigator.javaEnabled()) return;
  if (!audioEnabled) return;
  document.display.soundcard.value = (IEsound) ?
document.audiopanel.isSoundCardEnabled() : "not supported";
  document.display.filename.value = (IEsound) ?
document.audiopanel.fileName : "not supported";
  endTime = (IEsound) ? 
document.audiopanel.selectionEnd : "not supported"; // global variable
  document.display.position.value =
    (IEsound) ? Math.floor(document.audiopanel.currentPosition) : 
       "not supported"; // join with previous line
  state();
}

function showPosition() {
  if (!navigator.javaEnabled()) return;
  if (!audioEnabled) return;
  document.display.position.value = (IEsound) ?
Math.floor(document.audiopanel.currentPosition) : "not supported";
  setTimeout("showPosition()", 1000);
}

function playIt() {
  if (!navigator.javaEnabled()) return;
  if (!audioEnabled) return;
  if (IEsound) document.audiopanel.play()
  else if (NSsound) document.audiopanel.play(true);
  state();
  timerID = setTimeout("showPosition()", 1000);
}

function stopIt() {
  if (!navigator.javaEnabled()) return;
  if (!audioEnabled) return;
  document.audiopanel.stop();
  state();
  clearInterval(timerID);
}

function pauseIt() {
  if (!navigator.javaEnabled()) return;
  if (!audioEnabled) return;
  document.audiopanel.pause();
  state();
  clearInterval(timerID);
}

function state() {
  if (!navigator.javaEnabled()) return;
  var field = document.display.state;
  if (IEsound) {
    var cur = document.audiopanel.currentState;
    if (cur == 0) field.value = "stopped";
    if (cur == 1) field.value = "paused";
    if (cur == 2) field.value = "playing";
  } else if (NSsound) {
      if (document.audiopanel.IsReady()) field.value = "stopped";
      if (document.audiopanel.IsPlaying()) field.value = "playing";
      if (document.audiopanel.IsPaused()) field.value = "paused";
  }
}

function toggleControls() {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.showControls = !document.audiopanel.showControls;
}

function startAtGivenSec(sec) {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.selectionStart = sec
  else if (NSsound)
         document.audiopanel.start_time(sec);
}

function stopAtGivenSec(sec) {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.selectionEnd = sec
  else if (NSsound)
    document.audiopanel.end_time(sec);
}

function stopAtEnd() {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.selectionEnd = endTime
  else if (NSsound)
    document.audiopanel.stop_at_end();
}

function decreaseVolume() {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.volume -= 10
  else if (NSsound)
    document.audiopanel.setvol(parseInt(document.audiopanel.GetVolume() - 10))
}

function increaseVolume() {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.volume += 10
  else if (NSsound)
    document.audiopanel.setvol(document.audiopanel.GetVolume() + 10)
}

function setDefaultVolume() {
  if (!navigator.javaEnabled()) return;
  if (IEsound)
    document.audiopanel.volume = 50
  else if (NSsound)
    document.audiopanel.setvol(50)
}

if (audioEnabled)
  document.write("<EMBED NAME='audiopanel' SRC='aladdin.mid' MASTERSOUND
    HEIGHT='60' WIDTH='144' HIDDEN='false'>") //join with previous line
else
  document.write("Your browser cannot play sound files.");
</SCRIPT>
<FORM NAME="display">
Sound card enabled: <INPUT TYPE="text" NAME="soundcard" SIZE="14"><BR>
State: <INPUT TYPE="text" NAME="state" SIZE="10"><BR>
Position: <INPUT TYPE="text" NAME="position" SIZE="14"><BR>
Filename: <INPUT TYPE="text" NAME="filename" SIZE="50"><BR>
<INPUT TYPE="button" VALUE="Play" onClick="playIt()">
<INPUT TYPE="button" VALUE="Pause" onClick="pauseIt()">
<INPUT TYPE="button" VALUE="Stop" onClick="stopIt()"><BR>
<INPUT TYPE="button" VALUE="Toggle Controls" onClick="toggleControls()"><BR>
<INPUT TYPE="button" VALUE="Start @ 10 Second" onClick="startAtGivenSec(10)">
<INPUT TYPE="button" VALUE="Stop @ 30 Second" onClick="stopAtGivenSec(30)"><BR>
<INPUT TYPE="button" VALUE="Start @ 0 Second" onClick="startAtGivenSec(0)">
<INPUT TYPE="button" VALUE="Stop @ End of Track" onClick="stopAtEnd()"><BR>
<INPUT TYPE="button" VALUE="<=====" onClick="decreaseVolume()">
<INPUT TYPE="button" VALUE="==^==" onClick="setDefaultVolume()">
<INPUT TYPE="button" VALUE="=====>" onClick="increaseVolume()"><BR>
</FORM>

webref The latest from WebReference.com Browse >
XML and PHP Simplified · Creating a ASP.NET Contact Form · Data Filtering with PHP
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Intel to Host Live Nehalem Q&A · 12 Tips to Troubleshoot Network File-Sharing · 10 Tips for Selling on Kijiji

Created: June 19, 1998
Revised: May 16, 1999

URL: http://www.webreference.com/js/column20/liveaudiocode.html