How do you play a sound on the web browser? - html

How do I play a sound on the web browser as notification?

You can use the <audio> tag combined with JavaScript to play sounds at a given time. You'll need JavaScript, of course, as it's done on the frontend, and hence, with client-side programming.
For example,
<audio style="display: none;" id="notification" preload src="path/to/soundfile">
Then, for the scripting, place this somewhere in any part of your script that requires sound notification to occur:
document.getElementById('notification').play();
For those who recommend Flash as it's supported in IE, note graceful degradation, where, for non-essential things (such as sound notification) we choose to use new, recommended technologies that work on most browsers, instead of using hackish, insecure methods to try to get all browsers to work.

With HTML5 you can use a bit of javascript and the <audio>-tag.
I have an example on my site: http://www.khaaaaan.com
The javascript:
<script type="text/javascript">
function soundPlay(which)
{
var audio = document.getElementById(which);
audio.play();
}
</script>
The button which activates the sound:
<input type="button" class="khaaaaan" onclick="soundPlay('khaaaaan');" Text="KHAAAAAN!" title="CLICK MEEEEEEEEE!" />
And then the audio-tag
<audio src="khaaaaan.wav" autobuffer="autobuffer" id="khaaaaan" />
This also works (Used it before the <audio>-script :)
Cross-platform, cross-browser way to play sound from Javascript?

Since the audio tag isn't normative, I'd suggest using the 'legacy' way of handling this.
Here's another SO post that deals with it:
Cross-platform, cross-browser way to play sound from Javascript?

You could also embed a Flash widget which could perform all sorts of other useful things at the same time, including keeping track of how many times a user has triggered a sound prompt, or provide an interface for disabling such aural prompting. Using Flash would also offer you streaming functionalities and flash cookie local data storage.

Though you can do it with audio tag it wont work in browsers that don't support HTML5. The easiest way to do will be using...
<embed src="1.mp3" width="200" height="55" autostart="true" loop="true" style="visibility:none; position:fixed;">
This uses the default player. Eg: Media Player in windows.
But the standard way is using flash
An tutorial can be found here.
This also works in all web browsers IE4 +, Firefox(all), Chrome... And don't depend on HTML 5 or Flash, and uses the default player which is always there.
N.B:
EMBED tag is not a part of the HTML 4 or xHTML 1 specifications, but it is still widely supported by modern browsers. Unlike other tags, the attributes used by EMBED tag depend on the type of plugin being used (this odd free-attribute concept is why EMBED tag has been rejected by the HTML standards makers).
But this solves problems :)

Related

Simple Background Music for website

I have been designing a website recently and I had the idea to try implementing some background music.
I went about this by simply having the music play through a video looper
<embed name="Example Song Here"
src="http://www.infinitelooper.com/?v=GGtcJCzB9cU&p=n"
loop="True"
hidden="true"
autostart="true">
</embed
That works perfectly, the problem comes that users will probably want to stop the music, so I added a "Stop Music" button
FORM METHOD="LINK" ACTION="WebsiteURL">
<INPUT TYPE="submit" VALUE="Stop Music">
</FORM>
Adds the button fine, but as you can see it doesn't do anything but redirect back to the site, I've been looking for that extra line I need to make the button stop the music above.
Yes, I'm a total novice.
This code uses Html5. if you can use html5 this works fine.
<audio id="myAudio"
<source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4"
type='audio/mp4'>
<source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga"
type='audio/ogg; codecs=vorbis'>
Your user agent does not support the HTML5 Audio element.
</audio>
<button type="button" onclick="aud_play_pause()">Play/Pause</button>
<script>
function aud_play_pause() {
var myAudio = document.getElementById("myAudio");
if (myAudio.paused) {
myAudio.play();
} else {
myAudio.pause();
}
}
</script>
The embed element as such is conforming in the current HTML specification (HTML5), but it isn’t what it used to be. The old, nonstandard embed element had various attributes that controlled the presentation, but they are not included in the specification and support to them varies greatly and is diminishing. Besides, your example does not actually embed sound directly. Instead, it embeds an HTML element, which has embedded sound its own way. Normally you cannot control what happens on a page that you embed from another server.
There is no HTML way to control playing in an embed element, since it is expected to be implemented with a plugin. Note that modern browsers might lack any plugins for the purpose by default or might have such plugins disabled. So embed really isn’t what it used to be.
Others have suggested, for good reasons, that you don’t use background sound and that if you use, implement it with the audio element. (You could have an embed element inside the audio element, as fallback content for browsers that do not recognize audio markup at all.) However, audio only applies to direct embedding of sound, as opposite to embedding a page that has background sound.
In your case, you can create your own stop button using JavaScript. You can make a click on the button to simply remove the embed element. Of course, it would then simply terminate the sound. Removing an element is a bit indirect in JavaScript: you tell its parent to kick that child out.
<embed id=bgs name="Example Song Here" controls=stopbutton
src="http://www.infinitelooper.com/?v=GGtcJCzB9cU&p=n">
</embed>
<button onclick=
"var bgs = document.getElementById('bgs'); bgs.parentNode.removeChild(bgs);
this.parentNode.removeChild(this)">
Stop background sound</button>

