Search code examples
javascriptjplayer

Autoplay audio with jPlayer


Recently I purchased this handy script from codecanyon called "Flatie", it works for literally every new project I have except I have stumbled into a dead end in terms of getting the audio to autoplay.

<script type="text/javascript">
$(document).ready(function() {
  $("#player4").flatie({
     media: {
     mp3: "http://www.example.com/mp3embed-song-name.mp3"
  },
     swfPath: "http://www.example.com/jplayer.swf"
  });
});
</script>

Here's the current Javascript I am using (This is all I have ever needed + including the JavaScript file found here.)

When I used the standard method shown on the jPlayer documentation for autoplaying it makes my player disappear.

(I am a complete noob when it comes to Javascript so I have no idea what I am doing wrong.)


Solution

  • <script type="text/javascript">
    $(document).ready(function(){
    
        var stream = {
            //title: "ABC Jazz",
            mp3: "path/../Wolf.mp3"
        },
        ready = false;
    
        $("#jquery_jplayer_1").jPlayer({
            ready: function (event) {
                ready = true;
                $(this).jPlayer("setMedia", stream);
                $(this).jPlayer("play", 1);
            },
            pause: function() {
                $(this).jPlayer("clearMedia");
            },
            error: function(event) {
                if(ready && event.jPlayer.error.type === $.jPlayer.error.URL_NOT_SET) {
                    // Setup the media stream again and play it.
                    $(this).jPlayer("setMedia", stream).jPlayer("play");
                }
            },
    
        swfPath: "path/../jPlayer/dist/jplayer",
        supplied: "mp3",
        preload: "none",
        volume: 1,
        wmode: "window",
        useStateClassSkin: true,
        autoBlur: false,
        keyEnabled: true
      });
    
    });
    </script>