【问题标题】:Unity Audio download issue on certain devices某些设备上的 Unity 音频下载问题
【发布时间】:2018-07-07 00:06:45
【问题描述】:

我正在开发一个为 iOS 和 Android 设备构建的 Unity 应用程序。在我的应用程序中,我从 Web 服务获取一个 url,该 url 用于我正在下载并保存在应用程序中的音频文件 (.mp3)。下面是相同的代码。

if (!File.Exists(audioPath + url.Key + currentDataSetID + FindAudioType(url.Value))){
UnityWebRequest www = new UnityWebRequest("http://www.streetartmankind.org/vuforiadashboard" + url.Value);

// UnityWebRequest www = new UnityWebRequest("http://likewapdownload.com/load/Full_Mp3_Songs/2013_Bollywood_MP3_Songs/ABCD_Any_Body_Can_Dance/Bezubaan.mp3");

www.downloadHandler = new DownloadHandlerFile(audioPath + url.Key +currentDataSetID + FindAudioType(url.Value));

yield return www.SendWebRequest();

if (www.isNetworkError || www.isHttpError){
    Debug.Log(www.error);
    }
    else
        {
          Debug.Log("Audio Downloaded - " + www.url);
          Debug.Log("File successfully downloaded and saved to " + audioPath);

          Debug.Log(www.url + "\n" + audioPath + url.Key +
                        currentDataSetID + FindAudioType(url.Value));
        }

  }

此代码在我的 Android 和 iPhone 设备上完美运行,但在客户端的三星 S9 上,音频文件无法下载。 音频文件在 S9 中创建,但文件大小仅为 6 KB 左右,而原始音频文件约为 5 MB。

现在在代码中你可以看到一个被注释掉的 url。当我使用注释的 url 时,音频下载也可以在 S9 中工作。 所以我怀疑这是一些与 ip 相关的问题,但客户端尝试了各种 wifi 网络,但对他没有用。

所以我想知道这是服务器问题还是 ip 问题或设备特定问题。

【问题讨论】:

  • 这可能是DownloadHandlerFile 的错误。为什么不尝试手动保存文件?
  • @Programmer 你能举个例子吗?
  • 提供一个作为答案
  • @Programmer 感谢您的回答。我正在检查
  • 对于所有正在编辑此问题的人,请不要删除注释代码。这是我的问题的一部分。请在删除之前阅读问题。

标签: c# unity3d audio


【解决方案1】:

这可能与您保存音频文件的路径有关。 Unity中保存文件的正确路径是Application.persistentDataPath/FolderName/FileName.extension

这也可能是DownloadHandlerFile 的错误。使用UnityWebRequest 请求数据,然后使用File.WriteAllBytes 等API 手动保存。

void Start()
{
    StartCoroutine(GetRequest("http:///www.yoururl.com"));
}

IEnumerator GetRequest(string uri)
{
    UnityWebRequest uwr = UnityWebRequest.Get(uri);
    yield return uwr.SendWebRequest();

    if (uwr.isHttpError || uwr.isNetworkError)
    {
        Debug.Log("Error While Sending: " + uwr.error);
    }
    else
    {
        Debug.Log("Downloaded");

        string dataFileName = "FileName";
        string tempPath = Path.Combine(Application.persistentDataPath, "Audio");
        tempPath = Path.Combine(tempPath, dataFileName + ".mp3");

        SaveFile(tempPath, uwr.downloadHandler.data);
    }
}

void SaveFile(string path, byte[] fileBytes)
{
    //Create Directory if it does not exist
    if (!Directory.Exists(Path.GetDirectoryName(path)))
    {
        Directory.CreateDirectory(Path.GetDirectoryName(path));
    }

    try
    {
        File.WriteAllBytes(path, fileBytes);
        Debug.Log("Saved Data to: " + path.Replace("/", "\\"));
    }
    catch (Exception e)
    {
        Debug.LogWarning("Failed To Save Data to: " + path.Replace("/", "\\"));
        Debug.LogWarning("Error: " + e.Message);
    }
}

要加载它,请参阅this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 2018-04-01
    • 2021-03-14
    • 2017-07-12
    相关资源
    最近更新 更多