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

相关文章

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome