国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Audio API 實(shí)現(xiàn)音頻播放器

這篇具有很好參考價(jià)值的文章主要介紹了Audio API 實(shí)現(xiàn)音頻播放器。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

市面上實(shí)現(xiàn)音頻播放器的庫(kù)有很多,比如wavesurfer.js、howler.js等等,但是都不支持大音頻文件處理,100多M的文件就有可能導(dǎo)致程序崩潰。總之和我目前的需求不太符合,所以打算自己實(shí)現(xiàn)一個(gè)音頻播放器,這樣不管什么需求 在技術(shù)上都可控。下面我們簡(jiǎn)單介紹下wavesurferJs、和howlerJs的實(shí)現(xiàn),然后再講解如何利用audio API實(shí)現(xiàn)自定義語(yǔ)音播放器。

具體資源github下載

wavesurferJs

一開始選擇wavesurferJs 主要是因?yàn)樗囊纛l圖功能。
效果如下:
Audio API 實(shí)現(xiàn)音頻播放器
是不是很漂亮 hh
下面是實(shí)現(xiàn)步驟:

  1. 初始化
this.playWavesurfer = WaveSurfer.create({
	container: '#waveform2',
	mediaType: 'audio',
	height: 43,
	scrollParent: false,
	hideScrollbar: true,
	waveColor: '#ed6c00',
	interact: true,
	progressColor: '#dd5e98',
	cursorColor: '#ddd5e9',
	interact: true,
	cursorWidth: 1,
	barHeight: 1,
	barWidth: 1,
	plugins: [
		WaveSurfer.microphone.create()
	]
});
  1. 動(dòng)態(tài)加載音頻地址
this.playWavesurfer.load(this.audioUrl);
  1. 設(shè)置加載loading和完畢后計(jì)算音頻總時(shí)長(zhǎng)
this.playWavesurfer.on('loading', (percent, xhr) => {
		this.audioLoadPercent = percent - 1;
	})
	this.playWavesurfer.on('ready', () => {
		this.audioLoading = false;
		const duration = this.playWavesurfer.getDuration();
		this.duration = this.formatTime(duration);
		this.currentTime = this.formatTime(0);
	})
  1. 播放中計(jì)算時(shí)長(zhǎng)
this.playWavesurfer.on('audioprocess', function () {
	const duration = that.playWavesurfer.getDuration();
	const currentTime = that.playWavesurfer.getCurrentTime();
	that.currentTime = that.formatTime(currentTime);
	that.duration = that.formatTime(duration);
	if (that.currentTime === that.duration) {
		that.audioPlayingFlag = false;
	}
});
  1. 播放、暫停
this.playWavesurfer.playPause.bind(this.playWavesurfer)();
  1. 快進(jìn)、快退
this.playWavesurfer.skip(15);
//this.playWavesurfer.skip(-15);
  1. 倍數(shù)播放
this.playWavesurfer.setPlaybackRate(value, true);

這樣基本功能大概實(shí)現(xiàn)。

利用howlerJs實(shí)現(xiàn)

  1. 初始化、動(dòng)態(tài)加載音頻路徑
this.howler = new Howl({
	src: [this.audioUrl]
});
  1. 加載完畢計(jì)算音頻總時(shí)長(zhǎng)
this.howler.on('load', () => {
	this.audioLoading = false;
	const duration = this.howler.duration();
	this.duration = this.formatTime(duration);
	this.currentTime = this.formatTime(0);
});

  1. 播放中獲取當(dāng)前時(shí)間
this.currentTime = this.formatTime(this.howler.seek());
  1. 播放完畢
this.howler.on('end', () => {
	this.audioPlayingFlag = false;
	this.siriWave2.stop();
	this.currentTime = "00:00:00";
	this.progressPercent = 0;
	cancelAnimationFrame(this.playTimer);
})
  1. 快進(jìn)、快退
this.howler.seek(this.howler.seek() + 15);
//this.howler.seek(this.howler.seek() - 15);
  1. 設(shè)置倍數(shù)播放
this.howler.rate(value);
  1. 播放、暫停
this.howler.play();
// this.howler.pause();
  1. 手動(dòng)定位播放時(shí)長(zhǎng)
<div id="waveform2" ref="waveform2" @click="changProgress">
	<div class="bar" v-if="!audioLoading&&!audioPlayingFlag"></div>
	<div class="progress" :style="{width: `${progressPercent}`}"></div>
</div>
changProgress(e) {
	if (this.howler.playing()) {
		this.howler.seek((e.offsetX / this.$refs['waveform2'].offsetWidth)*this.howler.duration());
	}
},

這樣基本功能大概實(shí)現(xiàn)。

利用audio API實(shí)現(xiàn)播放器