Why would I need an HTML5 Audio library?

I notice that there are many libraries out there for playing HTML5 audio, but ain't the whole idea of HTML5 audio that we don't need a library for this to work properly?
If I don't need Flash fallback, but a simple solution for streaming music (my own) that can also play each track after each other, do I then need a library like jPlayer or Soundmanager2?
I know I need different audio formats, but that's not a problem as I won't have many tracks online.
The "whole idea" of HTML5 audio is that end users and web page authors shouldn't need to rely on a browser plugin to play audio - the functionality should be included in the browser, and controllable directly from standardised HTML/JavaScript.
I haven't used an HTML5 audio library, but looking at the jPlayer and Soundmanager2 home pages, it looks like the main functionality they offer is fallbacks for browsers that don't support HTML5 audio.
I suspect they might also have some built-in custom UI for playing audio though, in case you don't like the native audio controls that browsers provide.
You could use straight-up HTML5, but you'd soon realize that has a whole host of problems.
Library's like jPlayer (which is great by the way) account for many common issues like browser support, optimization and fall-backs etc.
If you want to code all that yourself you can, but a popular library will be leaps ahead and it's the wise choice to take.
The truth is, HTML5 offers very powerful features, but its support is thin at this current moment in time. It's all to make your life easier!
HTML-5 audio actually encompasses two distinct factions at this point. One is the audio tag and the other is the Web Audio API. The former is a quick mechanism for playing an embedding audio files, the latter is a way to play,process and manipulate audio in a low-latency manner acceptable for game development and more professional environments where features like reverb effects,filters,"3D" sound and other characteristics are needed. But to answer your question, the only real reason to use a library for the HTML 5 audio tag is (in my opinion) for backward compatibility between browsers as well as ease of use for things like multi-shot file triggering and event handling.

youtube.com and html5 video tag

I have a question. I know youtube has supported html5 for some time, so I just tried disabling the adobe flash plugin and took a look at the page source. I found no video tag. Could anybody explain that?
Using chrome, after disabling Flash, I do get a warning when trying to watch videos ("You need Flash!"), but it detects that I don't have flash, and uses the HTML5 version instead. If you use Chrome, you can look at the DOM with the developer tools (F12), and you'll indeed find a video tag in the video-container div, e.g.
<video class="video-stream" x-webkit-airplay="allow" src="http://o-o.preferred.twtelecom-dfw1.v15.lscache5.c.youtube.com/videoplayback?sparams=cp%2Cid%2Cip%2Cipbits%2Citag%2Cratebypass%2Csource%2Cexpire&fexp=900161&itag=43&ip=207.0.0.0&signature=55FCBF36A597656FECBEC8E78051B3BD30EE8D97.8F8D573710D767EAF4429FBC54C940DF7611A1BE&sver=3&ratebypass=yes&source=youtube&expire=1330650696&key=yt1&ipbits=8&cp=U0hSRVZQTl9OUENOMl9OSlJHOlBlcHJOMW9PSHhH&id=381980b5e867a1c5" data-youtube-id="OBmAtehnocU"></video>
For almost everything Google does on the web they use javascript. Most likely what is happening is that they test the browser to make sure it supports HTML5 video, they then load the the video dynamically using javascript.
To verify, use a web browser tool to inspect the current html as it's shown, not as it's sent to the browser.
And as Marius noted, be sure to have HTML5 enabled

