简体   繁体   English

如何为IOS 9初始化SoundJS

[英]How to initialise SoundJS for IOS 9

We've been using the CreateJS suite for a while now, but have just realised that our audio is not working on IOS9. 我们一直在使用CreateJS套件,但刚刚意识到我们的音频不适用于IOS9。 Unfortunately we only have one IOS9 test device, running IOS9.2.4, but are getting mixed results with audio playback. 不幸的是,我们只有一台运行IOS9.2.4的IOS9测试设备,但是音频播放结果却是混合的。

The rough process I'm using is; 我正在使用的粗糙过程是;

  • Preload all assets (scripts/images/audio) via PreloadJS 通过PreloadJS预加载所有资产(脚本/图像/音频)
  • Construct an initial splash screen in EaselJS, including a start button 在EaselJS中构建初始启动画面,包括一个开始按钮
  • Continue with the main content 继续主要内容

It would be advantageous to be able to preload all audio before presenting that splash screen. 能够在呈现该闪屏之前预加载所有音频将是有利的。 The splash screen was added initially to allow audio to play on mobile Safari, with an empty sound played on click. 最初添加启动画面以允许音频在移动Safari上播放,点击时播放空声。 This does of course work for IOS7/8, but not for 9. 这当然适用于IOS7 / 8,但不适用于9。

I've created this test case on codepen as an attempt to track down the issue and try some options. 我在codepen上创建了这个测试用例,试图找出问题并尝试一些选项。

Codepen sample Codepen样本

HTML HTML

<p id="status">Hello World</p>
<canvas id="canvas" width="200" height="200"></canvas>

JS JS

var canvas, stage, rect;

function init() {
  canvas = document.getElementById('canvas');
  canvas.style.background = "rgb(10,10,30)";
  stage = new createjs.Stage("canvas");

  createjs.Touch.enable(stage);

  rect = new createjs.Shape();
  rect.graphics.f("#f00").dr(50,75,100, 50);
  rect.on("mousedown", handleStart, null, true);
  rect.on("touchend", handleStart, null, true);
  //rect.on("click", handleStart, null, true);
  stage.addChild(rect);
  stage.update();

  $('#status').text("Touch to Start");
  createjs.Sound.initializeDefaultPlugins();
  //createjs.Sound.alternateExtensions = ["ogg"];
  createjs.Sound.registerSound("https://www.freesound.org/data/previews/66/66136_606715-lq.mp3", "ding1");

}

function handleStart(event) {
  createjs.WebAudioPlugin.playEmptySound();

  $('#status').text("Touch to Play");

  rect.graphics._fill.style = '#0f0';
  rect.removeAllEventListeners();
  rect.on('click', handlePlay);
  stage.update();
}

function handlePlay(){
  createjs.Sound.play("ding1");
  $('#status').text("Playing");
}

init();

Apologies for the lack of ogg version, was struggling to get test files to load x-domain. 对于缺少ogg版本的道歉,正在努力让测试文件加载到x-domain。

With this, audio partially works for us on IOS9. 有了这个,音频在IOS9上部分适用于我们。 If we leave clicking the red rectangle (figure start button) and leave it ~20 seconds, then click the green button no audio plays. 如果我们点击红色矩形(图形开始按钮)并保持约20秒,则单击绿色按钮不播放音频。 If we click it immediately, audio plays fine. 如果我们立即点击它,音频播放正常。

I have been reviewing this bug/thread and attempting to follow Grant's suggestions. 我一直在审查这个错误/线程,并试图遵循格兰特的建议。 I gather SoundJS v0.6.2 now automatically attempts to play the empty sound appropriate when plugins are initialized, however moving the initializeDefaultPlugins and registerSounds calls into the handleStart function appears to make no difference. 我收集SoundJS v0.6.2现在自动尝试播放插件初始化时适当的空声,但是将initializeDefaultPlugins和registerSounds调用移动到handleStart函数似乎没有任何区别。 If I'm understanding the issue correctly, calling the WebAudioPlugin.playEmptySound method should be sufficient? 如果我正确理解了这个问题,那么调用WebAudioPlugin.playEmptySound方法应该足够了吗?

Have also been looking at the event binding, trying mousedown/touchend instead of click, but the result is the same with the 20 second wait. 一直在看事件绑定,尝试mousedown / touchend而不是点击,但结果与20秒等待相同。 Event also appears to fire twice, although I could probably dig deeper into that if I could get it to work correctly. 事件似乎也会发生两次,尽管我可能会深入研究,如果我能让它正常工作的话。

I'm aware of the Mobile Safe Approach article aimed at this issue, but the need for a namespace at this level would mean a substantial rewrite of our existing content. 我知道针对此问题的移动安全方法文章,但是在此级别需要命名空间意味着对我们现有内容的重大改写。 Could someone perhaps advise if it is completely necessary to take this approach? 有人可能会建议是否完全有必要采取这种方法? I'm under the impression it should be feasible by correctly playing some empty audio within that initial handler. 我认为通过在初始处理程序中正确播放一些空音频应该是可行的。

Can't actually get the main project to this point, but if I can get a working example perhaps I'll be a step closer. 实际上无法获得主要项目到这一点,但如果我能得到一个有效的例子,也许我会更近一步。

Any thoughts would be appreciated! 任何想法将不胜感激!

The Mobile-safe tutorial is not really relevant anymore since the updates in 0.6.2. 自从0.6.2中的更新以来,移动安全教程不再具有真正的相关性。 It will likely be updated or removed in the near future. 它可能会在不久的将来更新或删除。

  • You should never need to initializeDefaultPlugins() , unless you want to act on the result (ie, check the activePlugin before doing something). 你永远不需要initializeDefaultPlugins() DefaultFlugins initializeDefaultPlugins() ,除非你想对结果采取行动(即,在做某事之前检查activePlugin)。 This method fires automatically the first time you try and register a sound. 第一次尝试注册声音时,此方法会自动触发。
  • The playEmptySound is also no longer necessary. playEmptySound也不再需要了。 SoundJS will listen for the first document mousedown/click, and automatically do this in the background. SoundJS将侦听mousedown / click的第一个文件,并在后台自动执行此操作。
  • touch events are not directly propagated from the EaselJS stage, but are instead turned into mouse events (touchstart=mousedown, touchmove=mousemove, touchend=pressup/click) 触摸事件不是直接从EaselJS阶段传播,而是变成鼠标事件(touchstart = mousedown,touchmove = mousemove,touchend = pressup / click)

Based on this, you should be able to play sound once anywhere in the document has been clicked, regardless of whether you listen for it or not. 基于此,无论您是否聆听,您都应该能够在单击文档的任何位置播放声音。

That is not to say that there isn't a bug, just that the steps you are taking shouldn't be necessary. 这并不是说没有错误,只是说你所采取的步骤不是必需的。 I did some testing, and it appears to work in iOS9. 我做了一些测试,它似乎在iOS9中工作。 Here is a simplified fiddle. 这是一个简化的小提琴。 https://jsfiddle.net/lannymcnie/b4k19fwc/ (link updated Dec 3, 2018) https://jsfiddle.net/lannymcnie/b4k19fwc/ (链接更新于2018年12月3日)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM