2013-06-25 224 views
0

我試圖設置一個客戶端的網站來播放頁面加載(在他的需求,而不是我的首選功能)的音頻文件。我已經使用了一些簡單的JavaScript,但音頻在頁面加載時不播放。任何想法爲什麼發生這種情況?音頻不播放 - JavaScript

<head> 
<!--other metadata up here--> 
    <script> 
function EvalSound(soundobj) { 
     var thissound = document.getElementById(soundobj); 
     thissound.Play(); 
    } 
    </script> 

    <embed src="scripts/audio.wav" autostart=true width=1 height=1 id="audio" 
    enablejavascript="true"> 
</head> 
<body> 
    <div id="container" onload="EvalSound('audio')"> 
<!--rest of page> 
</body> 

回答

1

HTML5溶液,(IE9 +,和所有其他適當的瀏覽器都支持)

<audio src="audio.mp3" preload="auto" controls></audio> 

<script> 
    var getAudio = document.getElementsByTagName("audio")[0]; 
    getAudio.play(); 
</script> 

自動玩沒有辦法阻止它的音頻雖然是一個可怕的想法。

+0

什麼是放在HTML內的最好的地方? – mikedugan

+0

把'audio'標籤放在'body'的某個地方,然後'Javascript'放在底部,靠近'body'標籤。 –

0

Divs沒有加載事件。你應該把它附着在身體上。

+0

我試過這樣做,並添加一個空的表單使用onload事件爲此無濟於事。 – mikedugan