简体   繁体   中英

jQuery to autoplay HTML5 audio

Ok, so let me tell you where I'm at. The audio file displays and loads. you can click play to play it. The jQuery isn't autoplaying the material. I'm having to load in the file based on the href of the link. Code is below. Trying to get this to autoplay.

            $('.voiceLink a', resbox).click(function(e){
                e.preventDefault();
                var link = $(this).prop('href'),
                    audio = $('.audioControl', el.parent());

                audio.find('source').prop('src', link);
                audio.parent().css('display','inline-block');
                audio.find('audio').load();
                setTimeout(audio.find('audio').play(), 100);

                $(window).scrollTop();
            });

resbox = The wrapper box.

The HTML Element:

<div class="audioHide" style="display: none;">
                <div class="audioControl" style="position:relative; top:10px; left:50px; display:inline-block;">
                    <audio controls><source src="#" type="audio/mpeg">
                        Not Supported
                    </audio>
                </div>
                </div>

Example Link:

<a target="_blank" href="http://relatientsounds.s3-website-us-east-1.amazonaws.com/recordings/2456266.mp3">2456266</a>

Edit

try this

audio.find('source').prop('src', link);
audio.parent().css('display','inline-block');
audio.find('audio').load();
//this line
audio.find('audio')[0].play();

Demo http://jsfiddle.net/m25kfy57/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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