Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Cannot play audio from persistent storage (Unity 2020.2.1f6)

Discussion in 'WebGL' started by jesper42, Mar 12, 2021.

  1. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    We store audio (and textures) in the persistent storage.

    In editor and for other build targets we play audio with:


       var sound = Path.Combine(Application.persistentDataPath, "test.mp3");
    using var webRequest = UnityWebRequestMultimedia.GetAudioClip(sound, AudioType.MPEG);
    yield return webRequest.SendWebRequest();
    var clip = ((DownloadHandlerAudioClip)webRequest.downloadHandler).audioClip;
    audioSrc.PlayOneShot(clip, 1);


    But, on WebGl this yields this error:

        :49411/idbfs/6d977885fbd1c5ead313d73fe6d870bd/test.mp3:1 Failed to load resource: the server responded with a status of 404 (Not Found)


    If I try to prefix it with "file://" we get an access error:

        Not allowed to load local resource: file:///idbfs/33ef7b0b7a8dd7d8c894e4046f92f4f3/test.mp3


    I've tried many different paths, but none seems to work.

    ---

    Observations:
    - The sounds are there in the IndexedDB.
    - I've tried Chrome, Firefox and Edge. No luck.
    - Our textures are loaded from the same persistent storage without problems.

    Any help would be greatly appreciated :)

    Cheers,
    Jesper
     
  2. jesper42

    jesper42

    Joined:
    Jan 11, 2011
    Posts:
    28
    ps. I've also tried to play the sounds "directly" with a jslib "plugin".

    But, I cannot get this to work either ... sigh :(

    How do you play a sound when you have the object and not the url?

    help ... pls :)

       PlaySound: function (pFile) {
    var file = Pointer_stringify(pFile);
    console.log('PlaySound: ' + file);

    // IndexedDB
    window.indexedDB = window.indexedDB || window.webkitIndexedDB ||
    window.mozIndexedDB || window.OIndexedDB ||
    window.msIndexedDB,
    IDBTransaction = window.IDBTransaction ||
    window.webkitIDBTransaction ||
    window.OIDBTransaction || window.msIDBTransaction,
    dbVersion = 1.0;

    var indexedDB = window.indexedDB;

    var request = indexedDB.open("/idbfs");

    request.onerror = function (event) {
    console.log("Failed to Open the indexedDB database");
    };


    request.onsuccess = function (event) {
    console.log("Succesfully opened the /idbfs db");
    db = request.result;

    // Open a transaction to the database
    var transaction = db.transaction(["FILE_DATA"], "readwrite");

    transaction.objectStore("FILE_DATA").get(file).onsuccess =
    function (event) {
    console.log("Retrieved sound from db: " + event.target.result);
    var audioFile = event.target.result;

    var URL = window.URL || window.webkitURL;
    var videoURL = URL.createObjectURL(audioFile);

    var audio = new Audio();
    audio.src = videoURL;
    audio.play();
    };
    }
    }
     
  3. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    277
    For testing, I once placed the mp3 file in the StreamingAssets folder and saved it in indexedDB with the following code.
    After that, I tried UnityWebRequestMultimedia.GetAudioClip() and was able to play the mp3 file.
    Note: Your browser will not be able to autoplay audio without your permission (or your site has a low score).

    Code (CSharp):
    1.  
    2.         var req = UnityWebRequest.Get(Path.Combine(Application.streamingAssetsPath, "test.mp3"));
    3.         yield return req.SendWebRequest();
    4.         var data = req.downloadHandler.data;
    5.         var savePath = Path.Combine(Application.persistentDataPath, "test.mp3");
    6.         File.WriteAllBytes(savePath, data);
    7.  
     
    jesper42 likes this.