0

I have have a string stored in a MySQL database.

I want to pull the string from the database, break it into char elements and attach sound files to each letter. The letters will be displayed in a web page and when clicked, will play a sound which represents that letter.

I have tried using the .split() function, but have not had much success. Has anyone seen anything similar to this or does anyone have any suggestions?

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
skad
  • 19
  • 4
  • 2
    Can you paste what you tried? Are you just trying to break up : 'whatagreatsong' into w h a t ... ? – Nix Jan 05 '13 at 15:51
  • 1
    Showing us your could make it easy to suggest solution. Why split did not help you? – Adil Jan 05 '13 at 15:53

3 Answers3

1
var char = [],
    str, // your string
    i,
    len = str.length
for (i = 0; i < len ; i++){
    char[i] = str.charAt(i);
}

// now you have an array of characters

// setup event handlers for the click action

// setup audio output
  • This seems very helpful. If I break 'AND' into 'A' 'N' 'D'. How could I attach the correct sound to each char dynamically? Somthing like, if(char[i] == ('a'){ char[i].playclip(a) } Am I roughly on the right track? Sorry, Im pretty new to javaScript. – skad Jan 05 '13 at 16:37
  • you said "when clicked" so you would need to set up event handlers for a click event. Also you need need to setup and element for each letter. –  Jan 05 '13 at 17:35
1

If I look here

Sound effects in JavaScript / HTML5

you can try

var chars = "<? echo $string; ?>".split("");
var sounds = [];
$.each(chars,function(i,char) {

  if (!sounds[char]) sounds[char] = new Audio(char+".wav"); // buffers automatically when created
  $("#container").append('<a href="#" onclick="sounds[this.innerHTML].play();return false">'+char+'</a>');
});
Community
  • 1
  • 1
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

To pull of the split its relatively simple, you can use traditional JS, or jQuery, all you need to do is plug in your markup generation to the below.

var str = "No clue what you are trying";
for (var x = 0; x < str.length; x++){
    var myC = str.charAt(x);
    //or var myC = str[x];
    //what do you want to do?
}

//jQuery overkill
$.each(
    str.split(''),
    function( idx, myC ){
        //what do you want to do?
    };
);
Nix
  • 57,072
  • 29
  • 149
  • 198