javascript – 为什么Safari或Firefox不能从MediaElementSource处理音频数据?

前端之家收集整理的这篇文章主要介绍了javascript – 为什么Safari或Firefox不能从MediaElementSource处理音频数据?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Neither Safari or Firefox are able to process audio data from a MediaElementSource using the Web Audio API.
var audioContext,audioProcess,audioSource,result = document.createElement('h3'),output = document.createElement('span'),mp3 = '//www.jonathancoulton.com/wp-content/uploads/encodes/Smoking_Monkey/mp3/09_First_of_May_mp3_3a69021.mp3',ogg = '//upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg',gotData = false,data,audio = new Audio();
 
function connect() {
  audioContext = window.AudioContext ? new AudioContext() : new webkitAudioContext(),audioSource  = audioContext.createMediaElementSource( audio ),audioScript  = audioContext.createScriptProcessor( 2048 );
 
  audioSource.connect( audioScript );
  audioSource.connect( audioContext.destination );
  audioScript.connect( audioContext.destination );
  audioScript.addEventListener('audioprocess',function(e){
    if ((data = e.inputBuffer.getChannelData(0)[0]*3)) {
      output.innerHTML = Math.abs(data).toFixed(3);
      if (!gotData) gotData = true;
    }
  },false);
}
 
(function setup(){
  audio.volume = 1/3;
  audio.controls = true;
  audio.autoplay = true;
  audio.src = audio.canPlayType('audio/mpeg') ? mp3 : ogg;
  audio.addEventListener('canplay',connect);
  result.innerHTML = 'Channel Data: ';
  output.innerHTML = '0.000';
  document.body.appendChild(result).appendChild(output);
  document.body.appendChild(audio);
})();

有没有计划在不久的将来补丁?还是有一些解决方案仍然会为用户提供音频控制?

对于苹果来说,这个可以在WebKit Nightlies中修复的东西,或者我们必须等到Safari 8.0发布才能获得HTML5< audio>使用Web Audio API播放好吗? Web Audio API已经存在于Safari中,因为至少6.0版本,我最初在Safari 7.0发布之前发布了这个问题.有没有这个原因呢?会不会修复?

对于Mozilla来说,我知道您仍然在使用旧的Audio Data API进行切换,但这是Web Audio实现的一个已知问题,在下一次发布Firefox之前将会被修复?

解决方法

这个答案几乎完全是从我的答案引用到一个相关的问题: Firefox 25 and AudioContext createJavaScriptNote not a function

如果媒体遵守Same-Origin Policy,Firefox将支持MediaElementSource,但是尝试使用来自远端的媒体时,Firefox不会产生任何错误.

该规范并不是真正具体的(双关语),但我被告知这是一个预期的行为,问题实际上是与Chrome …这是Blink实现(Chrome,Opera)need to be updated to require CORS.

MediaElementSource Node and Cross-Origin Media Resources

From: Robert O'Callahan <robert@ocallahan.org>
Date: Tue,23 Jul 2013 16:30:00 +1200
To: "public-audio@w3.org" <public-audio@w3.org>

HTML media elements can play media resources from any origin. When an
element plays a media resource from an origin different from the page’s
origin,we must prevent page script from being able to read the contents of
the media (e.g. extract video frames or audio samples). In particular we
should prevent ScriptProcessorNodes from getting access to the media’s
audio samples. We should also information about samples leaking in other
ways (e.g. timing channel attacks). Currently the Web Audio spec says
nothing about this.

I think we should solve this by preventing any non-same-origin data from
entering Web Audio. That will minimize the attack surface and the impact on
Web Audio.

My proposal is to make MediaElementAudioSourceNode convert data coming from
a non-same origin stream to silence.

如果这个建议成为规范,开发人员几乎不可能知道为什么他的MediaElementSource不工作.如现在所示,在< audio>上调用createMediaElementSource()元素在Firefox 26 actually stops the <audio> controls from working at all并且没有抛出任何错误.

您可以使用远程来源的音频/视频数据做什么危险的事情?一般的想法是,没有将相同原则策略应用于MediaElementSource节点,一些恶意的JavaScript可以访问只有用户应该访问(会话,vpn,本地服务器,网络驱动器)并发送其内容的媒体 – 或某些表示的 – 攻击者.

默认情况下,HTML5媒体元素没有这些限制.您可以通过使用< audio>,< img>或< video>在所有浏览器中包括远程媒体.元素.只有当您想要操作或提取相同原始策略所起的这些远程资源的数据时.

[It’s] for the same reason that you cannot dump image data cross-origin via <canvas>: media may contain sensitive information and therefore allowing rogue sites to dump and re-route content is a security issue. – 07005

猜你在找的JavaScript相关文章