Uploading Wav Audio File to Server
Hello, I have an mobile game that needs to record audio using the microphone and then upload these clips to the server. Been looking for a way to do this but I just can't figure how, There are examples of images being uploaded but when I follow through with the same method for Audio then that saves a blank file. I used SavWav and I was able to save the file to local memory but I am not able to figure how to upload it to a sever. Right now I have it working like this (pretty sure this is wrong)
public bool UploadToWeb(AudioClip clip)
{
var filename = "sample";
if (!filename.ToLower().EndsWith(".wav"))
{
filename += ".wav";
}
var samples = new float[clip.samples];
clip.GetData(samples, 0);
Int16[] intData = new Int16[samples.Length];
Byte[] bytesData = new Byte[samples.Length * 2];
int rescaleFactor = 32767;
for (int i = 0; i < samples.Length; i++)
{
intData[i] = (short)(samples[i] * rescaleFactor);
Byte[] byteArr = new Byte[2];
byteArr = BitConverter.GetBytes(intData[i]);
byteArr.CopyTo(bytesData, i * 2);
}
StartCoroutine (UploadFile (bytesData));
}
public IEnumerator UploadFile(Byte[] uploadBytes) {
WWWForm form = new WWWForm();
form.AddBinaryData("fileUpload", uploadBytes, "sample.wav", "audio/wav");
WWW w = new WWW(weburl, form);
yield return w;
if (!string.IsNullOrEmpty(w.error)) {
Debug.Log(w.error);
}
else {
Debug.Log("Finished Uploading Screenshot");
}
}
Here is the php script I was trying to use.
<?php
$byteArray = $_POST['audioArray'];
$myfile = fopen("sine.wav", "wb") or die("Unable to open file!");
fwrite($myfile, $byteArray);
fclose($myfile);
?>
Can someone please help me with how is this meant to be done?
Answer by khizer_awan · Nov 27, 2019 at 11:54 AM
i wanna do exact same thing, did you find any solution bro? @romitraj
Your answer
Follow this Question
Related Questions
run scipt on html webpage 1 Answer
Uploading to Photobucket 0 Answers
Unity 2019 picture upload as .jpg file 1 Answer
consume a php webservice in Unity3D C# 2 Answers
Can't get echo 1 Answer