Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by trakevital · Feb 10, 2021 at 04:06 PM · androidbugmicrophonemic

Microphone doesn't record (or save) sometimes Android Unity 20.1.10f1

I am using Unity to build an Android application. In the application I record my voice and save it to a wave file. However sometimes the mic doesn't record anything or the SavWav class doesn't save the recording file. It works fine half of the time but half of the other time it doesn't. I have tried to replicate it on a shorter piece of code but the program worked fine in it. I am suspecting that it has something to do with interference from some other part of the application or overload on the process but I can't figure it out. What my app is supposed to do is to record the persons voice while he is moving the phone and then when he finishes moving it the phone stops recording the audio and sends it to google cloud speech-to-text service and displays the words the person said. I tried replicating it but with only a simple touch or no-touch mechanism but the program seemed fine.

 public class Recorder
 {
     public const int STANDART_REC_LENGTH = 15;
     public readonly static string STANDART_REC_PATH = Application.persistentDataPath + "/recording.wav";
     public string currRecordingPath = "";
     public int currRecordingLen = STANDART_REC_LENGTH;
     private bool isRecording = false;
     public bool record = false;
     public TextMeshPro verbose;
 
     private AudioClip recording;
     private DateTime startTime;
 
     private object locker = new object();
 
     //Returns a trimmed version of the <originalClip>
     //originalClip - the originial clip
     //startPosSec - The starting position in seconds (eg: 1, 0.34, 2782.12, etc.)
     //lengthSec - The new length of the AudioClip in seconds (eg: 1, 0.34, 2782.12, etc.)
     private AudioClip TrimAudioClip(AudioClip originalClip, float startPosSec, float lengthSec)
     {
         var originalClipSamples = new float[originalClip.samples];
         originalClip.GetData(originalClipSamples, 0);
 
         //converts startPosSec & takeAmountSec from seconds to sample amount
         int newStartPosSample = (int)(startPosSec * originalClip.frequency);
         int newLengthSecSample = (int)(lengthSec * originalClip.frequency);
 
         //gets the trimmed version of the orignalClipSamples
         var newClipSamples = originalClipSamples.Skip(newStartPosSample).Take(newLengthSecSample).ToArray();
 
         //generates a new empty clip and sets its data according to the newClipSamples
         AudioClip resClip = AudioClip.Create(originalClip.name, newClipSamples.Length, originalClip.channels, originalClip.frequency, false);
         resClip.SetData(newClipSamples, 0);
 
         return resClip;
     }
    //is called on the update function
     public void UpdateRecorder()
     {
             lock (locker)
             {
                 if (record)
                 {
                     if (!isRecording)
                     {
                         recording = Microphone.Start("", false, currRecordingLen, 44100);
                         startTime = DateTime.Now;
                         isRecording = true;
                     }
 
                 }
                 else if (isRecording)
                 {
 
                     Microphone.End("");
                     if (currRecordingPath != "")
                     {
                         //trims the clip by the amount of seconds that have passed
                         if (verbose != null)
                         {
                             float[] samples = new float[recording.samples];
                             recording.GetData(samples, 0);
                             verbose.text += "avrg: " + samples.Average().ToString() + '\n';
 
                         }
                         AudioClip newClip = TrimAudioClip(recording, 0, (float)(DateTime.Now - startTime).TotalSeconds);
                         if (verbose != null)
                         {
                             float[] samples = new float[recording.samples];
                             newClip.GetData(samples, 0);
                             verbose.text += "avrg: " + samples.Average().ToString() + '\n';
 
                         }
                         //saves the clip in the given path
                         SavWav.Save(currRecordingPath, newClip);
                         isRecording = false;
                     }
 
             }
         }
     }
 }

SavWav - https://gist.github.com/drawcode/10341911

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

265 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a Unity 5 Bug affecting tags with Android builds? 2 Answers

Android 6.0 Marshmallow creates black bar along bottom of screen. 4 Answers

Unity Microphone Auto start recording and auto stop recording 0 Answers

AssetBundles don't load to scene on Android device. 3 Answers

GUI Layout 'Fading' on changing QualitySettings 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges