Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 abcname · Aug 17, 2015 at 10:50 AM · androidaudiopluginjava

use own sound recorder android library in unity

I want record and save audio in unity3d with my own library (without unity classes); so I write a library and export jar and then import it to unity

library :

import java.io.File; import java.io.IOException;

 import android.media.MediaMuxer.OutputFormat;
 import android.media.MediaRecorder;
 import android.media.MediaRecorder.AudioEncoder;
 import android.media.MediaRecorder.AudioSource;
 import android.os.Environment;
 import android.util.Log;
 
 public class SoundRecorder {
     MediaRecorder mediaRecorder;
     String soundName;
     String diractory;
     File soundFile;
     File directoryFile;
     String lastError = "";
     boolean isInRecord = false;
     
     void init(String directory, String soundName)
     {
         this.soundName = soundName;
         this.diractory = directory;
         directoryFile = new File(directory);
         if(!directoryFile.exists())
         {
             directoryFile.mkdirs();
         }
         soundFile = new File(directoryFile, soundName);    
         Log.i("Unity", "@#$%^&#$@$@#$@");
     }
     
     void prepareSound()
     {
         try
         {
             mediaRecorder = new MediaRecorder();
             mediaRecorder.setAudioSource(AudioSource.MIC);
             mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);
             mediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);
             mediaRecorder.setOutputFile(soundFile.getAbsolutePath());
             mediaRecorder.prepare();
         }
         catch (Exception e)
         {
             e.printStackTrace();
             lastError = e.getMessage();
            
         }
     }
 
     public void startStopRecording()
     {
  
         if (isInRecord)
         {
             if(mediaRecorder != null)
             {
                 mediaRecorder.reset();
             }
         } else
         {
             prepareSound();
             mediaRecorder.start();
         }
         isInRecord = !isInRecord;
     }
     
     public String getLastError()
     {
         return lastError;
     }
     
     boolean getIsInRecord()
     {
         
         return isInRecord;
     }
     
     public String getSoundName()
     {
         return soundName;
     }
     public String getSoundDirectory()
     {
         return diractory;
     }
     
     public String getExternalStoragePath()
     {
         return Environment.getExternalStorageDirectory().getAbsolutePath();
     }   
 }





and the unity script :

using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System;

 public class SoundRecorder : MonoBehaviour
 {
 
     AndroidJavaObject soundRecorder;
 
     void Start()
     {
         Application.RequestUserAuthorization(UserAuthorization.Microphone);
         soundRecorder = new AndroidJavaObject("com.tafavotco.soundrecorder.SoundRecorder");
         
         object[] args =
             {
                 soundRecorder.Call<String>("getExternalStoragePath")+"/aaa",
                 "abc.amr"
             };
         soundRecorder.Call("init", args);
         Debug.Log("Error : " + soundRecorder.Call<String>("getLastError"));
     }
 
     // Update is called once per frame
     void Update()
     {
 
     }
 
     void OnGUI()
     {
         if(GUI.Button(new Rect(0, 0, 200, 90), soundRecorder.Call<bool>("getIsInRecord") ? "stop" : "start"))
         {
             soundRecorder.Call("startStopRecording");
             Debug.Log("Error : " + soundRecorder.Call<String>("getLastError"));
         }
   
 
     }
 
 
 }




but it doesn't work and log in the log cat :

 08-17 10:01:58.134: I/Unity(32673): @#$%^&#$@$@#$@
 08-17 10:01:58.134: I/Unity(32673): Error : 
 08-17 10:01:58.134: I/Unity(32673):  
 08-17 10:01:58.134: I/Unity(32673): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 65)
 08-17 10:01:59.614: I/Unity(32673): AndroidJavaException: java.lang.IllegalStateException
 08-17 10:01:59.614: I/Unity(32673):   at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 
 08-17 10:01:59.614: I/Unity(32673):   at UnityEngine.AndroidJNISafe.CallVoidMethod (IntPtr obj, IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <filename unknown>:0 
 08-17 10:01:59.614: I/Unity(32673):   at UnityEngine.AndroidJavaObject._Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
 08-17 10:01:59.614: I/Unity(32673):   at UnityEngine.AndroidJavaObject.Call (System.String methodName, System.Object[] args) [0x00000] in <filename unknown>:0 
 08-17 10:01:59.614: I/Unity(32673):   at SoundRecorder.OnGUI () [0x00000] in <filename unknown>:0 
 08-17 10:01:59.614: I/Unity(32673):  
 08-17 10:01:59.614: I/Unity(32673): (Filename:  Line: -1)




what's the problem ?

UPDATE :

the "lastError" after click on gui button is : "setAudioSource failed."

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TheDesignZoo · Dec 11, 2015 at 01:55 PM

Im sure you got this sorted. You can get this error if you don't have the correct permission in the Android manfest.xml file for audio recording when you set the audiosource on the MediaRecorder.

Maybe it should be this be updated in the manifest versus the request on the Unity side? I believe this request might be for WebPlayer?

Comment
Add comment · Share
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

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

No OpenGL Context in Unity above 2017.1.2f1 0 Answers

Using AndroidJavaClass in a java Unity3d plugin 0 Answers

Using Java Plugin to add an Android Edittext view 0 Answers

How to talk to Java plugin 0 Answers

How to use Android Api in Unity 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