效果圖:
Audio API 實(shí)現(xiàn)音頻播放器
動(dòng)畫庫(kù) 暫時(shí)用的 siriwave.js

先定義audio標(biāo)簽隱藏,可以js里面動(dòng)態(tài)生成

<audio :src="audioUrl" style="display: none;" controls ref="audio"></audio>
this.audio = this.$refs['audio'];
  1. 獲取音頻url后 動(dòng)態(tài)加載 需要load一下
this.audio.load();
  1. 音頻加載完畢
this.audio.addEventListener("canplaythrough", () => {
	this.audioLoading = false;
	console.log('music ready');
}, false);
  1. 監(jiān)聽可以播放后 計(jì)算音頻時(shí)長(zhǎng)
this.audio.addEventListener("canplay", this.showTime, false);
showTime() {
	if (!isNaN(this.audio.duration)) {
		this.duration = this.formatTime(this.audio.duration);
		this.currentTime = this.formatTime(this.audio.currentTime);
	}
},
  1. 播放中 時(shí)間改變計(jì)算當(dāng)前 時(shí)間
this.audio.addEventListener("timeupdate", this.showTime, true);
  1. 監(jiān)聽播放事件
this.audio.addEventListener('play', () => {
	this.audioPlaying();
}, false);
  1. 播放完畢
this.audio.addEventListener('ended', () => {
	this.audioPlayingFlag = false;
	this.siriWave2.stop();
	this.currentTime = "00:00:00";
	this.progressPercent = 0;
	cancelAnimationFrame(this.playTimer);
}, false)
  1. 前進(jìn)、后退
this.audio.currentTime += 15;
// this.audio.currentTime -= 15;
  1. 設(shè)置播放倍數(shù)
this.audio.playbackRate = value;
  1. 播放、暫停
this.audio.play();
// this.audio.pause();
  1. 音頻定位
<div id="waveform2" ref="waveform2" @click="changProgress">
	<div class="bar" v-if="!audioLoading&&!audioPlayingFlag"></div>
	<div class="progress" :style="{width: `${progressPercent}`}"></div>
</div>

計(jì)算 定位時(shí)長(zhǎng)

changProgress(e) {
	// if (this.audioPlayingFlag) {
		this.audio.currentTime = (e.offsetX / this.$refs['waveform2'].offsetWidth)*this.audio.duration;
		this.progressPercent = ((this.audio.currentTime/this.audio.duration) * 100) + '%';
	// }
},
  1. siri動(dòng)畫實(shí)現(xiàn)
this.siriWave = new SiriWave({
	container: that.$refs['waveform'],
		height: 43,
		cover: true,
		color: '#ed6c00',
		speed: 0.03,
		amplitude: 1,
		frequency: 6
	});

開啟動(dòng)畫、停止動(dòng)畫

this.siriWave.start();
// this.siriWave.stop();

這樣基本功能大概實(shí)現(xiàn)。 即使加載再大音頻文件也不會(huì)卡。

踩坑

Audio API 實(shí)現(xiàn)音頻播放器
這里遇到一個(gè)大坑就是 audio自帶,音頻播放定位功能,在外面瀏覽器和vscode主體代碼里面都可以定位,偏偏我在vscode插件里面不可以定位,會(huì)自動(dòng)歸0。翻遍了文檔在MDN上找到這樣一段描述:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Configuring_servers_for_Ogg_media#Handle_HTTP_1.1_byte_range_requests_correctly

Handle HTTP 1.1 byte range requests correctly
In order to support seeking and playing back regions of the media that aren’t yet downloaded, Gecko uses HTTP 1.1 byte-range requests to retrieve the media from the seek target position. In addition, Gecko uses byte-range requests to seek to the end of the media (assuming you serve the Content-Length header) in order to determine the duration of the media.
Your server should accept the Accept-Ranges: bytes HTTP header if it can accept byte-range requests. It must return 206: Partial content to all byte range requests; otherwise, browsers can’t be sure you actually support byte range requests.
Your server must also return 206: Partial Content for the request Range: bytes=0- as well.

經(jīng)驗(yàn)證是和response header有關(guān)的。我通過對(duì)MP3資源set不同的response header來驗(yàn)證,結(jié)果如下(貌似segmentfault不支持markdown的表格,所以下面排版有點(diǎn)亂。):
ie
Content-Type 必須,當(dāng)我設(shè)為audio/mpeg時(shí)才能播放,設(shè)為application/octet-stream不能。
Content-Length必須。和Accept-Ranges無(wú)關(guān)。

chrome
Content-Type 無(wú)關(guān),設(shè)為application/octet-stream可以播放。
Content-Length,Accept-Ranges必須都有才可更改 currentTime。

