-1

I am working on a Unity App being built for iOS and Android devices. In my app I am fetching a url from a web service and that url is for an audio file (.mp3) which I am downloading and saving in the app. Below is code for the same.

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

  }

This code works perfectly in my Android and iPhone devices but on the client's Samsung S9, the audio file is not getting downloaded. Audio file is getting created in S9 but the file size is only around 6 KB while the original audio file is around 5 MB.

Now in the code you can see a url commented out.When I use the commented url then the audio download works in S9 too. So I was suspecting this to be some ip related issue but the client tried on various wifi networks but it didn't worked for him.

So I want to know is this a server issue or an ip issue or a device specific issue.

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84

1 Answers1

1

This could be due to the path you're saving the audio file. The proper path to save file in Unity is Application.persistentDataPath/FolderName/FileName.extension.

It could also be a bug with DownloadHandlerFile. Request the data with UnityWebRequest then manually save with API like File.WriteAllBytes.

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

For loading it, see this.

Programmer
  • 121,791
  • 22
  • 236
  • 328