How to preload a sound in Javascript?

前端 未结 6 1687
伪装坚强ぢ
伪装坚强ぢ 2020-11-27 13:15

I can preload images easily thanks to the onload function. But it doesn\'t work with audio. Browsers like Chrome, Safari, Firefox, etc. don\'t support the

6条回答
  •  生来不讨喜
    2020-11-27 13:51

    I tried the accepted answer by tylermwashburn and it didn't work in Chrome. So I moved on and created this and it benefits from jQuery. It also sniffs for ogg and mp3 support. The default is ogg because some experts say a 192KBS ogg file is as good as a 320KBS MP3, so you save 40% on your required audio downloads. However mp3 is required for IE9:

    // Audio preloader
    
    $(window).ready(function(){
      var audio_preload = 0;
      function launchApp(launch){
        audio_preload++;
        if ( audio_preload == 3 || launch == 1) {  // set 3 to # of your files
          start();  // set this function to your start function
        }
      }
      var support = {};
      function audioSupport() {
        var a = document.createElement('audio');
        var ogg = !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
        if (ogg) return 'ogg';
        var mp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
        if (mp3) return 'mp3';
        else return 0;
      }
      support.audio = audioSupport();
      function loadAudio(url, vol){
        var audio = new Audio();
        audio.src = url;
        audio.preload = "auto";
        audio.volume = vol;
        $(audio).on("loadeddata", launchApp);  // jQuery checking
        return audio;
      }
      if (support.audio === 'ogg') {
        var snd1 = loadAudio("sounds/sound1.ogg", 1);  // ie) the 1 is 100% volume
        var snd2 = loadAudio("sounds/sound2.ogg", 0.3);  // ie) the 0.3 is 30%
        var snd3 = loadAudio("sounds/sound3.ogg", 0.05);
            // add more sounds here
      } else if (support.audio === 'mp3') { 
        var snd1 = loadAudio("sounds/sound1.mp3", 1);
        var snd2 = loadAudio("sounds/sound2.mp3", 0.3);
        var snd3 = loadAudio("sounds/sound3.mp3", 0.05);
            // add more sounds here
      } else {
        launchApp(1);  // launch app without audio
      }
    
    // this is your first function you want to start after audio is preloaded:
      function start(){
         if (support.audio) snd1.play();  // this is how you play sounds
      }
    
    });
    

    Furthermore: Here is an mp3 to ogg converter: http://audio.online-convert.com/convert-to-ogg Or you can use VLC Media player to convert. Check your mp3 bitrate by right-clicking on the mp3 file (in Windows) and going to the file details. Try to reduce by 40% when selecting your new bitrate for your new 'ogg' file. The converter may throw an error, so keep increasing the size until is accepted. Of course test sounds for satisfactory quality. Also (and this applied to me) if you are using VLC Media player to test your audio tracks make sure you set the volume at 100% or below or otherwise you'll hear audio degradation and might mistakenly think it is the result of compression.

提交回复
热议问题