2
votes

I'm making a simple counter, that has sound at the end. It works perfectly on desktop Chrome, but on Android's Chrome (ICS if that matters) it has a strange bug.

Here is the code

<!DOCTYPE html>
<html lang="hu-HU">
<head>
<meta charset=utf-8>
    <title>-</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript"> 
        var snd;
        var buzzer;
        // refresh the counter, called every sec
        function refresh(time) {
            second = time % 60;
            $('#second').html(second < 10 ? '0' + second : second);
            if(second>55) 
                snd.play();             
        }

        $(function() {          
                snd = new Audio('ding.wav');                                
                var clock = 52;
                refresh(clock);
                window.setInterval(
                    function() {                        
                        clock++;
                        refresh(clock);
                    }, 
                    1000
                );
            }
        );

    </script>


    </style>
</head>
<body>

    <div id="second"></div>


<a href="#" onclick="doit();">Play</a>
<script>
    function doit() {
        snd.play();
    }
</script>
</body>
</html>

In the mobile Chrome, normally the sound doesn't play, which is strange as it should be. But if I press the Play link once it works like a charm. Any idea how to fix this?

1

1 Answers

2
votes

Mobile browsers prevent JavaScript from playing back sounds without user-initiated events. This is the same behavior as MobileSafari.

See these relevant SO questions: