在JSON文件中存储对MP3文件的引用是否可行?

huangapple go评论58阅读模式
英文:

Is there a way to store a reference to an MP3 file in a JSON file?

问题

我正在使用Unity制作一个节奏游戏,其中我将所需的谱表信息存储在一个JSON文件中。尽管像速度、名称和音符数组这样的信息相对简单,但当涉及到存储指向包含我想要播放的音频的MP3文件的引用时,我感到困惑。

在Unity中,我正在使用一个自定义类来存储与文件匹配的数据:

using UnityEngine;

[System.Serializable]
public class MusicChart
{
     public string name;
     public float startDelay;
     public float tempo;
     public float finishBeat;
     public Timing[] timings;
}

这里的一切都按预期工作,我只需要额外的内容附加到JSON文件和这个类,以使其正常工作。对不起,我对Unity还不熟悉,也不太了解JSON可能涉及的复杂性。我认为将路径存储为字符串,然后在Unity中的脚本中读取该路径会起作用,但我不知道如何执行后者。

英文:

I'm working on a rhythm game in Unity where I store the information needed for a given chart in a JSON file. While information like tempo, name, and an array of notes are fairly straightforward, I've been lost when it comes to storing a reference to an MP3 file that contains the audio I want to play alongside a rhythm chart.

With Unity, I'm using a custom class that stores the data that matches the file:

using UnityEngine;

[System.Serializable]
public class MusicChart
{
     public string name;
     public float startDelay;
     public float tempo;
     public float finishBeat;
     public Timing[] timings;
}

Everything here works as intended, I just need something extra to append to the JSON file and this class that will make it work. Apologies for my ignorance, I'm both new to Unity and not well-versed in any complexities JSON might have. I figure the idea of storing the path as a string, then reading that path in on a script in Unity would work, but I don't know how to do the latter part.

答案1

得分: 1

  1. 存储绝对或相对路径到你的 json 文件中的 mp3 文件。这将有风险,如果路径不正确,或者有人仅复制 json 文件或重命名 mp3 文件。要实现这一点,只需在你的类中添加一个 string musicPath 字段。
  2. 将 mp3 文件存储在你的 json 中作为 base64 编码的字符串。这将使你的 json 文件更难阅读,由于编码效率低下,会浪费一些空间。
  3. 将你的 json 和 mp3 文件存储在一个 zip 存档中。将 mp3 文件的条目名称存储在你的 json 文件中。这确保用户只看到一个单一文件,但会使手动创建或编辑文件变得有些繁琐。你可以将 mp3 文件标记为“存储”以避免压缩开销。

在所有情况下,你需要知道如何读取和使用表示 mp3 数据的流。

英文:

There are a few options:

  1. Store the absolute or relative path to your mp3 file in your json file. This will have the risk that the path is incorrect, is someone copies only the json file, or renames the mp3 file. To do this just add a string musicPath field to your class.
  2. Store the mp3 file inside your json as a base64 encoded string. This will make your json file harder to read, and it will waste some space due to the inefficient encoding.
  3. Store your json and mp3 file inside a zip-archive. Store the entry name of your mp3 file inside your json file. This ensures that the user only sees a single file, but makes it a bit more cumbersome to create or edit files by hand. You can mark the mp3 file as "store" to avoid the compression overhead.

In all cases you will need to know how read and use the stream representing the mp3 data.

答案2

得分: -1

U can convert mp3 file to Base64String and avoiding loosing it :

将 mp3 文件转换为 Base64 字符串以避免丢失它:

var audioBytes = File.ReadAllBytes("PATHTOAudio");
var base64String = Convert.ToBase64String(audioBytes);

使用此方法播放 mp3 文件:

使用此方法播放 mp3 文件:

static void PlaySound(string base64String)
{
var audioBuffer = Convert.FromBase64String(base64String);
using (var ms = new MemoryStream(audioBuffer))
{
var player = new System.Media.SoundPlayer(ms);
player.Play();
}
}

英文:

U can convert mp3 file to Base64String and avoiding loosing it :

  var audioBytes = File.ReadAllBytes("PATHTOAudio");
  var base64String = Convert.ToBase64String(audioBytes);

use This method to To play mp3 File :

static void PlaySoud(string base64String)
    {
        var audioBuffer = Convert.FromBase64String(base64String);
        using (var ms = new MemoryStream(audioBuffer))
        {
            var player = new System.Media.SoundPlayer(ms);
            player.Play();
        }
    }

huangapple
  • 本文由 发表于 2023年4月11日 10:20:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定