Search Unity

Can't get path to StreamingAssets in Android

Discussion in 'Scripting' started by IRALTA, Nov 21, 2017.

  1. IRALTA

    IRALTA

    Joined:
    Apr 7, 2015
    Posts:
    14
    Hello,

    I'm trying to use Facebook SDK 360 for ambisonic sound. At Mac it works fine, but when I try to play at Android devices doesn't works. Now I know that the problem is the path for load the audio. Facebook 360 SDK use StreamingAssets to save there a .tbe archive, and Unity has little problems to load StreamingAssets as PC/Mac. The default code is this:
    Code (CSharp):
    1.  private string resolvePath(string path, PathType type)
    2.          {
    3.              if (type == PathType.STREAMING_ASSETS)
    4.              {
    5.                  string streamingAssetsPath = Path.Combine(Application.streamingAssetsPath, path);
    6.                  if (streamingAssetsPath.Contains("apk!") && Application.platform == RuntimePlatform.Android)
    7.                  {
    8.                      return "asset:///" + Path.GetFileName(streamingAssetsPath);
    9.                  }
    10.                  return streamingAssetsPath;
    11.              }
    12.            
    13.              return path;
    14.          }
    I try to use WWW as Unity say and I made this another code:
    Code (CSharp):
    1. private string resolvePath(string path, PathType type)
    2.              {
    3.                  Debug.Log("WWWWWQW!");
    4.                      string streamingAssetsPath = Path.Combine(Application.streamingAssetsPath, path);
    5.    
    6.                     if(!Application.isEditor)
    7.                     {
    8.                        
    9.                         string dbPath;
    10.    
    11.    
    12.    
    13.                        WWW reader = new WWW(streamingAssetsPath);
    14.                         while (!reader.isDone)
    15.                         {
    16.                            
    17.                         }
    18.    
    19.                         string realPath = Application.persistentDataPath + path;
    20.                         System.IO.File.WriteAllBytes(realPath,reader.bytes);
    21.    
    22.                         dbPath = realPath;
    23.                         streamingAssetsPath = dbPath;
    24.                     }
    25.    
    26.                      return streamingAssetsPath;
    27.    
    28.              }
    It worked once, but now the app doesn't load nothing T.T

    Does anyone have any suggestion to make it work?

    Thanks in advance :)
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    The problem with StreamingAssets on Android is that they are stored inside APK, which is a zip file, so regular file access is not available. So you general approach is good.
    However, they way you use WWW is not good. You should use coroutine instead. And check for errors too.
     
  3. IRALTA

    IRALTA

    Joined:
    Apr 7, 2015
    Posts:
    14
    This is my code now and still not working
    Code (CSharp):
    1.  private void resolvePath(string path)
    2.         {
    3.  
    4.             if (!Application.isEditor)
    5.             {
    6.                 StartCoroutine(CargarAudio(path));
    7.             }
    8.             else
    9.             {
    10.                 pathStreamingAssets = Path.Combine(Application.streamingAssetsPath, path);
    11.                 //text = GameObject.Find("welcome").GetComponent<Text>();
    12.                 //text.text = pathStreamingAssets;
    13.             }
    14.        
    15.                
    16.         }
    17.         IEnumerator CargarAudio(string path)
    18.         {
    19.             string streamingAssetsPath = Path.Combine(Application.streamingAssetsPath, path);
    20.             if(!Application.isEditor)
    21.                {
    22.                   string dbPath;
    23.                   WWW reader = new WWW(streamingAssetsPath);
    24.                    while (!reader.isDone)
    25.                    {
    26.                      yield return null;
    27.                    }
    28.  
    29.                    string realPath = Application.persistentDataPath + path;
    30.                    System.IO.File.WriteAllBytes(realPath,reader.bytes);
    31.  
    32.                    dbPath = realPath;
    33.                    streamingAssetsPath = dbPath;
    34.                    pathStreamingAssets= streamingAssetsPath;
    35.                    text = GameObject.Find("welcome").GetComponent<Text>();
    36.                 text.text = pathStreamingAssets;
    37.                
    38.                 ///storage/emulated/0/Android/data/com.Iralta.SecuritasVideo80/filesVoiceDirections.tbe
    39.                }
    40.         }
    The device display this (/storage/emulated/0/Android/data/com.Iralta.SecuritasVideo80/filesVoiceDirections.tbe) url to get the audio (VoiceDirections.tbe is the name of the audio), but still not working.
    It's seems like its something wrong when get files and the name of audio without / or the name of the folder
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,736
    What error do you get? At which point?
    Is it File.WriteAllBytes() that fail? If so, it should throw exception that tells something.
    Bling guess: try using Path.Combine() in the following line
    string realPath = Application.persistentDataPath + path;
     
  5. IRALTA

    IRALTA

    Joined:
    Apr 7, 2015
    Posts:
    14
    I don't get any error, but when I try to load the audio file, the path is wrong and it doesn't load nothing.
    The best path I can get is /storage/emulated/0/Android/data/com.Iralta.SecuritasVideo80/filesVoiceDirections.tbe but I think something is missing between "files" and "VoiceDirections.tbe".

    I'm going to use Path.Combine() and see if I can add a "/" between files and VoiceDirections
     
  6. IRALTA

    IRALTA

    Joined:
    Apr 7, 2015
    Posts:
    14
    Still not working with Path.Combine..
    I opened apk in my pc and VoiceDirections was in folder assets directly, I don't know why I can't get the path,
     
  7. akayashi

    akayashi

    Joined:
    Nov 11, 2019
    Posts:
    1
    same issue, any solution? :((