- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在以下网址上:
https://www.tophtml.com/snl/15.mp3
我想在以下范围
上使用纯Web Audio API
播放一个音频:
range from: second: 306.6
range to: second: 311.8
total: 5.2 seconds
我将该文件下载到我的 table 面(我使用的是 Windows 10
),然后使用 VLC
打开它并获得以下文件信息:
number of channels: 2
sample rate: 44100 Hz
bits per sample: 32 (float32)
在这里你有关于这个概念的信息:
从那里我得到了以下摘录:
我想播放上面评论的range
(也贴在这里):
range from: second: 306.6
range to: second: 311.8
total: 5.2 seconds
通过从服务器下载支持请求 header 的片段:Range
。
然后我尝试了下面的代码:
...
let num_channels = 2;
let sample_rate = 44100;
let range_from = 0; // Goal: 306.6 seconds
let range_length = (sample_rate / num_channels) * 5.2; // Goal: 5.2 seconds
let range_to = range_from + (range_length - 1); // "range_to" is inclusive (confirmed)
request.setRequestHeader("Range", "bytes=" + range_from + "-" + range_to);
...
我的问题是:
我需要为变量找到正确的值:range_from
,以便它从秒开始播放:306.6
。
我想知道上面为 range_length
指定的值是否正确,因为可能有用于 header 等的字节,我的意思是:headers
+ 数据
。
这里是我目前的代码:
window.AudioContext = window.AudioContext || window.webkitAudioContext; // necessary for iPhone (maybe others). Could change a near future.
const URL = 'https://www.tophtml.com/snl/15.mp3';
const context = new AudioContext();
window.addEventListener('load', function() {
const button_option_1 = document.querySelector('.button_option_1');
const button_option_1_play = document.querySelector('.button_option_1_play');
button_option_1_play.disabled = true;
button_option_1.addEventListener('click', async function() {
let time_start, duration;
let buffer;
log('...', false);
button_option_1_play.disabled = true;
button_option_1_play.onclick = () => playBuffer(buffer);
//---
time_start = new Date().getTime();
let arrayBuffer = await fetch(URL);
// download complete
duration = sprintf('%.2fs', (new Date().getTime()-time_start)/1000);
log(sprintf('P2. Delay: +%s for download. Wait...', duration));
//---
time_start = new Date().getTime();
let audioBuffer = await decodeAudioData(context, arrayBuffer);
// decoding complete
duration = sprintf('%.2fs', (new Date().getTime()-time_start)/1000);
log(sprintf('P3. Delay: +%s for decoding.', duration));
//---
button_option_1_play.disabled = false;
buffer = audioBuffer;
button_option_1_play.click();
});
});
function playBuffer(buffer, from, duration) {
const source = context.createBufferSource(); // type of "source": "AudioBufferSourceNode"
source.buffer = buffer;
source.connect(context.destination);
source.start(context.currentTime, from, duration);
}
function log(text, append = true) {
let log = document.querySelector('.log');
if (!append)
log.innerHTML = '';
let entry = document.createElement('div');
entry.innerHTML = text;
log.appendChild(entry);
}
function decodeAudioData(context, arrayBuffer) {
return new Promise(async (resolve, reject) => {
if (false) {}
else if (context.decodeAudioData.length == 1) {
// console.log('decodeAudioData / Way 1');
let audioBuffer = await context.decodeAudioData(arrayBuffer);
resolve(audioBuffer);
}
else if (context.decodeAudioData.length == 2) {
// necessary for iPhone (Safari, Chrome) and Mac (Safari). Could change a near future.
// console.log('decodeAudioData / Way 2');
context.decodeAudioData(arrayBuffer, function onSuccess(audioBuffer) {
resolve(audioBuffer);
});
}
});
}
function fetch(url) {
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
let num_channels = 2;
let sample_rate = 44100;
let range_from = 0; // Goal: 306.6 seconds
let range_length = (sample_rate / num_channels) * 5.2; // Goal: 5.2 seconds
let range_to = range_from + (range_length - 1); // "range_to" is inclusive (confirmed)
request.setRequestHeader("Range", "bytes=" + range_from + "-" + range_to);
request.onload = function() {
let arrayBuffer = request.response;
let byteArray = new Uint8Array(arrayBuffer);
// console.log(Array.from(byteArray)); // just logging info
resolve(arrayBuffer);
}
request.send();
});
}
.log {
display: inline-block;
font-family: "Courier New", Courier, monospace;
font-size: 13px;
margin-top: 10px;
padding: 4px;
background-color: #d4e4ff;
}
.divider {
border-top: 1px solid #ccc;
margin: 10px 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/sprintf/1.1.1/sprintf.min.js"></script>
<button class="button_option_1">Option 1</button>
<button class="button_option_1_play">Play</button><br />
<div class="log">[empty]</div>
这里有对应的CodePen.io
:
https://codepen.io/anon/pen/RYXKmP
能否请您为 range_from
提供正确的值并将其用于 CodePen.io
上的 fork 代码?
相关问题:https://engineering.stackexchange.com/questions/23929
[编辑 1]
这是一个更简单的 CodePen.io
:https://codepen.io/anon/pen/YJKVde ,它的重点是检查浏览器在给定随机位置的情况下移动到下一个有效帧的能力。
在我做的一个快速实验中,使用 { Windows 10, Android, iPhone } x { Native browser, Chrome, Firefox }
的组合,上面右边的代码只适用于:{ (Windows 10,Chrome),(Android,Chrome),(Android, native 浏览器)
。
遗憾的是它不起作用:
{ (iPhone, Safari), (iPhone, Chrome), (Windows 10, Firefox), (Android, Firefox) }
有没有办法可以向浏览器开发者提出请求,让他们关注这个问题?
Google Chrome
在 Windows 10
和 Android
上表现非常出色。
如果其他浏览器也这样做会很有趣。
谢谢!
最佳答案
帧长(秒)= 帧样本/采样率,即 38.28 帧/秒。
帧长度(字节)= 144*比特率/采样率
因此,您的 fetch() 现在应该可以工作了(我也更改了范围长度):
function fetch(url) {
return new Promise((resolve, reject) => {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.responseType = 'arraybuffer';
let num_channels = 2;
let bitrate = 192000;
let sample_rate = 44100;
let byte_per_sec = 144 * (bitrate/sample_rate) * 38.28;
let range_from = Math.floor(byte_per_sec * 306.6);
let range_length = Math.floor(byte_per_sec * 5.2);
let range_to = range_from + (range_length - 1);
request.setRequestHeader("Range", "bytes=" + range_from + "-" + range_to);
request.onload = function() {
let arrayBuffer = request.response;
let byteArray = new Uint8Array(arrayBuffer);
//******************
for ( let i = 0; i < byteArray.length; i += 1 ) {
if (( byteArray[i] === 0b11111111 ) && ( byteArray[ i + 1 ] & 0b11110000 ) === 0b11110000 ){
log('we have a winner! Frame header at:'+i, true);
console.log((parseInt(byteArray[i], 10)).toString(2)); //frame header 4 bytes
console.log((parseInt(byteArray[i+1], 10)).toString(2));
console.log((parseInt(byteArray[i+2], 10)).toString(2));
console.log((parseInt(byteArray[i+3], 10)).toString(2));
resolve(arrayBuffer.slice(i));
break;
}
}
//******************
}
request.send();
});
}
编辑我添加了基本的帧头搜索,我的天哪,连老狐狸都吃掉了。对于稳定的解决方案,您必须解析文件头以获取元数据,并将其与帧头数据进行比较。并在找不到 header 时执行某些操作并且... ...
关于javascript - 从 url 中提取音频片段并使用纯 Web Audio API 播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52513425/
我正在做一个业余爱好项目,使用 Ruby、PHP 或 Java 来抓取 ASP.net 网站的内容。例如,如果网站 url“www.myaspnet.com/home.aspx”。我想从 home.a
如果我有这些字符串: mystrings <- c("X2/D2/F4", "X10/D9/F4", "X3/D22/F4",
我有以下数据集 > head(names$SAMPLE_ID) [1] "Bacteria|Proteobacteria|Gammaproteobacteria|Pseudomonadales|Mor
设置: 3个域类A,B和C。A和B在插件中。 C在依赖于此插件的应用程序中。 class A{ B b static mapping = { b fetch: 'joi
我不知道如何提取 XML 文件中的开始标记元素名称。我很接近〜意味着没有错误,我正在获取标签名称,但我正在获取标签名称加上信息。我得到的是: {http://www.publishing.org}au
我有一个字符串 x <- "Name of the Student? Michael Sneider" 我想从中提取“Michael Sneider”。 我用过: str_extract_all(x,
我有一个如下所示的文本文件: [* content I want *] [ more content ] 我想读取该文件并能够提取我想要的内容。我能做的最好的事情如下,但它会返回 [更多内容] 请注意
假设我有一个项目集合 $collection = array( 'item1' => array( 'post' => $post, 'ca
我正在寻找一种过滤文本文件的方法。我有许多文件夹名称,其中包含许多文本文件,文本文件有几个没有人员,每个人员有 10 个群集/组(我在这里只显示了 3 个)。但是每个组/簇可能包含几个原语(我在这里展
我已经编写了一个从某个网页中提取网址的代码,我面临的问题是它不会以网页上相同的方式提取网址,我的意思是如果该网址位于某些网页中法语,它不会按原样提取它。我该如何解决这个问题? import reque
如何在 C# 中提取 ZipFile?(ZipFile 是包含文件和目录) 最佳答案 为此使用工具。类似于 SharpZip .据我所知 - .NET 不支持开箱即用的 ZIP 文件。 来自 here
我有一个表达: [training_width]:lofmimics 我要提取[]之间的内容,在上面的例子中我要 training_width 我试过以下方法: QRegularExpression
我正在尝试创建一个 Bash 脚本,该脚本将从命令行给出的最后一个参数提取到一个变量中以供其他地方使用。这是我正在处理的脚本: #!/bin/bash # compact - archive and
我正在寻找一个 JavaScript 函数/正则表达式来从 URI 中提取 *.com...(在客户端完成) 它应该适用于以下情况: siphone.com = siphone.com qwr.sip
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
编辑:添加了实际的 JSON 对象和代码以供审查 我有这种格式的 JSON(只是这种层次结构,假设 JSON 正常工作) {u'kind': u'calendar#events', u'default
我已经编写了代码来使用 BeautifulSoup 提取一本书的 url 和标题来自页面。 但它并没有在 > 之间提取惊人的 super 科学故事 1930 年 4 月这本书的名字。和 标签。 如何提
使用 Java,我想提取美元符号 $ 之间的单词。 例如: String = " this is first attribute $color$. this is the second attribu
您好,我正在尝试找到一种方法来确定字符串中的常量,然后提取该常量左侧的一定数量的字符。 例如-我有一个 .txt 文件,在那个文件的某处有数字 00nnn 数字的例子是 00234 00765 ...
php读取zip文件(删除文件,提取文件,增加文件)实例 从zip压缩文件中提取文件 复制代码 代码如下: <?php /* php 从zip压缩文件
我是一名优秀的程序员,十分优秀!