Embedding a media player in a website using HTML [closed] - html

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
What I want to do is to embed music files within a website (Have something a'la little mp3 player on the site). I want the audience to be able to play, stop etc the songs by using custom made controllers.
How do I code those custom made buttons so that they all work fine?

You can use plenty of things.
If you're a standards junkie, you can use the HTML5 <audio> tag:
Here is the official W3C specification for the audio tag.
Usage:
<audio controls>
<source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.mp4"
type='audio/mp4'>
<!-- The next two lines are only executed if the browser doesn't support MP4 files -->
<source src="http://media.w3.org/2010/07/bunny/04-Death_Becomes_Fur.oga"
type='audio/ogg; codecs=vorbis'>
<!-- The next line will only be executed if the browser doesn't support the <audio> tag-->
<p>Your user agent does not support the HTML5 Audio element.</p>
</audio>
jsFiddle here.
Or, if you want to support older browsers, you can use many of the free audio flash players available. Such as:
Dewplayer
MP3 Player (boring name... right? :) )
Website Music Player (even more boring... right?)
Zanorg Player
Note: I'm not sure which are the best ones, as I have never used one (yet).
UPDATE: As mentioned in another answer's comment, you are using XHTML 1.0 Transitional. You might be able to get <audio> to work with some hack.
UPDATE 2: I just remembered another way to do play audio. This will work in XHTML!!! This is fully standards-compliant.
You use this JavaScript:
var aud = document.createElement("iframe");
aud.setAttribute('src', "http://yoursite.com/youraudio.mp4"); // replace with actual file path
aud.setAttribute('width', '1px');
aud.setAttribute('height', '1px');
aud.setAttribute('scrolling', 'no');
aud.style.border = "0px";
document.body.appendChild(aud);
This is my answer to another question.
UPDATE 3: To customise the controls, you can use something like this.

Definitely the HTML5 element is the way to go. There's at least basic support for it in the most recent versions of almost all browsers:
http://caniuse.com/#feat=audio
And it allows to specify what to do when the element is not supported by the browser. For example you could add a link to a file by doing:
<audio controls src="intro.mp3">
Introduction to HTML5 (10:12) - MP3 - 3.2MB
</audio>
You can find this examples and more information about the audio element in the following link:
http://hacks.mozilla.org/2012/04/enhanceyourhtml5appwithaudio/
Finally, the good news are that mozilla's April's dev Derby is about this element so that's probably going to provide loads of great examples of how to make the most out of this element:
http://hacks.mozilla.org/2012/04/april-dev-derby-show-us-what-you-can-do-with-html5-audio/

Here is a solution to make an accessible audio player with valid xHTML and non-intrusive javascript thanks to W3C Web Audio API :
What to do :
If the browser is able to read, then we display controls
If the browser is not able to read, we just render a link to the file
First of all, we check if the browser implements Web Audio API:
if (typeof Audio === 'undefined') {
// abort
}
Then we instanciate an Audio object:
var player = new Audio('mysong.ogg');
Then we can check if the browser is able to decode this type of file :
if(!player.canPlayType('audio/ogg')) {
// abort
}
Or even if it can play the codec :
if(!player.canPlayType('audio/ogg; codecs="vorbis"')) {
// abort
}
Then we can use player.play(), player.pause();
I have done a tiny JQuery plugin that I called nanodio to test this.
You can check how it works on my demo page (sorry, but text is in french :p )
Just click on a link to play, and click again to pause. If the browser can read it natively, it will. If it can't, it should download the file.
This is just a little example, but you can improve it to use any element of your page as a control button or generate ones on the fly with javascript... Whatever you want.

I found the that either IE or Chrome choked on most of these, or they required external libraries. I just wanted to play an MP3, and I found the page http://www.w3schools.com/html/html_sounds.asp very helpful.
<audio controls>
<source src="horse.mp3" type="audio/mpeg">
<embed height="50" width="100" src="horse.mp3">
</audio>
Worked for me in the browsers I tried, but I didn't have some of the old ones around at this time.

<html>
<head>
<H1>
Automatically play music files on your website when a page loads
</H1>
</head>
<body>
<embed src="YourMusic.mp3" autostart="true" loop="true" width="2" height="0">
</embed>
</body>
</html>

