我試圖使用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.
謝謝。響應表單提交播放聲音,因此在用戶交互之後。 用戶提交表單,通過AJAX處理,然後AJAX響應設置一個變量,然後用它來播放聲音。但這似乎並不奏效。在用戶發起的AJAX請求不被用戶啓動後,iPhone是否考慮'播放聲音'? – 2012-08-06 10:53:32