网页中播放声音

 1.使用dhtml标记<bgsound>。

<bgsound   id=bg1   src="mid/1.mid">       
  <select   onchange="bg1.src=this.value">  
  <option   value="mid/1.mid">1</option>  
  <option   value="mid/2.mid">2</option>  
  <option   value="mid/3.mid">3</option>  
  </select>

实现通过下拉框选择不同的音乐来播放

<bgsound   src=""   loop=""   />中的loop如果设置为-1表示循环播放无穷大次。

2.使用<embed>嵌入播放器来播放

<embed   src=xx.mp3   autostart=true></embed>

<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'

codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='450' height='384'>
<param name='movie' value='http://live.csdn.net/vcastr21.swf' />
<param name='quality' value='high' /><param name='IsShowBar' value='1'>
<param name='allowFullScreen' value='true' /><param name='BufferTime' value='10' />
<param name='FlashVars' value='vcastr_xml=http://live.csdn.net/xml_movielist/23.xml&IsAutoPlay=1' />
<embed src='http://live.csdn.net/vcastr21.swf' allowFullScreen='true'

FlashVars='vcastr_xml=http://live.csdn.net/xml_movielist/23.xml&IsAutoPlay=1'

pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='450'

height='384'></embed>
</object>

3.使用javascript控制播放与停止

<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _Default "  validateRequest = " false " %>
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 无标题页 </ title >
    
< script language = " javascript "  type = " text/javascript " >
        
// 用javascript进行声音播放控制
       function ManageSoundControl(action)
       
{
            var soundControl 
= document.getElementById("soundControl");
            
if(action == "play")
          
{
                soundControl.play();
            }

            
if(action == "stop")
            
{
                soundControl.stop();
            }

        }

    
</ script >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
        
< table width = " 100% " >
            
< tr >
                
< td >
                     
< embed id = " soundControl "  src = " 求佛.mp3 "  mastersound hidden = " true "  loop = " false "  autostart = " false " ></ embed >
                     
< input id = " btPlay "  type = " button "  value = " 播放 "  onclick = " ManageSoundControl('play') "   />
                     
< input id = " btStop "  type = " button "  value = " 播放 "  onclick = " ManageSoundControl('stop') "   />
                
</ td >
            
</ tr >           
        
</ table >
    
</ form >
</ body >
</ html >

<html>
<head>
<title>用javascript进行声音播放控制</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript">
<!-- //定义一个声音播放控制的function, 需注意Netscape和IE对script的解释各不相同
function MM_controlSound(sndAction,sndObj)
{  
  if(eval(sndObj) != null)
  {   
    if(navigator.appName=='Netscape')
    {
     eval( sndObj+ ( (sndAction=='stop') ? '.stop()' : '.play(false)' ) );   
    }
    else if(eval(sndObj+".FileName"))  
 {
   eval(sndObj+((sndAction=='stop')?'.stop()':'.play()'));  
    }
  }
}
//-->
</script>
</head>

<body>
<!-- 开始控制声音播放 -->
<embed name='MM_controlSound1' src='1.wma' loop=false autostart=false mastersound hidden=true width=10 height=10>
</embed>
<!-- 用onMouseOver控制声音播放 -->
<a href="#" onMouseOver="MM_controlSound('play','document.MM_controlSound1')"
            onMouseOut="MM_controlSound('stop','document.MM_controlSound1')"
> 当鼠标移到这儿时会播放声音</a>
</body>
</html>

你可能感兴趣的:(网页)