也就是說ie需要response header 有正確的Content-TypeContent-Length。
chrome需要頭部有Content-LengthAccept-Ranges。

然后我想到 vscode插件系統(tǒng)使用了 Service Worker

const headers = {
		'Content-Type': entry.mime,
		'Content-Length': entry.data.byteLength.toString(),
		'Access-Control-Allow-Origin': '*',
	};

果然 沒有添加 Accept-Ranges字段

const headers = {
	'Content-Type': entry.mime,
	'Content-Length': entry.data.byteLength.toString(),
	'Access-Control-Allow-Origin': '*',
};

/**
 * @author lichangwei
 * @description 音頻額外處理 否則無(wú)法調(diào)節(jié)進(jìn)度
 * https://developer.mozilla.org/en-US/docs/Web/HTTP/Configuring_servers_for_Ogg_media#Handle_HTTP_1.1_byte_range_requests_correctly
 */

if (entry.mime === 'audio/mpeg') {
	headers['Accept-Ranges'] = 'bytes';
}

添加后就可以使用了。

后續(xù)

看了一下印象筆記的語(yǔ)音筆記實(shí)現(xiàn)。
Audio API 實(shí)現(xiàn)音頻播放器

Audio API 實(shí)現(xiàn)音頻播放器
印象筆記是如何避免大文件處理的呢

  • 首先錄音過程中繪制的是真的音頻線
  • 錄音結(jié)束后是用假的音頻線替代的

其實(shí)錄制過程中實(shí)時(shí)處理音頻數(shù)據(jù)還好,不至于導(dǎo)致瀏覽器崩潰,錄制后生成的音頻文件處理數(shù)據(jù)量太大了,內(nèi)存直接飆升2-3G,所以會(huì)導(dǎo)致程序崩潰

后續(xù)實(shí)現(xiàn)音頻圖文章來源地址http://www.zghlxwxcb.cn/news/detail-496157.html

兄弟萌給個(gè)關(guān)注~

