I have a list of vocabulary (ex. http://nihongoup.com/vocabulary/animals/) where each word is associated with an audio file whose name is composed of the kanji for the word (first column in the list) and it's reading in hiragana (second column). For example, the audio file for 動物 is called 動物_どうぶつ (mp3 and wav).
Audio button code:
<span onclick="playAudio('/files/audio/words/動物_どうぶつ');" class="btn-audio"></span>
JavaScript that embeds the audio file:
var audioEmbed = null;
function playAudio(which)
{
if (audioEmbed)
{
document.body.removeChild(audioEmbed);
audioEmbed.removed = true;
audioEmbed = null;
}
audioEmbed = document.createElement("audio");
var mp3Embed = document.createElement("source");
mp3Embed.setAttribute("src", which + ".mp3");
mp3Embed.setAttribute("type", "audio/mpeg");
audioEmbed.appendChild(mp3Embed);
var wavEmbed = document.createElement("source");
wavEmbed.setAttribute("src", which + ".wav");
wavEmbed.setAt开发者_开发问答tribute("type", "audio/x-wav");
audioEmbed.appendChild(wavEmbed);
audioEmbed.setAttribute("autoplay", true);
audioEmbed.removed = false;
document.body.appendChild(audioEmbed);
}
For some reason, the audio plays fine in all browsers except Firefox. If I change the name of the files to something written in Latin characters, the sound plays fine too. Is this a bug in Firefox and is there any way of solving this problem? Thanks!
It looks like these WAV files are encoded as 24-bit mono PCM. Firefox's WAV decoder only supports 8-bit and 16-bit PCM encodings, so it can't play these files. See https://bugzilla.mozilla.org/show_bug.cgi?id=524109
That should be unrelated to the filename; perhaps the Latin filename you tested pointed to a WAV file with a different encoding?
The "simple" solution is to convert all the WAV files involved to 16-bit PCM...
Try the JavaScript function encodeURI(), eg:
var mp3Embed = document.createElement("source");
mp3Embed.setAttribute("src", encodeURI(which + ".mp3"));
精彩评论