Load sound file paused and then start to play after a number of seconds
I'm using setTimeout to play an audio file after 8 seconds:
setTimeout(function() {
document.getElementById('delayed_play').style.display = 'block';
}, 8 * 1000);
<div id="delayed_play">
The problem is that I need it to start exactly after those 8 seconds, and instead the file starts loading and takes a different amount of time depending the connection speed.
Is there a way to make the file load paused and tell it to start at 8 seconds?
This is the audio file with the rest of the script:
<script type="text/javascript">
document.getElementById('delayed_play').style.display = 'none';
</script>
<object height="0" width="40%">
<embed allowscriptaccess="always" height="0" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F14151193%3Fsecret_token%3Ds-OVmfN&show_comments=false&auto_play=true&color=ff7700" type="application/x-s开发者_运维知识库hockwave-flash" width="100%"></embed>
</object>
Thanks
I am not familiar with the API, but here is what I would try:
- Load the player with the page, but disable autoplay (there should be a parameter in the URL, I guess)
- Set a timer for the page load event that executes a function that calls the appropriate API function to start the player.
This requires knowledge of the API which should have a documentation on the website.
The documentation on the player can be found at http://developers.soundcloud.com/docs/widget.
The SoundCloud Player is customizable through URL parameters and scriptable through a JavaScript API.
The parameter of interest is auto_play which can disable automatic playing of the sound.
You can manipulate the player as described in the 'Widget methods' section:
api_play() Player starts playing either from 0 or from the last paused track position. In the case the widget contains multiple tracks it will start playing the first track.
You can hook your code to various events described in the 'Event types' section.
onPlayerReady Fired when the widget has loaded its data and is ready to accept external calls. Triggered only once per widget instance.
onMediaBuffering Fired when the widget is still buffering. data (second argument) contains property percent. It also means you can't seek in the track fully yet
You have to test the buffering behaviour to see how to set up your event listeners. You might also need to use the onMediaDoneBuffering event.
精彩评论