简体   繁体   中英

Unity3D Send AudioClip Through Whatsapp

I am working on an app feature to be able to pass the sounds that I have in my app to whatsapp. I've made some progress but I am stuck at getting byte array of an AudioClip.

SS

So I need to get the audioclip from the resources folder and write it to the path I gave, and then send it to whatsapp.

This gets an error

FormatException: Invalid length. System.Convert.FromBase64String (System.String s) (at /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);
        }
    }

Thanks in advance.

In following line -

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

The ToString method returns the name of the object .

You should use AudioClip.GetData method to get raw data from audio clip -

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

Then you can convert the samples array to its byte representation using BinaryWriter or BitConverter for further use.

这对我来说很完美

 Application.OpenURL("whatsapp://send?phone=9999878653&text=[hello]");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM