Play mp3 file using javascript
Asked Answered
S

3

16

Which is the best and cross-browser way to play a mp3 (very short) file via javascript? I tried different ways but there's always a problem...

EDIT 1:

I don't need a "full" player, I just need a function than I can call everytime something happens

EDIT 2:

Ok, I've to explain better my problem. The server is a smartphone connected in a LAN, but not always to the internet. I want to use mp3 because the file weighs only 3kb (instead of 60kb of wav), but if the mechanism to play this file is too "heavy" (a player or jquery.js) I think is better to use the wav file. Other suggestions?

Sabrasabre answered 15/2, 2012 at 20:16 Comment(4)
@Johan I tried html5 audio tag with this code, but it doesn't work on ChromeSabrasabre
And exactly how can jQuery be heavier than a 63kb wav? BTW, the server is a smartphone?Brnaby
I mean that I'm avoiding to use the wav and jquery to spend only 3k. And yes, I wrote a web server for android (that's a piece of the main app)Sabrasabre
@supergiox, You didn't mark any entry as an answer. Could you tell us what trick did you use finally?Luehrmann
M
28

Use this:

new Audio('your/url/here').play()

The documentation for the this (HTMLAudioElement) can be found at MDN, note that most of the controls are inherited by HTMLMediaElement.

Mullin answered 11/1, 2015 at 18:28 Comment(2)
What if I want to play several songs? Won't issuing a new Audio for each one cause a memory leak?Galvano
The play method is not allowed by the user agent or the platform in the current contextRelativity
B
5

Load the page with just a direct download to the audio file. Detect if the browser supports MP3s. If it does, progressively enhance the direct download into an AUDIO tag. Here's a sample:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Audio demo</title>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.0.6/modernizr.min.js"></script>
</head>
<body>
    <p id="audio">
        <a href="BadAppleEnglish.mp3">Listen &raquo;</a>        
    </p>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
    <script src="audio.js"></script>
</body>
</html>

And the Javascript:

$(function () {
var audioElement = $('#audio'),
    href = audioElement.children('a').attr('href');
$.template('audioTemplate', '<audio src="${src}" controls>');
if (Modernizr.audio.mp3) {
    audioElement.empty();
    $.tmpl('audioTemplate', {src: href}).appendTo(audioElement);
}
});

I would imagine that most of the zillions of prebuilt MP3 playing plugins work like this.

UPDATE:

Since you've edited the question to specify that you'd prefer a solution that doesn't use jQuery, I will point out that rewriting this to not use jQuery is trivial. It's just longer and less elegant. Do keep in mind that Javascript libraries loaded from CDNs are usually cached by the browser.

Brnaby answered 15/2, 2012 at 21:0 Comment(0)
I
0

try using my own script,

<script src="https://webtinq.nl/ap/script.js></script>

and when that script is linked, the only thing you need to do is

play('example');
Intense answered 22/1, 2023 at 15:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.