Search Unity

Saving mp3 files to caches and playing them

Discussion in 'Editor & General Support' started by ThomasVandenberghe, Jul 15, 2014.

  1. ThomasVandenberghe

    ThomasVandenberghe

    Joined:
    Feb 28, 2014
    Posts:
    22
    Hi, currently I've written a basic caching script, but now I'd need to be able to store audio files.

    This is the code where I'm checking the cache and/or download the file.

    Code (CSharp):
    1. private IEnumerator CoroutineLoadFromCacheOrDownload(string url, int version, CachingType cachingType, Action<float> progress, Action<WWW, string> callBack)
    2.         {
    3.             bool wwwReturned = false;
    4.             string cachePath = Application.temporaryCachePath + "/" + GetHashString(url + version);
    5.  
    6.             // Only check cache if version is > 0
    7.             if (version > 0) {
    8.                 // Check cache first if there is a file present
    9.                 using (WWW www = new WWW("file://" + cachePath)) {
    10.                     while (!www.isDone) {
    11.                         if (progress != null)
    12.                             progress(www.progress);
    13.                         yield return 0;
    14.                     }
    15.                     yield return www;
    16.                     if (www.error != null) {
    17.                         Debug.Log("Didn't find a file in the cache. Proceeding to download from url.\n" + www.error);
    18.                         // Didn't find a file in the cache.
    19.                         // Code will proceed to download it from the url.
    20.                     }
    21.                     else {
    22.                         // A file was found in the cache. Return this file, and skip the download.
    23.                         if (progress != null)
    24.                             progress(1);
    25.                         if (callBack != null)
    26.                             callBack(www, cachePath);
    27.                         wwwReturned = true;
    28.                     }
    29.                 }
    30.             }
    31.  
    32.             if (!wwwReturned) {
    33.                 using (WWW www = new WWW(url)) {
    34.                     while (!www.isDone) {
    35.                         if (progress != null)
    36.                             progress(www.progress);
    37.                         yield return 0;
    38.                     }
    39.                     yield return www;
    40.                     if (www.error != null) {
    41.                         Debug.Log("Error downloading file :: " + www.error);
    42.                         if (callBack != null)
    43.                             callBack(null, "");
    44.                     }
    45.                     else {
    46.                         // Only cache if version is 0 or higher
    47.                         if (version >= 0) {
    48.                             switch(cachingType) {
    49.                             case CachingType.TEXTURE: // Save Texture2D to file
    50.                                 {
    51.                                     FileStream file = File.Open(cachePath, FileMode.Create);
    52. #if UNITY_IPHONE
    53.                                     iPhone.SetNoBackupFlag(cachePath);
    54. #endif
    55.                                     BinaryWriter binaryWriter = new BinaryWriter(file);
    56.                                     binaryWriter.Write(www.bytes);
    57.                                     file.Close();
    58.                                 }
    59.                                 break;
    60.                             case CachingType.TEXTASSET: // Save text to file
    61.                                 {
    62.                                     FileStream file = File.Open(cachePath, FileMode.Create);
    63. #if UNITY_IPHONE
    64.                                     iPhone.SetNoBackupFlag(cachePath);
    65. #endif
    66.                                     StreamWriter streamWriter = new StreamWriter(file);
    67.                                     streamWriter.Write(www.text);
    68.                                     streamWriter.Close();
    69.                                     file.Close();
    70.                                 }
    71.                                 break;
    72.                             case CachingType.VIDEO: // Save video to file
    73.                                 {
    74.                                     FileStream file = File.Open(cachePath, FileMode.Create);
    75. #if UNITY_IPHONE
    76.                                     iPhone.SetNoBackupFlag(cachePath);
    77. #endif
    78.                                     BinaryWriter binaryWriter = new BinaryWriter(file);
    79.                                     binaryWriter.Write(www.bytes);
    80.                                     file.Close();
    81.                                 }
    82.                                 break;
    83.                             case CachingType.AUDIO:
    84.                                 {
    85.                                     FileStream file = File.Open(cachePath, FileMode.Create);
    86. #if UNITY_IPHONE
    87.                                     iPhone.SetNoBackupFlag(cachePath);
    88. #endif
    89.                                     BinaryWriter binaryWriter = new BinaryWriter(file);
    90.                                     binaryWriter.Write(www.bytes);
    91.                                     file.Close();
    92.                                 }
    93.                                 break;
    94.                             default:
    95.                                 throw new Exception("Trying to save unknown file type.");
    96.                             }
    97.                         }
    98.                         if (progress != null)
    99.                             progress(1);
    100.                         if (callBack != null)
    101.                             callBack(www, cachePath);
    102.                     }
    103.                 }
    104.             }
    105.         }
    When I'm accessing the file for the first time, it succesfully downloads and saves the data. The first time it's downloaded from the server, the second time from the cache. When downloaded from the cache however, no audio plays. Length and samples are both 0.

    Code (CSharp):
    1. if (www != null)
    2.             {
    3.                 Debug.Log("Byte size :: " + value.bytes.Length);
    4.  
    5. //                audioClip = value.GetAudioClip(false, false, AudioType.MPEG);
    6.                 audioClip = value.audioClip;
    7.  
    8.                 Debug.Log(audioClip.isReadyToPlay);
    9.  
    10.                 Debug.Log("url :: " + value.url);
    11.                 Debug.Log("audioClip :: " + audioClip + " :: " + audioClip.length + " :: " + audioClip.samples);
    12.                
    13.                 audioReady = true;
    14.  
    15.                 TryAndPlayVideo();
    16.             }
    When I export the saved data via iExplorer, I can open and play the file as it should be.
    This method works on mac and android, but seems to fail on iOS.


    Any idea how I would be able to save mp3 files and play them from the device again?

    Kind regards,
    Thomas
     
  2. anthonyk2

    anthonyk2

    Joined:
    Sep 18, 2012
    Posts:
    47
    Hi Thomas, have you found any solution with iOS ?
    Thanks !
     
  3. CMDi-admin

    CMDi-admin

    Joined:
    Jul 23, 2013
    Posts:
    15
    I've been using this code to save the file when downloading:
    Code (CSharp):
    1. FileStream file = File.Open(path, FileMode.Create);
    2. BinaryWriter writer = new BinaryWriter(file);
    3. writer.Write(www.bytes);
    4. writer.Close();
    5. file.Close();
    You have to make sure you set the path extension to whatever it is you're using (mp3, wav...). This was the main issue in not being able to play the sound again.
     
  4. anthonyk2

    anthonyk2

    Joined:
    Sep 18, 2012
    Posts:
    47
    Nice thank you ! :)