0

I am trying to record audio in firemonkey by calling android api TJAudioRecord directly but it doesn't work, audioRecord.getState returns STATE_UNINITIALIZED whatever I tried, please help?

There is some error in this function

  audioRecord := 

    TJAudioRecord.JavaClass.init(TJMediaRecorder_AudioSource.JavaClass.VOICE_COMMUNICATION,
                                                 16000,
                                                 TJAudioFormat.JavaClass.CHANNEL_IN_MONO,
                                                  TJAudioFormat.JavaClass.ENCODING_PCM_16BIT,
                                                  bufferSize);

full code

 bufferSize := TJAudioRecord.JavaClass.getMinBufferSize(16000,
                                                            TJAudioFormat.JavaClass.CHANNEL_IN_MONO,
                                                           TJAudioFormat.JavaClass.ENCODING_PCM_16BIT);

     if (bufferSize <> TJAudioRecord.JavaClass.ERROR_BAD_VALUE) then
     begin
      audioRecord := TJAudioRecord.JavaClass.init(TJMediaRecorder_AudioSource.JavaClass.VOICE_COMMUNICATION,
                                                 16000,
                                                 TJAudioFormat.JavaClass.CHANNEL_IN_MONO,
                                                  TJAudioFormat.JavaClass.ENCODING_PCM_16BIT,
                                                  bufferSize);
        ShowMessage(IntToStr(buffersize));
       if (audioRecord.getState = TJAudioRecord.JavaClass.STATE_INITIALIZED)then begin
      //then begin
      audioRecord.startRecording;
      ShowMessage('ok');
      end;
    end;
     except
    ShowMessage('Error 0');
    end;
2
  • You are checking the result of getMinBufferSize() for ERROR_BAD_VALUE (-2), but you are not checking it for ERROR (-1) as well, which would indicate a hardware problem. I would suggest you check the result for >0 instead of specific errors. Commented Apr 15, 2014 at 19:18
  • buffersize is ok, it returns 4096, it won't initialize, also I have turned on audiorecord in persmission list Commented Apr 16, 2014 at 8:23

1 Answer 1

1

I use the following code and it works on my Samsung Galaxy Note III.

  Ses := TJAudioRecord.JavaClass.init(TJMediaRecorder_AudioSource.JavaClass.MIC, 11025, TJAudioFormat.JavaClass.CHANNEL_IN_MONO, TJAudioFormat.JavaClass.ENCODING_PCM_16BIT, 44100 * 2);
  (Ses As JAudioRecord).startRecording;
  Okunan := 0;
  M := TMemoryStream.Create;
  for I := 1 to 2 do
  Begin
    Tampon := TJavaArray<SmallInt>.Create(44100);
    Okunan := Okunan + (Ses As JAudioRecord).read(Tampon, 0, 44100);
    M.Write(Tampon.Data^, Tampon.Length * 2);
    Tampon.Free;
  End;
  (Ses As JAudioRecord).stop;
  Ses.release;
  M.SaveToFile(TPath.GetRingtonesPath+'/ses.raw');
Sign up to request clarification or add additional context in comments.

7 Comments

thanks, I have modified my code with your example and it is working
I'm glad that you have solved it. Would you accept the answer then?
sure :) I need to figure out to put this code in the thread and that is it, standard deplhi TThread throws exception
I tried it with a thread object and I also get the exception.
Delphi lets you write code that runs on multiple platforms (iOS, Android, OSX, Win32, and Win64). The cross-platform support adds overhead. If you don't need cross-platform, Delphi isn't the right choice. If you do, it beats Android Studio hands down for everything not Android when you try to use the same source. :-)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.