【问题标题】:HTML5 mobile: Play only one side of audio from a video stream on iOSHTML5 移动设备:在 iOS 上仅播放来自视频流的一侧音频
【发布时间】:2015-06-19 19:19:13
【问题描述】:

我正在尝试在 iOS 上播放视频,同时只收听立体声音频的一侧。

下面的代码在桌面 Chrome 上运行良好,但在 8.3 iOS 的 iPhone5 上的 Safari 上运行不正常。

var AudioContext = window.webkitAudioContext;
  var audioCtx = new AudioContext();
  var splitter = audioCtx.createChannelSplitter(2);
  var merger = audioCtx.createChannelMerger(1);

  source = audioCtx.createMediaElementSource(video);
  source.connect(splitter);
  splitter.connect(merger, 0); 
  merger.connect(audioCtx.destination);

'vid​​eo' 是对 DOM 视频元素的引用。

任何帮助将不胜感激

非常感谢

萨尔

【问题讨论】:

  • 你能在IOS上看看是window.AudioContext还是window.webkitAudioContext被加载了?
  • Safari 使用 window.webkitAudioContext

标签: javascript ios html audio web-audio-api


【解决方案1】:

这是一个 polyfill,用于检查网络音频是否存在以及是否使用-webkit

//borrowed from underscore.js
function isUndef(val){
    return val === void 0;
}

if (isUndef(window.AudioContext)){
    window.AudioContext = window.webkitAudioContext;
} 

if (!isUndef(AudioContext)){
    audioContext = new AudioContext();
} else {
    throw new Error("Web Audio is not supported in this browser");
}

还有一个类似的 IOS 错误 here 可能会有所帮助。

【讨论】:

    【解决方案2】:

    检查设备使用哪种音频上下文而不是使用||。例如:

    var AudioContext 
    if('webkitAudioContext' in window) {
       AudioContext = new webkitAudioContext();
    }
    else{
       AudioContext = new AudioContext ();
    }
    

    【讨论】:

    • 谢谢,我知道,但他正在寻找跨浏览器的兼容性,所以两者都应该支持
    • 谢谢。更新了问题,因为我只关心 iOS。
    猜你喜欢
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    相关资源
    最近更新 更多