最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Playing sound inside javascript event - Stack Overflow

programmeradmin5浏览0评论

I have an event where I need to play a mp3/wav sound every time a certain condition is verified. Is there a way to play it cross-browser, also even if I open android browser on a tablet?

Actually I am using a javascript plugin called ionSound but looks like it doesn't work under android's tabled..

Like:

jQuery.ajax({
    type: "GET",
    dataType: "json",
    url: "./getData.php?idx="+id,
    success: function(data) {
        if (data.length > 0) {
            // alert('Gotta play sound...');
            dataFound = true;
            $.ionSound({
                sounds: [ "woop_woop" ],
                path: "../../images/"
            });

            $.ionSound.play("woop_woop");
            // alert('Played sound right now...');
        } else {
            dataFound = false;
        }
    }
});

Thanks in advance to all!

Cheers, Luigi


I'm picking back this question again... I figured out on some platforms it doesn't really play sound... for example on another desktop PC with Chrome it doesn't play audio, neither on another tablet...I think it's due of browser's version... so is there a pure javascript way to just play an audio like a little mp3 or wav from code?..

Thanks in advance! Cheers Luigi

I have an event where I need to play a mp3/wav sound every time a certain condition is verified. Is there a way to play it cross-browser, also even if I open android browser on a tablet?

Actually I am using a javascript plugin called ionSound but looks like it doesn't work under android's tabled..

Like:

jQuery.ajax({
    type: "GET",
    dataType: "json",
    url: "./getData.php?idx="+id,
    success: function(data) {
        if (data.length > 0) {
            // alert('Gotta play sound...');
            dataFound = true;
            $.ionSound({
                sounds: [ "woop_woop" ],
                path: "../../images/"
            });

            $.ionSound.play("woop_woop");
            // alert('Played sound right now...');
        } else {
            dataFound = false;
        }
    }
});

Thanks in advance to all!

Cheers, Luigi


I'm picking back this question again... I figured out on some platforms it doesn't really play sound... for example on another desktop PC with Chrome it doesn't play audio, neither on another tablet...I think it's due of browser's version... so is there a pure javascript way to just play an audio like a little mp3 or wav from code?..

Thanks in advance! Cheers Luigi

Share Improve this question edited Mar 21, 2014 at 2:30 Bill the Lizard 406k211 gold badges572 silver badges889 bronze badges asked Feb 14, 2014 at 9:33 LuiginoLuigino 7793 gold badges9 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

Create an Audio object and play it! The link Audio takes in the constructor may be from any source.

var audio = new Audio('audio_file.mp3');
audio.play()
发布评论

评论列表(0)

  1. 暂无评论