Firefox OS中的Audio.src和路径

时间:2014-01-21 17:26:29

标签: javascript html5 firefox-os

我正在开发适用于Firefox OS的应用程序< 1.3将歌曲设置为铃声。 回购https://github.com/Mte90/RingTone-Picker-for-FirefoxOS和有问题的文件是script.js

https://github.com/Mte90/RingTone-Picker-for-FirefoxOS/blob/master/script.js#L73行中,路径正确,如“/emmc/audio.ogg”,但音频播放器返回核心错误4。 这个问题是错误的路径,但路径是正确的!

如果我在第74行添加console.log(player.src),则返回“app://strangenumberhash/emmc/audio.ogg”之类的路径。 我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

不允许使用app协议来引用打包应用中的音频/视频文件。我相信这是一个安全限制,是为了防止跨应用内容阅读。您需要在HTML中使用音频标记或使用XMLHttpRequest。类似于以下内容(视频示例):

var xhr = new XMLHttpRequest();
xhr.open('GET', 'myvid.ogg');
xhr.responseType = 'blob';
xhr.send();
xhr.onload = function() {
videoblob = new Blob([xhr.response], { type: 'video/ogg' });
    var openingVideo = new MozActivity({
            name: "open",
            data: {
                type: [
                  "video/webm",
                  "video/mp4",
                  "video/3gpp",
                  "video/mkv",
                  "video/ogg"
                ],
                blob: videoblob
            }
        });                

};
xhr.onerror = function() {
    console.log('Error loading test video', xhr.error.name);
};

如果文件在SD卡上,您有几个选择:

您可以使用挑选活动并让用户找到它:

var act = new MozActivity({
    name: 'pick',
    data: {
      type: 'audio/ogg'
    }
    }); 

或者您可以在清单中的SD卡上设置读写权限并手动读取它并使用音频标签或打开活动(非常少的错误检查)播放。

var sdcard = navigator.getDeviceStorage('sdcard');
//assumes sample.ogg is located at the top of the sdcard
var request = sdcard.get("sample.ogg");

request.onsuccess = function () {
  var file = this.result;
  console.log("Get the file: " + file.name);
  var mysrc = URL.createObjectURL(file);
  var audio1 = new Audio();
  audio1.onerror = function(e) {
    console.log(" error playing file ");
  }
  audio1.src =  mysrc;
  audio1.type = "video/ogg";
  console.log( "audio src " + audio1.src);
  audio1.play();
  URL.revokeObjectURL(url);
}
request.onerror = function () {
  console.warn("Unable to get the file: ");
}