No sound on iOS 6 Web Audio API

后端 未结 12 1379
灰色年华
灰色年华 2020-12-02 09:41

I was really excited to see iOS 6 supports the Web Audio API, since we make HTML5 games. However, I cannot get iOS 6 to play any sound at all using the Web Audio API with e

12条回答
  •  旧时难觅i
    2020-12-02 10:44

    updated for 2015 solution: hey all, if you are here working on a web audio problem with ios6+ I've found these links as help.

    -this is a good article with code solution: http://matt-harrison.com/perfect-web-audio-on-ios-devices-with-the-web-audio-api/

    -here is an update to the api after the above ^ solution article was written https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API/Porting_webkitAudioContext_code_to_standards_based_AudioContext

    -below is my updated solution to the first article, using the changes from the second article. The issue I was having was iOS 7 safari throwing a strange not-enough-args error. this fixed it:

    define(function() {
    
      try {
        window.AudioContext = window.AudioContext || window.webkitAudioContext;
        window.audioContext = new window.AudioContext();
      } catch (e) {
        console.log("No Web Audio API support");
      }
    /*
     * WebAudioAPISoundManager Constructor
     */
     var WebAudioAPISoundManager = function (context) {
      this.context = context;
      this.bufferList = {};
      this.playingSounds = {};
    };
    
    /*
     * WebAudioAPISoundManager Prototype
     */
     WebAudioAPISoundManager.prototype = {
       addSound: function (url) {
          // Load buffer asynchronously
          var request = new XMLHttpRequest();
          request.open("GET", url, true);
          request.responseType = "arraybuffer";
    
          var self = this;
    
          request.onload = function () {
            // Asynchronously decode the audio file data in request.response
            self.context.decodeAudioData(
              request.response,
    
              function (buffer) {
                if (!buffer) {
                  alert('error decoding file data: ' + url);
                  return;
                }
                self.bufferList[url] = buffer;
              });
          };
    
          request.onerror = function () {
            alert('BufferLoader: XHR error');
          };
    
          request.send();
        },
        stopSoundWithUrl: function(url) {
          if(this.playingSounds.hasOwnProperty(url)){
            for(var i in this.playingSounds[url]){
              if(this.playingSounds[url].hasOwnProperty(i)) {
                this.playingSounds[url][i].stop(0);
              }
            }
          }
        }
      };
    
    /*
     * WebAudioAPISound Constructor
     */
     var WebAudioAPISound = function (url, options) {
      this.settings = {
        loop: false
      };
    
      for(var i in options){
        if(options.hasOwnProperty(i)) {
          this.settings[i] = options[i];
        }
      }
    
      this.url = '/src/www/assets/audio/' + url + '.mp3';
      this.volume = 1;
      window.webAudioAPISoundManager = window.webAudioAPISoundManager || new WebAudioAPISoundManager(window.audioContext);
      this.manager = window.webAudioAPISoundManager;
      this.manager.addSound(this.url);
        // this.buffer = this.manager.bufferList[this.url];
      };
    
    /*
     * WebAudioAPISound Prototype
     */
     WebAudioAPISound.prototype = {
      play: function () {
        var buffer = this.manager.bufferList[this.url];
        //Only play if it's loaded yet
        if (typeof buffer !== "undefined") {
          var source = this.makeSource(buffer);
          source.loop = this.settings.loop;
            source.start(0);
    
            if(!this.manager.playingSounds.hasOwnProperty(this.url)) {
              this.manager.playingSounds[this.url] = [];
            }
            this.manager.playingSounds[this.url].push(source);
          }
        },
        stop: function () {
          this.manager.stopSoundWithUrl(this.url);
        },
        getVolume: function () {
          return this.translateVolume(this.volume, true);
        },
        //Expect to receive in range 0-100
        setVolume: function (volume) {
          this.volume = this.translateVolume(volume);
        },
        translateVolume: function(volume, inverse){
          return inverse ? volume * 100 : volume / 100;
        },
        makeSource: function (buffer) {
          var source = this.manager.context.createBufferSource();
          var gainNode = this.manager.context.createGain();
          source.connect(gainNode);
          gainNode.gain.value = this.volume;
          source.buffer = buffer;
          // source.connect(gainNode);
          gainNode.connect(this.manager.context.destination);
          return source;
        }
      };
    
      return WebAudioAPISound;
    });
    

提交回复
热议问题