Is it possible to use HTML5 to make a sound when a user hovers over one of my list item buttons? I'd like to play a very short click/chirp sound one time when a user hovers over a navigation button. I realize it won't be compatible on every browser or device and that's OK as long as it degrades gracefully. Would it be better to use Flash for this for some reason?
Edit
Also, if it's possible to do this I'd be interested in some sample code, including the javascript (if javascript is needed). I'm OK with HTML but not too handy with Javascript.
I’ve not done anything like this, but it should be possible. E.g.
<audio id="myaudio" src="myaudio.mp3"></audio>
<button onmouseover="document.getElementById('myaudio').play()">Hover me</button>
I’m not familiar with Flash, so I’m not sure if you can use JavaScript to get a Flash file to play.
This is an old answer, nowdays you can just use HTML5 really.
I'd not recommend a sound on a hover. Users can get annoyed very easily (I would).
In any case, here's how, and it doesn't need HTML5:
1) Have a simple javascript to play the sound
<script>
function EvalSound(soundobj) {
var thissound=document.getElementById(soundobj);
try {
thissound.Play(); //Quicktime, Windows Media Player, etc.
}
catch (e) {
thissound.DoPlay(); //Real Player
}
}
</script>
2) Make an invisible embed with the sound
<embed src="mysound.wav"
autostart=false
width=0
height=0
id="mySound"
enablejavascript="true" />
3) Make it play on hover
<ul id="myList">
<li onHover="EvalSound('mySound')">Foo</li>
<li onHover="EvalSound('mySound')">Bar</li>
</ul>
Alternatively, attach event with jQuery:
<script>
$(document).ready(function() {
$('#myList').hover(EvalSound('mySound'));
});
</script>
Edited because realplayer uses DoPlay() instead of Play().
Related
I study graphics as one of my subjects at school. Currently, my task is to make a website for a party so I have started to create the homepage. The home page has four balloons on it and when you roll over them they pop. But, what I also want them to do is to play a balloon popping sound effect when you roll over them and they pop.
However, I cant work out what the HTML would be to achieve this.
The website: partybox.businesscatalyst.com
This cannot be done efficiently using only HTML, as it will require the use of <audio> as in
<audio autoplay>
<source src="file.wav" type="audio/wav"><!--file.wav will replace your sound file with the pop effect-->
</audio>
However, the <audio> still required to be started (in case of controls) or dynamically created (in case of autoplay) - which will still require JavaScript.
A simpler JavaScript method would be like - on your onclick or onhover function, simply add this JavaScript Code:
var sound = new Audio("file.wav"); //file.wav will replace your sound file with the pop effect
sound.play(); //Play the sound
How can I listen to the events play/stop/pause of a HTML5 video?
I tried with the following code:
$("video").on('play',function(){
//my code here
});
but it does not work.
Alternatively I would be fine also intercept the click that stop and start the video (in fact I would prefer), but even this code not works:
$('video').on('click', function(event) {
//my code here
});
I think because often above the video element is placed a div with associated events start and stop, but I do not know how to select via jQuery or Javascript:
P.S. This code should work on pages that are composed dynamically / not generated by me, so I can not refer the elements indicating their ID or a specific class.
UPDATE
I did not realize that the page on which I was testing had a video inside an iframe.
The syntax is best suited to my needs:
doc.querySelector("video").addEventListener('play', function(e) {
//my code here
}, true);
See here:
https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Media_events
So... :
$("video")[0].onplay = function () {
// Do play stuff here
};
Hope that helps!
Your first attempt should work. But you need to make sure that jQuery is actually finding video elements. You say that the page is generated dynamically, so perhaps it's possible that your code is running before the video elements are added. This would also be the case if your script is higher up in the document than the video elements (e.g., in the <head>).
Try placing the following code immediately before the code you have there and check the output:
console.log($("video"));
If the output is an empty array, then you aren't finding any video elements. Try placing your script at the very end of the page or inside a DOMContentLoaded event. Or, if another script is generating the video elements, make sure your script runs after that one.
A few other notes on your question:
There is no stop event. Here's a list of media events.
I don't think the click event is what you want. That will fire no matter where on the video the user clicks: play button, volume control, etc.
The video being placed inside a div shouldn't have any effect on these events. I'm not sure why you'd have start and end events on a div, so maybe I'm not following.
Give this a try!
//Direct
<video id="videoThis" src="blahhh.mp4" onended="yourFunction()"></videoThis>
-or-
//JavaScript (HTML5)
document.querySelector("#videoThis").addEventListener("ended",yourFunction,false);
-or-
//jQuery
$("#videoThis").bind("ended",yourFunction);
I need to play and pause the video player if any part of the video is clicked, so I implemented something like:
$("#promoPlayer").click(function() {
$("#promoPlayer").click(function() {
if(this.paused){
this.play();
} else {
this.pause();
}
});
...but now the controls of the video won't pause/play.
You can see it in action here(http://175.107.134.113:8080/). The video is the second slide on the main carousel at the top.
I suspect I'm now getting 2 onclick events one from my code above and a second on the actual pause/play button.
I don't understand how the actual video controls work, because when I inspect the element, I just see a tag. If I could differentiate the controls, then perhaps I might have figured out a solution by now.
Anyone know how I should have done this, or is my solution ok. If my solutions ok, how do I intercept a click on the control, so that I only have one click event to pause / play?
If you look on the media element home page, they have a big play button, complete with mouseOver, but I can't see how they have done that? Can someone else help?
The controls of the browser's native player are not individual DOM elements so I don't believe there's a way to identify them.
My suggestion would be to create an invisible div which covers the player but not the controls, and bind your logic to that.
Ok, I was being an idiot. I hadn't initialised the MediaElement player:
$(document).ready(function(){
$("#promoPlayer").mediaelementplayer({
features: ['playpause','progress','current','duration','tracks','volume','fullscreen'],
// Hide controls when playing and mouse is not over the video
alwaysShowControls: false
});
});
Which mean't that what I thought was MediaElement, was actually just my browser's native support for the tag...
As expected MediaElement does of course give you such things as being able to click the whole video frame and have the video start for free...
The player should already play/pause on click, but if you want to attach events it's best to do so within the success handler:
$(document).ready(function(){
$("#promoPlayer").mediaelementplayer({
success: function(media, domElement, player) {
$('#somebutton').on('click', function() {
media.play();
});
}
});
});
Does anyone know if html5 video allows objects like buttons, menu, etc connected to the timeline?
Youtube flash player do this: in specific moment, show an object (banner, links, comments) over the video for defined seconds.
thanks
Yes and no,
It's possible to create very interactive video-based presentations using html5 video objects however it requires a lot more than just the video object itself. You can nest video into a canvas object then mess with the actual video image frames, you can overlay any visual html element on top of the video object itself then animate these, you can control the video playback with buttons, click events etc. You can even have the video object control the rest of the page with time-based listeners.
Popcorn.js is really really good and easy to learn, allowing you to do all of what you mentioned.
http://popcornjs.org
http://popcornjs.org/demos
It's not part of the HTML5 video standard, but it's easy to implement manually by wiring up some scripting to the progress event. By looking at the currentTime property of the video object you can decide when to show/hide additional elements.
eg showing an element on top of a video between 1 and 2 seconds into a video:
<video id="v">...</div>
<div id="overlay" style="position: relative; top: -80px;">HELLO</div>
<script type="text/javascript">
var overlay= document.getElementById('overlay');
var video= document.getElementById('v');
video.addEventListener('progress', function() {
var show= video.currentTime>=1 && video.currentTime<2;
overlay.style.visibility= show? 'visible' : 'hidden';
}, false);
</script>
X2TV (www.x2.tv) has a drag and drop studio where you can overlay icons and additional content within the HTML5 layer.
In case you woul like to use a more generic framework https://github.com/nise/vi-two could be interesting for you.
Is there any way, in HTML, to include an animated GIF in an <img> tag, but automatically tell the GIF to not animate? I realize that the user can stop animation by pressing ESC or clicking Stop, but I want the GIFs not to animate at all.
I only want to do this on one specific page, and making separate non-animated versions of the (1500+) GIFs is not feasible. I simply want the GIFs to not animate.
You could use Giffer.
Just include the library in your page:
<script type="text/javascript" src="gifffer.min.js"></script>
Instead of setting src attribute on your image use data-gifffer.
<img data-gifffer="image.gif" />
At the end, call Gifffer() whenever you want. For example:
window.onload = function() {
Gifffer();
}
If you don't want the GIF to move at all you can always edit the .js file to remove the play button.
Not with plain HTML but using PHP with imagecreatefromgif might help you
I don't think calling window.stop() will be a good solution. This would need to be called for every image that is loaded to prevent it from running half way through and stopping. The best solution is to use a library such as GD to create images featuring just the first frame of the animated GIF.
Use ImageMagik, and you can readily convert all 1500 images.
You could use window.stop() in javascript, which should be the equivalent of pressing ESC/clicking stop. However, I'm pretty sure it won't work in all browsers (i.e. IE).