Search code examples
javascripthtmlfirefox-os

Audio.src and paths in Firefox OS


I'm working on an app for Firefox OS < 1.3 to set your songs as ringtones. The repo https://github.com/Mte90/RingTone-Picker-for-FirefoxOS and the file with the problem is script.js

In the line https://github.com/Mte90/RingTone-Picker-for-FirefoxOS/blob/master/script.js#L73 the path it's correct like "/emmc/audio.ogg" but the audio player return core error 4. This problem is for a wrong path but the path is correct!

If i add on the line 74 console.log(player.src) return a path like "app://strangenumberhash/emmc/audio.ogg". I have no absolutely idea how to fix this problem.


Solution

  • The app protocol is not allowed to be used to reference audio/video files within a packaged app. I believe this is a security restriction is to prevent cross app content reading. You need to either use an audio tag in your HTML or use an XMLHttpRequest. Something like the following (video example):

    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);
    };
    

    If the file is on the SDCard you have a couple of options:

    One you could just use a pick activity and let the user locate it:

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

    or you can set the readwrite permission on the sdcard in the manifest and read it manually and play it with the audio tag or with a open activity (very little error checking).

    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: ");
    }