1

我一直在寻找一个如何在 iOS 中实现 onclick 音频播放的简单示例,但我似乎找不到。我知道由于数据使用方面的考虑,Apple 已禁用自动播放 - 但我想做的是在用户单击图像时播放音频 - 我绝不是 javascript 的大师,我找不到应用于我的代码的简单示例,因此我希望有人可以演示您如何做到这一点。

我在下面输入了我的代码的简化版本。我已经在使用 javascript 函数来确定单击图像时是否需要提供 ogg 文件或 mp3 文件。我还使用了一个 JQuery 插件,它可以在单击时放大图像,另一个控制页面上几个标题的一些字体大小,另一个控制 chromeframe。我的头部有两个单独的脚本标签,因为我不知道如何让它们全部在一个标签中工作。我知道这很草率,我打算清理它,但在我这样做之前,我想弄清楚音频问题。

无论如何,我认为因为我已经设置了音频以在单击图像时播放它可以在 iOS 中工作,但它不是,我不确定我应该如何调整代码。如果有人有时间提供一个简单的例子作为如何做到这一点的起点,我会非常感激。

<!DOCTYPE html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="jquery/jquery.zoomooz.min.js"></script>
<script src="jquery/jquery.fittext.js.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js"></script>
<script>

// Mouseover/ Click sound effect- by JavaScript Kit (www.javascriptkit.com)
// Visit JavaScript Kit at http://www.javascriptkit.com/ for full source code

//** Usage: Instantiate script by calling: var uniquevar=createsoundbite("soundfile1",       "fallbackfile2", "fallebacksound3", etc)
//** Call: uniquevar.playclip() to play sound

var html5_audiotypes={ //define list of audio file extensions and their associated audio    types. Add to it if your specified audio file isn't on this list:
"mp3": "audio/mpeg",
"mp4": "audio/mp4",
"ogg": "audio/ogg",
"wav": "audio/wav"
 }

 function createsoundbite(sound){
var html5audio=document.createElement('audio')
if (html5audio.canPlayType){ //check support for HTML5 audio
    for (var i=0; i<arguments.length; i++){
        var sourceel=document.createElement('source')
        sourceel.setAttribute('src', arguments[i])
        if (arguments[i].match(/\.(\w+)$/i))
            sourceel.setAttribute('type', html5_audiotypes[RegExp.$1])
        html5audio.appendChild(sourceel)
    }
    html5audio.load()
    html5audio.playclip=function(){
        html5audio.pause()
        html5audio.currentTime=0
        html5audio.play()
    }
    return html5audio
}
else{
    return {playclip:function(){throw new Error("Unfortunately your browser doesn't support HTML5 audio")}}
}
 }

 //Initialize two sound clips with 1 fallback file each:

 var mouseoversound=createsoundbite("example1.ogg", "example1.mp3")
 var clicksound=createsoundbite("example2.ogg", "example2.mp3")

<script type="text/javascript">


$(document).ready(function(){
            if($(window).width()>0){
                $("li.fittext").fitText(1.68);
                $("#footerwrap p").fitText(5);
            };
        });
        $(".zoomTarget").click(function(evt) {
                evt.stopPropagation();
                evt.preventDefault();
                $(this).zoomTo({debug:true});
                $(this).zoomTo({easing:'ease-in'});
            });

            $(window).click(function(evt) {
                evt.stopPropagation();
                $("body").zoomTo({targetsize:1.0});
            });

            // for iPhone
            $("#container").click(function(evt) {
                evt.stopPropagation();
                $("body").zoomTo({targetsize:1.0});
            });

            $("body").zoomTo({targetsize:1.0});


        $(window).load(function() {
            CFInstall.check({
             mode: "overlay",
             destination: "http://www.waikiki.com"
           });
           });



</script>
</head>

<body>

<div id="wrapper">

<img onclick="clicksound.playclip()" class="zoomTarget" data-targetsize="1.0" data-duration="10000" easing="linear" src="images/image.jpg"/>
</div><!--wrapper--> 

</body>                         
4

0 回答 0