0

Using jQuery I am going to incorporate audio in an application. I am attempting to do this by creating an object called Audio_types which has a method called soundbits. I have been using this post Play an audio file using jQuery when a button is clicked to assist me in accomplishing this task.

My audio file is not playing. I have after reviewing my code several times but I am unable to identify what I am doing wrong. I have created a jsfiddle: http://jsfiddle.net/gD4Dr/1433/

Here is a glance at the code.

var Audio_types = {
              "mp3": "audio/mpeg",
                  "mp4": "audio/mp4",
                  "ogg": "audio/ogg",
                  "wav": "audio/wav",

              soundbits: function (sound) {
                  var audio_element = document.createElement('audio');
                  if (audio_element.canPlayType) {
                      for (var i = 0; i < arguments.length; i++) {
                          var source_element = document.createElement('source');
                          source_element.setAttribute('src', arguments[i]);
                      }
                  }

                  if (arguments[i].match(/\.(\w+)$/i)) {
                      source_element.setAttribute('type', Audio_types[RegExp.$1]);
                      audio_element.appendChild(source_element);
                  }
                  audio_element.load();
                  audio_element.playclip = function () {
                      audio_element.currentTime = 0;
                      audio_element.play();
                  }

                  return audio_element;

              }
          }

          var buzz = Audio_types.soundbits( 'http://www.uscis.gov/files/nativedocuments/Track%2093.mp3');

          buzz.playclip();
Community
  • 1
  • 1
user3574939
  • 819
  • 3
  • 17
  • 34

1 Answers1

0

This solution works but if anyone has a better solution please post.

$(document).ready(function () {

  var Audiotypes = {
          "mp3": "audio/mpeg",
          "mp4": "audio/mp4",
          "ogg": "audio/ogg",
          "wav": "audio/wav",

      soundbits: function (sound) {
          var audio_element = document.createElement('audio')
          if (audio_element.canPlayType) {
              for (var i = 0; i < arguments.length; i++) {
                  var source_element = document.createElement('source')
                  source_element.setAttribute('src', arguments[i])
                  if (arguments[i].match(/\.(\w+)$/i)) source_element.setAttribute('type', Audiotypes[RegExp.$1])
                  audio_element.appendChild(source_element)
                  audio_element.load()
                  audio_element.currentTime = 0
                  audio_element.play()

              }
          }
      }
  }
  var buzz = Audiotypes.soundbits('http://www.uscis.gov/files/nativedocuments/Track%2093.mp3');

 buzz.play();

});

user3574939
  • 819
  • 3
  • 17
  • 34