If you are using HTML 5, there is the <audio> element.
On MDN:
The audio element is used to embed sound content in an HTML or XHTML document. The audio element was added as part of HTML5.
Update:
In order to play audio in the browser in HTML versions before 5 (including XHTML), you need to use one of the many flash audio players.

Related

Can I link audio thats not local into my html file?

My hosting doesn't allow me to upload mp3 or any audio files, so is there any way I can link audio in some other way? I know we can embed soundcloud and stuff but just wondering if there was any other alternative.
I’ve found this very useful guide that covers some alternatives:
Linking to a sound file using a href allows a browser to open and play
an audio file if the viewer of your web page has properly configured
their Internet browser. You can also use the tag or the newer
tag to insert a sound file directly into a web page.
<a href> tag
<a href="https://www.computerhope.com/jargon/m/example.mp3">Play sound
file</a>
<audio> tag
The tag can create a media player as part of the web page. It allows the visitor to play, stop, pause, or download an audio file. The element is compatible with all modern web browsers.
<audio controls>
<source src="https://www.computerhope.com/jargon/m/example.mp3" />
</audio>
<embed> tag
An older method of including audio files is to use the tag. While this method certainly works, it is less efficient than those which were mentioned above. As such, we recommend using one of the solutions demonstrated above.
<embed src="https://www.computerhope.com/jargon/m/example.mp3">
I figured the best way to do it without uploading your audio/music, BASE64 ENCODING!!
Very easy, kinda messy and supposed to be used for images I guess but works fine with audios and should work with videos as well (haven't tried videos)
Here a base64 encoder: https://omatsuri.app/b64-encoding
WARNING THOUGH! IT MIGHT GET LAGGY & MESSY

How to make HTML5 audio play exactly once on clicking?

I'm putting together an online evaluation survey of synthesized speech. Part of the test requires that listeners are only allowed to hear a given WAV file play once (as an experimental control).
I've already figured out how to simply embed an audio clip with controls, so that it can be played several times:
<audio controls>
<source src="http://mypage.com/my_sound.wav" type="audio/wav">
Your browser does not support the audio element
</audio>
Furthermore, I've seen that some HTML questions have attempted to resolve the error of HTML audio playing back only once:
Audio played once only in Google Chrome in html
JavaScript + HTML5: Audio will only play once under certain circumstances
However, my question is how to code this (play HTML audio just once on-click) intentionally? Everything I've seen treats it as a bug to be fixed, rather than an intentional goal.
Cheers!
I've been looking for this one too.
You have a few options.
The key to this stop is the onended event, which as you might expect, triggers when the fat lady stops singing.
<audio id="gilda">
<source url="epic-aria.mp3">
</audio>
var fatLady = document.getElementById('gilda');
fatLady.onended = function() {
fatLady.pause();
fatLady.currentTime = 0; // << only needed if you're cutting off the sound misstep (before the end) and need to return to the beginning - but you might need it. Since you are doing some gaming, I figured that might come up...
};
There is also a jQuery way, which I am currently using. jQuery doesn't use onended, however - it uses ended instead...
var fatLady = $('#gilda');
fatLady.bind('ended', function(event) {
fatLady
.pause()
.currentTime = 0; // << haven't field-tested this yet, but jquery didn't yell at me about it.
});
Let me know if you run into any hiccups.

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>

Adding .wmv-video to site with <video>-tags

It seems that i cannot add .wmv-videos to my site, though some ppl (in Google search) says it can an anothers say it cannot...
But i only have these videoes in .wmv-format and therefore have to put them on page in this format.
I have tried many things as:
<video id="sampleMovie" width="300px" height="168px" preload controls>
<source src="video.wmv" type="video/x-ms-wmv" />
</video>
And also have i rendered my .htaccess
AddType video/x-ms-wmv .wmv
And also without the dot.
But what it tells me when i try to load the page with the video is:
"No video found with the supporting MIME-type"
(It says that on another language than english, so it might not be 100% correct, but the point should be there)
How can i make this work?
You have three options.
One:
WMV files can play in IE with the Windows Media Player object. See this.
Two:
There is no way. No browser (excpet IE with above solution) supports playing WMV files. You will have to convert it into a format that browsers know how to play. A free online converter to MP4 is
here
and a standalone one is here.

How do you play a sound on the web browser?

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 :)