Adding sound on clicking a image in HTML 5 - html

I have a image on my html page and i want it to play a sound effect when i click on it.
Here's the image code:
<img src="images/button1.png" width="32" height="32" onclick="alert();">
I want to change the alert box into the sound effect that i load in. How do i do it?

Well, it is not so easy, actually it is difficult to be accomplished ONLY with HTML. See here for more details.
You`ll face a various problems like:
Different browsers have different audio format support.
If a browser
does not support the file format, the audio will not play without a
plug-in.
If the plug-in is not installed on the users' computer, the
audio will not play.
If you convert the file to another formats, it
will still not play in all browsers.
Using A Hyperlink
If a web page includes a hyperlink to a media file, most browsers will use a "helper application" to play the file.
The following code fragment displays a link to an mp3 file. If a user clicks on the link, the browser will launch a helper application to play the file:
Play the sound
The best solution by my opinion is to use a Flash for playing sounds, i.e. make your links as Flash objects/buttons/text and assign sounds to them inside Adobe Flash on hover/click/etc.

Use SoundManager and will free up from a lot of headaches. Even more it will fallback nicely to flash if audio is not supported natively by browser.
However if you don't want to relay on a third party library there is another solution, a little bit daunting and exhausting but functional in every browser supporting the audio tag.
The first thing you need to do is to populate the audio tag with the source content. You can do this with the following lines of code:
<audio id = 'audio'>
<source src="test.mp3" type="audio/mpeg" />
<source src="test.ogg" type="audio/ogg" />
<object class="playerpreview" type="application/x-shockwave-flash"
data="player_mp3_mini.swf" width="200" height="20">
<param name="movie" value="player_mp3_mini.swf" />
<param name="bgcolor" value="#085c68" />
<param name="FlashVars" value="mp3=test.mp3" />
<embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200"
height="20" name="movie" align=""
type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
</embed>
</object>
</audio>
Because there is not a common agreement between different browser vendors regarding the supported audio format we need to include each supported ones. If one browser does not support it, then will skip it and fallback to the next one. If none of them is supported then you can use flash as a fallback. Google AJAX Libraries API will facilitate much our work. Below is the full source code:
<script src="http://www.google.com/jsapi"></script>
<script>google.load("swfobject", "2.2");</script>
<img src="http://www.kafkabrigade.org.uk/wp-content/uploads/2011/07/button-pic.jpg" onclick="playSound();">
<div id="fallback"></div>
<audio id = 'audio'>
<source src="http://www.hellopixel.net/click.mp3" type="audio/mpeg" />
<source src="http://www.hellopixel.net/click.ogg" type="audio/ogg" />
<object class="playerpreview" type="application/x-shockwave-flash"
data="player_mp3_mini.swf" width="200" height="20">
<param name="movie" value="player_mp3_mini.swf" />
<param name="bgcolor" value="#085c68" />
<param name="FlashVars" value="mp3=test.mp3" />
<embed href="player_mp3_mini.swf" bgcolor="#085c68" width="200"
height="20" name="movie" align=""
type="application/x-shockwave-flash" flashvars="mp3=test.mp3">
</embed>
</object>
</audio>
<script>
function playSound() {
if (document.getElementById('audio').canPlayType) {
if (!document.getElementById('audio').canPlayType('audio/mpeg')) {
document.getElementById('audio').style.display = 'none';
swfobject.embedSWF(
"player_mp3_mini.swf",
"fallback",
"200",
"20",
"9.0.0",
"",
{"mp3":"http://www.hellopixel.net/click.mp3"},
{"bgcolor":"#085c68"});
} else {
document.getElementById('audio').play();
document.getElementById('fallback').style.display = 'none';
}
}
}
</script>​
Fiddle: http://jsfiddle.net/SMR4V/
Note: I have not created the ogg file, so won't work in firefox, because FF is using ogg files.

Related

Embedding WMV file into Html and making it work in most of the browsers

I was asked to embed a wmv file into a piece of html.
So far so good, googling on how to do it quickly reveals that this is achieved using html tags object and embed.
My problem is that the sample I set up (shown below) is only working in IE. Chrome displays a a gray box with the message "This plugin is not supported", and following the help that shows up turns out chrome does not play NPAPI plug-ins anymore. Firefox shows nothing whatsoever, not even the object frame.
I researched a bit for workarounds to play it on chrome notwithstanding the fact google stopped supporting it sometime on 2015.
Messing with chrome://flags/#enable-npapi looked promising (as suggested here), but I can't find a enable Npapi among my flags.
Anyone knows how to have it working at least in Chrome, Firefox and IE ?
Is it possible to (freely) convert a wvm to Html5 video ?
<Html>
<Head></Head>
<Body>
<object id="MediaPlayer" width="192" height="190" type="video/x-ms-asf">
<param name="FileName"value="D:/myVideo.wmv">
<param name="autostart" value="false">
<param name="ShowControls" value="true">
<param name="ShowStatusBar" value="false">
<param name="ShowDisplay" value="false">
<embed type="application/x-mplayer2" src="D:/myVideo.wmv"
width="192" height="190" ShowControls="1" ShowStatusBar="0" ShowDisplay="0" autostart="0" />
</object>
</Body>
</Html>
There are any number of resources that will convert your wmv to mp4/ogg/other types of cross-browser format, including http://video.online-convert.com/convert-to-mp4, however thats beside the point.
If you want to display the video in html5, you should really use the <video>
tag.
An example of this would be
<!DOCTYPE html>
<html>
<body>
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
<source src="movie.webm" type="video/webm">
Your browser does not support the video tag.
</video>
</body>
</html>

How can I embed my own .mov in to a web page ?

I just exported a .mov file from Final Cut Pro.
I want to embed that video into my HTML.
I tried :
<object width="800" height="600"
classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"
codebase="http://www.apple.com/qtactivex/qtplugin.cab">
<param name="src" value="FRIEND.mov">
<param name="autoplay" value="true">
<param name="controller" value="false">
<embed src="FRIEND.mov" width="160" height="144"
autoplay="true" controller="false"
pluginspage="http://www.apple.com/quicktime/download/">
</embed>
</object>
I got :
Then, I tried :
<video width="800" height="600" src="FRIEND.mov" controls ></video>
I got this :
Then, when I press the play btn, I got the sound to play, but not the video. ??? Curious ?
What is the most efficient way to achieve something like that ?
First of all you can check the video format which are supported by html5 video tag from here.
.mov format does not have support by any browser.
So you need to do this with object tag. At your object tag you are using quicktime player plugin which is not exists on your browser.
install quick time player plugin to your browser. if you using chrome check this

MediaElement flash fallback getting called twice

I have integrated the medialement player. It works great on all browsers except firefox. As I am trying to integrate a mp4 video. I know that firefox does not support mp4, and hence setup a flash fallback. The flash fallback works, but it gets called twice. And I have 2 instances of the player getting played simultaneously. Any idea why would this happen?
The code:
<video id="player1" width="700" height="430">
<source type="video/mp4" src="'+videoUrl+'" />
<object width="700" height="430" type="application/x-shockwave-flash" data="flashmediaelement.swf">
<param name="movie" value="/assets/flashmediaelement.swf" />
<param name="flashvars" value="controls=true&file="'+videoUrl+'" />
</object>
</video>
The videoUrl is submitted by the user. The user is given a prompt to paste the URL.

Why does Chrome keep attempting to mount mp4 files when it can't play them?

Why does Chrome keep attempting to mount mp4 files when it can't play them?
I am working on a PC Windows 7, IE 10, Chrome Version 25.0.1364.172.
It doesn't matter whether mp4 source line is before or after the object lines, Chrome seems to want to mount the mp4 files even if it can't play them.
I would like to play the mp4 file in IE 10 and the flash equivalent in Chrome, but I can't seem to make any combination of line ordering work.
So far, my only workaround is to use flash in both browsers. But why do I have to? I thought the html5 video tag was supported by both browsers?
Is this the correct way for a flash fallback to be coded, as it doesn't seem to be working. Is there a way to code this so I don't have to use flash in IE 10, which can play the mp4 files native?
Thanks for whatever assistance you can give me.
Here's a copy of the code generated at http://sandbox.thewikies.com/vfe-generator/
<!-- "Video For Everybody" http://camendesign.com/code/video_for_everybody -->
<video controls="controls" poster="Mark.jpg" width="640" height="480">
<source src="Mark.mp4" type="video/mp4" />
<object type="application/x-shockwave-flash" data="http://player.longtailvideo.com/player.swf" width="640" height="360">
<param name="movie" value="http://player.longtailvideo.com/player.swf" />
<param name="allowFullScreen" value="true" />
<param name="wmode" value="transparent" />
<param name="flashVars" value="controlbar=over&image=Mark.jpg&file=Mark.mp4" />
<img alt="Mark" src="Mark.jpg" width="640" height="480" title="No video playback capabilities, please download the video below" />
</object>
</video>

HTML5 Video with Flash fallback not working

I am trying to use a video element for HTML5 video and a nested element for Flash fallback. I have seen code using flowplayer to fallback to flash - but their documentation only shows a javascript interface. Is there any way to do something like what is below? To test, I remove the two lines with "source src="... "
If I have to use javascript for the flash, how would I code it in line with HTML5?
Thanks!
<video id="movie" width="640" height="360" preload controls>
<source src="./videos/fiddler-tradition.ogv" type="video/ogg; codecs=theora,vorbis" />
<source src="./videos/fiddler-tradition.mp4" />
<object id="flowplayer" width="640" height="360"
data="./flowplayer/flowplayer-3.2.16.swf"
type="application/x-shockwave-flash">
<param name="movie" value="./flowplayer/flowplayer-3.2.16.swf" />
<param name="allowfullscreen" value="true" />
<param name="flashvars"
value='config={"clip":"./videos/fiddler-tradition.flv"}' />
</object>
</video>
The above non java flowplayer code (the code within the object tag) works fine when outside the video tags. I realize my issue is testing the fallback without html5 on my localhost machine while all I have available is an html5 browser. I will upload to the web and try on another pc with an older browser.
UPDATE: All is working - Testing in IE7, the code falls back to flash player. Yeah!