到了這里,關(guān)于Audio API 實(shí)現(xiàn)音頻播放器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【html本地工具】html+css+js本地音樂播放器,實(shí)現(xiàn)可視化音頻頻譜

    【html本地工具】html+css+js本地音樂播放器,實(shí)現(xiàn)可視化音頻頻譜

    html+css+js本地音樂播放器,實(shí)現(xiàn)可視化音頻頻譜 之前用swing寫了個(gè)本地音樂播放器(如下圖),但是效果一言難盡,界面丑,功能bug也多,唉 所以后面又重新用html寫了個(gè),界面樣式和功能方面,比swing寫的好看、完善多了。 導(dǎo)入音樂(已完成) 展示列表(已完成) 列表雙擊

    2024年02月14日
    瀏覽(46)
  • Android 中封裝優(yōu)雅的 MediaPlayer 音頻播放器,支持多個(gè)播放器

    Android 中封裝優(yōu)雅的 MediaPlayer 音頻播放器,支持多個(gè)播放器實(shí)例的示例: 上述代碼中,使用 getInstance() 方法獲取 AudioPlayer 的單例對(duì)象,參數(shù)傳入 Context 對(duì)象。 在 getInstance() 方法中判斷單例對(duì)象是否為空,如果為空則創(chuàng)建新的 AudioPlayer 對(duì)象,否則返回已有的單例對(duì)象。 這樣

    2024年02月12日
    瀏覽(25)
  • uniapp之音頻播放器

    uniapp之音頻播放器

    日常業(yè)務(wù)會(huì)遇到 微信音頻 mp3播放器, 特別是微信文章閱讀,下面僅作參考 1.解決滑動(dòng)卡頓bug 加了防抖 2.滑動(dòng)進(jìn)度條時(shí) 先暫停再播放 就不會(huì)出現(xiàn)卡頓 3.初始化時(shí) 要onCanplay鉤子中 setInterval 獲取音頻文件長(zhǎng)度 不然會(huì)顯示 0 注意用了vantUI 框架的icon 不用可以去掉 換圖片或者其他

    2024年02月11日
    瀏覽(23)
  • 播放器開發(fā)(六):音頻幀處理并用SDL播放

    AudioOutPut 模塊 1、初始化【分配緩存、讀取信息】 2、開始線程工作【從隊(duì)列讀幀-重采樣-SDL回調(diào)-寫入音頻播放數(shù)據(jù)-SDL進(jìn)行播放】 分配緩存 重采樣相關(guān) SDL的音頻回調(diào) AudioOutPut PlayerMain 添加音頻輸出代碼 測(cè)試運(yùn)行結(jié)果 如果需要同時(shí)執(zhí)行視頻和音頻的輸出,記得要在解復(fù)用模塊

    2024年02月20日
    瀏覽(32)
  • 瀏覽器網(wǎng)頁(yè)內(nèi)嵌Qt-C++音視頻播放器的實(shí)現(xiàn),支持軟硬解碼,支持音頻,支持錄像截圖,支持多路播放等,提供源碼工程下載

    瀏覽器網(wǎng)頁(yè)內(nèi)嵌Qt-C++音視頻播放器的實(shí)現(xiàn),支持軟硬解碼,支持音頻,支持錄像截圖,支持多路播放等,提供源碼工程下載

    ????在瀏覽器中實(shí)現(xiàn)播放RTSP實(shí)時(shí)視頻流,?體上有如下?個(gè)?案: ?案一:瀏覽器插件?案 ActiveX、NPAPI、PPAPI ????ActiveX插件適用于IE瀏覽器,NPAPI與PPAPI插件適用于谷歌瀏覽器,不過這些插件都已經(jīng)不被瀏覽器所支持。 ?案二:先轉(zhuǎn)碼再轉(zhuǎn)流?案 ?????作原理是架設(shè)一

    2024年01月17日
    瀏覽(100)
  • 用selenium爬取直播信息,前端音頻播放器

    用selenium爬取直播信息,前端音頻播放器

    #保存數(shù)據(jù)的函數(shù) def save_data(self,data_list,i): #在當(dāng)前目錄下將數(shù)據(jù)存為txt文件 with open(‘./douyu.txt’,‘w’,encoding=‘utf-8’) as fp: for data in data_list: data = str(data) fp.write(data+‘n’) print(“第%d頁(yè)保存完成!” % i) (2)保存為json文件 #保存數(shù)據(jù)的函數(shù) def save_data(self,data_list,i): with op

    2024年04月16日
    瀏覽(18)
  • 一個(gè)WPF開發(fā)的、界面簡(jiǎn)潔漂亮的音頻播放器

    一個(gè)WPF開發(fā)的、界面簡(jiǎn)潔漂亮的音頻播放器

    今天推薦一個(gè)界面簡(jiǎn)潔、美觀的、支持國(guó)際化開源音頻播放器。 這是一個(gè)基于C# + WPF開發(fā)的,界面外觀簡(jiǎn)潔大方,操作體驗(yàn)良好的音頻播放器。 支持各種音頻格式,包括:MP4、WMA、OGG、FLAC、M4A、AAC、WAV、APE 和 OPUS;支持標(biāo)記、實(shí)時(shí)顯示歌詞等功能;支持換膚、中英文等主流

    2024年02月01日
    瀏覽(48)
  • Vue 3 + ffmpeg + wasm 實(shí)現(xiàn)前端視頻剪輯、音頻剪輯、音波展示、視頻抽幀、gif抽幀、幀播放器、字幕、貼圖、時(shí)間軸、素材軌道

    預(yù)覽 www.bilibili.com/video/BV1YT411Y7YJ 技術(shù)棧: ?? Vue 3、Vue-Router 4、Vite、pnpm、esbuild、TypeScript ?? Pinia 狀態(tài)管理 ?? Tailwind 原子css集成 ?? ffmpeg、wasm 底層音視頻處理集成 功能 多軌道時(shí)間軸,支持幀縮放,時(shí)間縮放 支持多種類型軌道的添加刪除 多功能軌道調(diào)節(jié),支持音視頻軌

    2024年02月11日
    瀏覽(31)
  • 音頻播放器Web頁(yè)面代碼實(shí)例(基于HTML5)

    音頻播放器Web頁(yè)面代碼實(shí)例(基于HTML5)

    音頻播放器Web頁(yè)面代碼實(shí)例(基于HTML5): ? 特別需要注意的點(diǎn): ??? 如果上傳文件時(shí)設(shè)置的是默認(rèn)轉(zhuǎn)碼方式,所有的文件都會(huì)轉(zhuǎn)碼為視頻文件,使用音頻播放器播放視頻文件時(shí),只會(huì)播放聲音,沒有圖像。 ??? 如果上傳文件時(shí)設(shè)置了\\\"源文件播放\\\",平臺(tái)不會(huì)對(duì)源文件進(jìn)行

    2024年02月16日
    瀏覽(28)
  • 基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(六):使用 SDL 播放音頻和視頻

    基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(六):使用 SDL 播放音頻和視頻

    基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(一):FFMPEG + Conan 環(huán)境集成 基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(二):基礎(chǔ)知識(shí)和解封裝(demux) 基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(三):視頻解碼 基于 FFmpeg 的跨平臺(tái)視頻播放器簡(jiǎn)明教程(四):像素格式與格式轉(zhuǎn)換

    2024年02月13日
    瀏覽(124)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包