• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why is my Android Java Object null when invoking a method? 0 Answers

How can I set/get/confirm the API used for an embedded Android script? 0 Answers

Problem reading sensor plugin, event won't trigger 2 Answers

Now that Unity 4.1.2 broke the Android plugin examples, what do you use to learn them? 1 Answer

UnityPlayerActivity vs. UnityPlayerNativeActivity 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges