webkitAudioContext在iOS Safari上创建MediaElementSource不工作

webkitAudioContext createMediaElementSource on iOS Safari not working

本文关键字:MediaElementSource 工作 创建 iOS Safari webkitAudioContext      更新时间:2024-01-01

我想在iPhone上进行实时声音分析。为此我使用webkitAudioContext分析器。

var ctx = new (window.AudioContext || window.webkitAudioContext);
var audioGoodmorning = new Audio('assets/sounds/greeting.m4a');
var audioSrc = ctx.createMediaElementSource(audioGoodmorning);
var analyser = ctx.createAnalyser();
analyser.fftSize = 32;
audioSrc.connect(analyser);
audioSrc.connect(ctx.destination);
var frequencyData = new Uint8Array(analyser.fftSize);
analyser.getByteFrequencyData(frequencyData);

这在Mac上的Chrome中运行良好。它也适用于Safari,当将网站添加到主屏幕时,使用

<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="CHAR">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">

如果不将网站添加到主屏幕,Safari就无法运行。当使用嵌入iOS wkwebview的网站时,它不起作用。这就是我想要实现的。不工作时,frequencyData阵列充满零。

有人经历过这种问题吗?

提前感谢

检查这个小提琴:

https://jsfiddle.net/4b2dvhqy/1/

var audio = new Audio();
audio.loop = true;
audio.autoplay = true;
audio.crossOrigin = "anonymous";
audio.addEventListener('error', function(e) {
  console.log(e);
});
audio.src = "https://greggman.github.io/doodles/sounds/DOCTOR VOX - Level Up.mp3";
audio.play();
audio.controls = true;
document.getElementById("wrapper").append(audio);
audio.addEventListener('canplay', function() {
  var audioSourceNode = audioContext.createMediaElementSource(audio);
  audioSourceNode.connect(analyser);
  analyser.connect(audioContext.destination);
});

它在Safari上运行良好,因为音频内容是在用户点击事件下处理的。

如果没有"用户点击事件",它只能在Chrome或Safari"localhost"上正常工作。

此外,另一个Fiddle可以很好地处理HTML标记音频:

https://jsfiddle.net/cbua1pkn/1/

这里的技巧是当用户点击播放按钮时初始化audioContext!(因此您处于用户事件上下文下)。

根据https://caniuse.com/#search=webaudioSafari仍然不支持createMediaElementSource,所以我认为你在这里运气不好。不幸的是,Safari对WebAudio API的支持不是很好。

相关文章:
  • 没有找到相关文章