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

相关文章

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

python使用fastapi实现多语言国际化的操作指南

《python使用fastapi实现多语言国际化的操作指南》本文介绍了使用Python和FastAPI实现多语言国际化的操作指南,包括多语言架构技术栈、翻译管理、前端本地化、语言切换机制以及常见陷阱和... 目录多语言国际化实现指南项目多语言架构技术栈目录结构翻译工作流1. 翻译数据存储2. 翻译生成脚本

C++初始化数组的几种常见方法(简单易懂)

《C++初始化数组的几种常见方法(简单易懂)》本文介绍了C++中数组的初始化方法,包括一维数组和二维数组的初始化,以及用new动态初始化数组,在C++11及以上版本中,还提供了使用std::array... 目录1、初始化一维数组1.1、使用列表初始化(推荐方式)1.2、初始化部分列表1.3、使用std::

C++ Primer 多维数组的使用

《C++Primer多维数组的使用》本文主要介绍了多维数组在C++语言中的定义、初始化、下标引用以及使用范围for语句处理多维数组的方法,具有一定的参考价值,感兴趣的可以了解一下... 目录多维数组多维数组的初始化多维数组的下标引用使用范围for语句处理多维数组指针和多维数组多维数组严格来说,C++语言没

在 Spring Boot 中使用 @Autowired和 @Bean注解的示例详解

《在SpringBoot中使用@Autowired和@Bean注解的示例详解》本文通过一个示例演示了如何在SpringBoot中使用@Autowired和@Bean注解进行依赖注入和Bean... 目录在 Spring Boot 中使用 @Autowired 和 @Bean 注解示例背景1. 定义 Stud

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行