PCMPlayer播放器的使用教程(添加清理暂存区方法)

2024-09-04 14:20

本文主要是介绍PCMPlayer播放器的使用教程(添加清理暂存区方法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 解决的问题:在我们使用PCMPlayer播放的时候,时间长,即使停止播放了,也会有一段声音,这是因为音频暂存区没有清理 

解决方案:添加一个stop方法,话不多上 直接上代码  复制就能使用 !!!

function PCMPlayer(option) {this.init(option);
}PCMPlayer.prototype.init = function(option) {var defaults = {encoding: '16bitInt',channels: 1,sampleRate: 8000,flushingTime: 1000};this.option = Object.assign({}, defaults, option);this.samples = new Float32Array();this.flush = this.flush.bind(this);this.interval = setInterval(this.flush, this.option.flushingTime);this.maxValue = this.getMaxValue();this.typedArray = this.getTypedArray();this.createContext();
};PCMPlayer.prototype.getMaxValue = function () {var encodings = {'8bitInt': 128,'16bitInt': 32768,'32bitInt': 2147483648,'32bitFloat': 1};return encodings[this.option.encoding] ? encodings[this.option.encoding] : encodings['16bitInt'];
};PCMPlayer.prototype.getTypedArray = function () {var typedArrays = {'8bitInt': Int8Array,'16bitInt': Int16Array,'32bitInt': Int32Array,'32bitFloat': Float32Array};return typedArrays[this.option.encoding] ? typedArrays[this.option.encoding] : typedArrays['16bitInt'];
};PCMPlayer.prototype.createContext = function() {this.audioCtx = new (window.AudioContext || window.webkitAudioContext)();this.gainNode = this.audioCtx.createGain();this.gainNode.gain.value = 1;this.gainNode.connect(this.audioCtx.destination);this.startTime = this.audioCtx.currentTime;
};PCMPlayer.prototype.isTypedArray = function(data) {return (data.byteLength && data.buffer && data.buffer.constructor === ArrayBuffer);
};PCMPlayer.prototype.feed = function(data) {if (!this.isTypedArray(data)) return;const formattedData = this.getFormatedValue(data);var tmp = new Float32Array(this.samples.length + formattedData.length);console.log("缓冲区tmp:" + tmp.length);tmp.set(this.samples, 0);tmp.set(formattedData, this.samples.length);this.samples = tmp;console.log("缓冲区samples:" + this.samples.length);
};PCMPlayer.prototype.getFormatedValue = function(inputData) {var data = new this.typedArray(inputData.buffer),float32 = new Float32Array(data.length),i;for (i = 0; i < data.length; i++) {float32[i] = data[i] / this.maxValue;}return float32;
};PCMPlayer.prototype.volume = function(volume) {this.gainNode.gain.value = volume;
};PCMPlayer.prototype.destroy = function() {if (this.interval) {clearInterval(this.interval);}this.samples = null;this.audioCtx.close();this.audioCtx = null;
};PCMPlayer.prototype.flush = function() {if (!this.samples.length) return;var bufferSource = this.audioCtx.createBufferSource(),length = this.samples.length / this.option.channels,audioBuffer = this.audioCtx.createBuffer(this.option.channels, length, this.option.sampleRate),audioData,channel,offset,i,decrement;for (channel = 0; channel < this.option.channels; channel++) {audioData = audioBuffer.getChannelData(channel);offset = channel;decrement = 50;for (i = 0; i < length; i++) {audioData[i] = this.samples[offset];/* fadein */if (i < 50) {audioData[i] =  (audioData[i] * i) / 50;}/* fadeout*/if (i >= (length - 51)) {audioData[i] =  (audioData[i] * decrement--) / 50;}offset += this.option.channels;}}if (this.startTime < this.audioCtx.currentTime) {this.startTime = this.audioCtx.currentTime;}console.log('start vs current ' + this.startTime + ' vs ' + this.audioCtx.currentTime + ' duration: ' + audioBuffer.duration);bufferSource.buffer = audioBuffer;bufferSource.connect(this.gainNode);bufferSource.start(this.startTime);this.startTime += audioBuffer.duration;this.samples = new Float32Array();
};PCMPlayer.prototype.stop = function() {// 清理所有缓冲区数据this.samples = new Float32Array();// 检查 AudioContext 状态并确保完全关闭if (this.audioCtx && this.audioCtx.state !== 'closed') {this.audioCtx.close().then(() => {// 确保所有音频缓冲区已停止if (this.audioCtx.state === 'closed') {setTimeout(() => {// 重新创建 AudioContextthis.audioCtx = new (window.AudioContext || window.webkitAudioContext)();this.gainNode = this.audioCtx.createGain();this.gainNode.gain.value = 1;this.gainNode.connect(this.audioCtx.destination);this.startTime = this.audioCtx.currentTime;}, 100); // 延迟 100ms 后重新创建 AudioContext}});}
};export default PCMPlayer;

使用方法:

1 初始化:

var player = new PCMPlayer({encoding: '16bitInt',channels: 1,sampleRate: 16000,flushingTime: 1000
});

2 开始播放:

            // 处理音频数据const data = new Uint8Array('传入数据源');player.feed(data);

3 停止播放,清理暂存区

 player.stop()

这篇关于PCMPlayer播放器的使用教程(添加清理暂存区方法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136218

相关文章

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

springboot简单集成Security配置的教程

《springboot简单集成Security配置的教程》:本文主要介绍springboot简单集成Security配置的教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,... 目录集成Security安全框架引入依赖编写配置类WebSecurityConfig(自定义资源权限规则

mysql出现ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost‘ (10061)的解决方法

《mysql出现ERROR2003(HY000):Can‘tconnecttoMySQLserveron‘localhost‘(10061)的解决方法》本文主要介绍了mysql出现... 目录前言:第一步:第二步:第三步:总结:前言:当你想通过命令窗口想打开mysql时候发现提http://www.cpp

Mysql删除几亿条数据表中的部分数据的方法实现

《Mysql删除几亿条数据表中的部分数据的方法实现》在MySQL中删除一个大表中的数据时,需要特别注意操作的性能和对系统的影响,本文主要介绍了Mysql删除几亿条数据表中的部分数据的方法实现,具有一定... 目录1、需求2、方案1. 使用 DELETE 语句分批删除2. 使用 INPLACE ALTER T

Spring Boot3虚拟线程的使用步骤详解

《SpringBoot3虚拟线程的使用步骤详解》虚拟线程是Java19中引入的一个新特性,旨在通过简化线程管理来提升应用程序的并发性能,:本文主要介绍SpringBoot3虚拟线程的使用步骤,... 目录问题根源分析解决方案验证验证实验实验1:未启用keep-alive实验2:启用keep-alive扩展建

MySQL INSERT语句实现当记录不存在时插入的几种方法

《MySQLINSERT语句实现当记录不存在时插入的几种方法》MySQL的INSERT语句是用于向数据库表中插入新记录的关键命令,下面:本文主要介绍MySQLINSERT语句实现当记录不存在时... 目录使用 INSERT IGNORE使用 ON DUPLICATE KEY UPDATE使用 REPLACE

MySQL Workbench 安装教程(保姆级)

《MySQLWorkbench安装教程(保姆级)》MySQLWorkbench是一款强大的数据库设计和管理工具,本文主要介绍了MySQLWorkbench安装教程,文中通过图文介绍的非常详细,对大... 目录前言:详细步骤:一、检查安装的数据库版本二、在官网下载对应的mysql Workbench版本,要是