Esempio n. 1
0
    //TODO: Fix false positive when overwriting existing files
    // Maybe a check between the files date metadata and the current time?
    /// <summary>
    /// This method takes in a AudioClip, file name, and file directory to create a
    /// byte array and write it to the proper directory. Returns true if the file
    /// is successfully saved.
    /// </summary>
    /// <param name="clip"></param>
    /// <param name="fileName"></param>
    /// <param name="fileDir"></param>
    /// <returns>bool</returns>
    private static bool SaveAudioToDir(AudioClip clip, string fileName, string fileDir)
    {
        //Check if the word audio directory exists, and if not then create it. This should only be needed when playing for first time.
        if (!Directory.Exists(Application.persistentDataPath + "/WordAudio"))
        {
            Debug.Log("Creating the /WordAudio directory");
            Directory.CreateDirectory(Application.persistentDataPath + "/WordAudio");
        }

        //Check if
        if (!Directory.Exists(fileDir))
        {
            Debug.Log("Creating new directory for " + fileName + " at: " + fileDir + " for audioclip:" + clip.name);
            Directory.CreateDirectory(fileDir);
        }
        else
        {
            Debug.Log("Error: That directory already exists!");
        }

        // Convert audio clip to wav encoded bytes
        byte[] bytes = WavEncoder.FromAudioClip(clip);

        // Get the file path and write bytes to the file
        string filePath = fileDir + "/" + fileName.ToLower() + ".wav";

        File.WriteAllBytes(filePath, bytes);

        return(File.Exists(filePath));
    }