2016-05-20 51 views
1

音頻當我點擊按鈕 「PLAY」 或 「暫停」,它不會在Chrome中工作:我不能用javascript來控制鉻

<script> 
    var audioPlayer = document.getElementsByName("player_audio"); 

    function play(){ 
     audioPlayer.play(); 
    } 

    function pause(){ 
     audioPlayer.pause(); 
    } 
</script> 

HTML:

<audio id="player_audio" controls> 
<source id="audio_sources" src="http://admin.test.cnrmobile.com/18/1463652002.aac"> 

<p> 
<button onclick="play()"> play </button> 
</p> 
<p> 
<button onclick="pause()"> pause </button> 
</p> 
+1

代替'VAR audioPlayer ='document.getElementsByName'( 「player_audio」)的;'** **使用VAR' audioPlayer ='document.getElementById'(「player_audio」);' – zer00ne

+0

我剛剛更新了我的答案。 – fuzz

回答

1
<!DOCTYPE html> 
<html> 
<body> 

<button onclick="playAudio()" type="button">Play Audio</button> 
<button onclick="pauseAudio()" type="button">Pause Audio</button><br> 

<audio id="myAudio" width="320" height="176"> 
    <source src="http://admin.test.cnrmobile.com/18/1463652002.aac" type="audio/ogg"> 
    Your browser does not support HTML5 audio. 
</audio> 

<script> 
var aud = document.getElementById("myAudio"); 

function playAudio() { 
    aud.play(); 
} 

function pauseAudio() { 
    aud.pause(); 
} 
</script> 

</body> 
</html> 

https://jsfiddle.net/0fjgxosu/

您與document.getElementById例如,而不是隻是罰款太:

<script> 
    var audioPlayer = document.getElementById("player_audio"); 

    function play(){ 
     audioPlayer.play(); 
    } 

    function pause(){ 
     audioPlayer.pause(); 
    } 
</script> 

https://jsfiddle.net/0fjgxosu/1/

+0

我也讀過這個例子,但它不起作用。 – user6358951

+0

什麼不起作用? Google Chrome瀏覽器對我來說工作得很好。你測試過JSFiddle鏈接嗎?我可以聽到一些亞洲人在後臺播放音樂,所以它工作的很好。 – fuzz

+0

用你的問題更新了我的答案,並更改了'document.getElementById'以使它工作。 – fuzz