2017-05-03 106 views
0

我正在開發一個應用程序功能,以便將我的應用程序中的聲音傳遞給whatsapp。我已經取得了一些進展,但我堅持要獲取AudioClip的字節數組。Unity3D通過Whatsapp發送音頻剪輯

SS

所以我需要從資源文件夾中的音頻剪輯,並將其寫入我給了路徑,然後將其發送到WhatsApp的。

這會得到一個錯誤

FormatException:無效的長度。 System.Convert.FromBase64String(System.String S)(在/Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Convert.cs:146)

void sendProccess() 
    { 
     string s = Resources.Load<AudioClip>("Bava").ToString(); 

     byte[] dataToSave = System.Convert.FromBase64String(s); 

     string destination = Path.Combine(Application.persistentDataPath, System.DateTime.Now.ToString("yyyy-MM-dd-HHmmss") + ".mp3"); 

     File.WriteAllBytes(destination, dataToSave); 


     if (!Application.isEditor) 
     { 
      //instantiate the class Intent 
      AndroidJavaClass intentClass = new AndroidJavaClass("android.content.Intent"); 
      AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent"); 
      intentObject.Call<AndroidJavaObject>("setAction", intentClass.GetStatic<string>("ACTION_SEND")); 

      //instantiate the class Uri 
      AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); 
      AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", "file://" + destination); 

      //call putExtra with the uri object of the file 
      intentObject.Call<AndroidJavaObject>("putExtra", intentClass.GetStatic<string>("EXTRA_STREAM"), uriObject); 
      intentObject.Call<AndroidJavaObject>("setType", "audio/mp3"); 

      //instantiate the class UnityPlayer 
      AndroidJavaClass unity = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 

      //instantiate the object currentActivity 
      AndroidJavaObject currentActivity = unity.GetStatic<AndroidJavaObject>("currentActivity"); 

      //call the activity with our Intent 
      currentActivity.Call("startActivity", intentObject); 
     } 
    } 

預先感謝。

+1

所以...你的問題是'如何將音頻轉換成Base64?'。還把你的代碼放入問題中,而不是圖像。 – Smartis

+0

是的,這是我有困難的部分,我添加了代碼,對不起,我是新的。 –

回答

0

在下面的行 -

string s = Resources.Load<AudioClip>("Bava").ToString(); 

ToString方法returns the name of the object

您應該使用AudioClip.GetData方法從音頻剪輯獲得原始數據 -

AudioClip clip = Resources.Load<AudioClip>("Bava") 
float[] samples = new float[clip.samples * clip.channels]; 
clip.GetData(samples, 0); 

然後你可以samples數組轉換爲使用的BinaryWriter或BitConverter進一步使用其字節表示。