About embed midi files on a webpage

I am working on a project related to vintage web designs. I would like to embed and autoplay a midi file but:
It doesen't work on IOS (I tried with my ipad)
On Firefox, sometimes the tune is not played, I don't know why.
Does the midi player depends on the browser or a plugin?
Can I find a univeral alternative to play my tune?
I am using the <embed> tag this way:
<embed src="tune.mid" hidden="true" autostart="true" autoplay="true">
I've referenced this question before and fell upon the answer of using the library midijs.
However, since that lib now has a bitcoin miner included, I've made a variant without the mining code:
https://kitchwww.github.io/midi/midi.js
It can be included and used in exactly the same way:
<script type='text/javascript' src='https://kitchwww.github.io/midi/midi.js'></script>
<a href="#" onClick="function(){
MIDIjs.initAll();
MIDIjs.play('path/to/yoursong.midi');}">Play My Song</a>
EDIT: updated to include an init function to be called on a User Gesture, as all audio must now be initiated from one.
Yes, the embed tag is reserved for plugins, which Mobile Safari doesn't support.
Take a look at HTML5 audio tag, which is supported by most browsers.
But please don't play music on a webpage... it's annoying - most either listens to music, have the speakers turned off, or are at the office.
Your HTML is OK. The problem is that some lower class browsers have lost the ability to play midi files. They have to install an add on. Real Player used to be a good solution, but now they are pushing a cloud subscription. There are plenty of other midi players around, but the users have to install one.
Browsers dropped support for playing MIDI files natively over time. You might want to try MIDI.js, a JavaScript based cross browser library.
Add the MIDI.js script to your webpage:
<script type='text/javascript' src='http://www.midijs.net/lib/midi.js'></script>
And then add a link to start playing:
Play My Song
Take a look at http://www.midijs.net for details.
For mid, and kar files, I suggest using vanBasco midi player as your default player. See the source on http://midkar.com/blues/blues_01.html
To Embed;
embed src="musicfile.mid" width=144 height=60 autostart=true repeat=false loop=false
(replace the "musicfile.mid" with the name of your midi file)
Use the HTML5 Audio tag. But like The guy before me said.... please reconsider music on your page.
<audio src="example.midi" preload="auto" autoplay="autoplay"></audio>
Well, here it is Aug 13, 2017 and don't you know embedding a bg midi on a web page is still a mystery.
In fact I'm pretty certain it can't even be done anymore (except IE's bgsound src, which still works fine for me).
But as for Firefox and Chrome, the solution provided by jofeu is a great work-around.
I only wish there was a way to embed so the midi just starts playing on page load, without having to click anything. I guess those days are gone.

embed vs flash video player for displaying video

I want to show video in one of my pages of an enterprise application. I was thinking on using a flash based player such as Flowplayer. Then I learnt that I could also use the EMBED tag.
The default browser for accessing my web app is IE - whatever version. I also supose there should be
some users using other browsers.
My question is are the drawbacks on each and what have you used before to show video?
I would highly recommend using framework which supports HTML5 video tag with flash or other format fallback for old browsers. Examples are numerous and include VideoJS, FlareVideo and others. This way you would depend on (name-your-disadvantages-like-insecure-and-proprietary) technologies only for older browsers and support a newer ones well. The result is fewer problems you face when the management decides to upgrade the clients.
embedding offers the easiest solution. The iframe code is simple and does the job
<iframe src="http://(your video source here)" width="400" height="380" frameborder="0"> </iframe>