歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 用JavaScript操縱HTML5的本地音頻

用JavaScript操縱HTML5的本地音頻

日期:2017/3/1 10:20:01   编辑:Linux編程

HTML5的音頻元素是一個非常強大的元素並且可以避免過多依賴第三方插件。例如QuickTime,Flash。最新的浏覽器,比如Chrome10+和Firefox3.6+都已經嵌入了JavaScript庫,並且還提供了方法和屬性來操縱<audio>元素。在這篇文章中,我們將探討幾種最重要的方法並探討如何使用JavaScript來運行audio文件。

摘要:盡管這篇文章主要介紹Audio對象,但是這些方法和屬性也同樣適用於Video對象。

Audio Methods

.load();

---Loads and re-loads the audio element for playback。(加載或者重加載audio元素實現重播)

  1. audioElement.load();

.play();

---Starts playing the audio file。(開始播放音頻文件)

  1. audioElement.play();

.pause();

Pauses playback of the audio file。(暫停播放音頻文件)

  1. audioElement.pause();

canPlayType(type);

---Checks with browser to see if type of audio file is supported。(檢查浏覽器是否支持音頻文件)

  1. audioElement.canPlayType('audio/ogg; codecs="vorbis"');

Audio Properties

.currentTime

---Sets or returns the playback position in the audio file, Value is in seconds。(設置或返回音頻文件開始播放的位置,返回值以“秒”為單位)

  1. audioElement.currentTime = seconds;

.duration

---Returns the length of the audio file in seconds。(返回播放音頻在某秒上的播放的長度)

  1. var seconds = audioElement.duration;

.src

---The url where the audio file exists。(音頻文件的url)

  1. audioElement.src = 'audio.ogg';

.volume

---Sets or returns the volume of the audio file。(設置或返回音頻文件的體積)

  1. audioElement.volume = value;

注意:以上並沒有把音頻的所有方法和屬性列出來,只介紹了常用的幾個方法和屬性。

Copyright © Linux教程網 All Rights Reserved