具有日文文件名的音频文件无法在Firefox中播放

时间:2011-05-12 15:15:40

标签: javascript firefox html5 unicode audio

我有一个词汇表(例如http://nihongoup.com/vocabulary/animals/),其中每个单词都与一个音频文件相关联,该名称由单词的汉字(列表中的第一列)组成,并且在平假名中读取(第二栏)。例如,动物的音频文件称为动物_どうぶつ(mp3和wav)。

音频按键代码:

<span onclick="playAudio('/files/audio/words/動物_どうぶつ');" class="btn-audio"></span>

嵌入音频文件的JavaScript:

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.setAttribute("type", "audio/x-wav");
    audioEmbed.appendChild(wavEmbed); 
    audioEmbed.setAttribute("autoplay", true);
    audioEmbed.removed = false;
    document.body.appendChild(audioEmbed);
}

出于某种原因,除了Firefox之外,所有浏览器中的音频都能正常播放。如果我将文件的名称更改为用拉丁字符编写的内容,则声音也可以正常播放。这是Firefox中的一个错误,有没有办法解决这个问题?谢谢!

2 个答案:

答案 0 :(得分:3)

看起来这些WAV文件被编码为24位单声道PCM。 Firefox的WAV解码器仅支持8位和16位PCM编码,因此无法播放这些文件。见https://bugzilla.mozilla.org/show_bug.cgi?id=524109

这应该与文件名无关;也许您测试的拉丁文件名指向具有不同编码的WAV文件?

“简单”的解决方案是将所有涉及的WAV文件转换为16位PCM ...

答案 1 :(得分:1)

尝试使用JavaScript函数encodeURI(),例如:

    var mp3Embed = document.createElement("source");  
    mp3Embed.setAttribute("src", encodeURI(which + ".mp3"));
相关问题