2012-08-03 90 views
0

我试图使用SoundManager2作为iPhone Web应用程序的一部分,在使用jQuery提交表单后播放声音。播放的特定声音取决于结果,因此生成的页面会设置一个变量来标识要播放的声音文件。iPhone上的SoundManager2 - 声音没有在jQuery上播放

这一切都可以在桌面上顺利运行,但在iPhone上运行时会失败。

在iPhone上调试控制台显示它尝试...

1124: SMSound.play(): "ValidSound" is loading - attempting to play... 
1124: SMSound.play(): "ValidSound" is starting to play 
1124: setPosition(0): delaying, sound not ready 
1124: HTML5::loadstart: ValidSound 
1124: HTML5::suspend: ValidSound 

...但没有声音每戏剧。

有什么想法可能会导致此失败?我已经包括了主要页面的代码,那就是使用jQuery返回了 '回应' 页面的例子:

主要

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<script src="http://code.jquery.com/jquery-latest.js"></script> 
<script type="text/javascript" src="sm/script/soundmanager2.js"></script> 
<script> 
soundManager.setup({ 

    url: 'sm/swf/', 
    useHTML5Audio: true, 

    onready: function() { 

    soundManager.createSound({ 
     id: 'ValidSound', 
     useHTML5Audio: true, 
     preferFlash: false, 
     url: 'http://www.example.com/example sound.mp3' 
    }); 

    } 

}); 
</script> 

</head> 

<body onload="window.top.scrollTo(0,1);"> 

<div class="scannerform"> 
    <form id="scanner" name="scanner" method="post" action="ok.php"> 
    <input type="text" name="scan" id="scan" /> 
    <input type="submit" name="button" id="button" value="Submit" /> 
    </form> 
</div> 

<div class="loadingDiv" id="loadingDiv">Loading...</div> 
<div class="wrapper" id="wrapper"><div name="data" id="data"></div></div> 

<script> 
$('#loadingDiv') 
    .hide() 
    .ajaxStart(function() { 
     $(this).show(); 
    }) 
    .ajaxStop(function() { 
     $(this).hide(); 
    }) 
; 

$("#scanner").submit(function(event) { 

    $("#loadingImage").show(); 

    event.preventDefault(); 
    var $form = $(this), 
     scanned_data = $form.find('input[name="scan"]').val(), 
     url = $form.attr('action'); 

    $.post(url, { scanned_data: scanned_data }, 
     function(data) { 
      var content = $(data); 
      $("#data").empty().append(content); 
      $("#loadingImage").hide(); 
      if (playSound) playSound(); 
      soundManager.play(SoundToPlay); 
     } 
    ); 
    }); 
    </script> 
</body> 
</html> 

响应

<script> 
function playSound() 
{ 
    SoundToPlay="ValidSound"; 
} 
</script> 

<div id="example">Hello World</div> 

谢谢你的帮助!

J.

回答

9

你知道iPhone Safari有声音限制吗?基本上它不会播放声音,没有用户以某种方式与页面交互(通过触摸屏)。 因此,忘记通过“onLoad”或任何其他事件播放的自动声音。

虽然SoundManager2和我的iPhone虽然有时会发现有点棘手..我很确定它会在我的应用程序中手指点击至少1次后自动播放声音(甚至是几个)(例如,关闭初始HTML弹出窗口(基本))

因此,检查这导致至少...

托马斯

资源:

http://buildingwebapps.blogspot.com/2012/04/state-of-html5-audio-in-mobile-safari.html

http://blog.logicking.com/2012/03/cross-platform-sounds-for-html5-games.html

+0

谢谢。响应表单提交播放声音,因此在用户交互之后。 用户提交表单,通过AJAX处理,然后AJAX响应设置一个变量,然后用它来播放声音。但这似乎并不奏效。在用户发起的AJAX请求不被用户启动后,iPhone是否考虑'播放声音'? – 2012-08-06 10:53:32