Pergunta

So, I went over the Java's sound tutorial and I did not find it all so helpful. Anyways, what I understood from the tutorial for recording sound from a mic is this:
sound

Although they do show how to get a target data line and so on, they do not tell how you can actually record sound [or maybe I didn't get it all well].
My understanding so far has been this:

  • Mixer can be your sound card or sound software drivers that can be used to process the sound, whether input or output
  • TargetDataLine is used when you want to output your sound into the computer. Like save it to the disk
  • Port is where your external devices like mic, etc are connected
  • Problems that remain

  • How do I select the proper mixer? Java's tut says that you get all the available mixers and query each one to see if it has what you want. That's quite vague for a beginner
  • How do I get the port on which my integrated mic is? Specifically, how do I get input from it into the mixer?
  • How do I output this to the disk?
  • Foi útil?

    Solução

    Using the AudioSystem.getTargetDataLine(AudioFormat format) method you will get

    ... a target data line that can be used for recording audio data in the format specified by the AudioFormat object. The returned line will be provided by the default system mixer, or, if not possible, by any other mixer installed in the system that supports a matching TargetDataLine object.

    See the accepted answer for Java Sound API - capturing microphone for an example of this.
    If you want more control of which data line to use you can enumerate all the mixers and the data lines they support and pick the one you want. Here is some more information regarding how you would go about doing that: Java - recording from mixer

    Once you've obtained the TargetDataLine you should open() it, and then call read() repeatedly to obtain data from that data line. The byte[] that you fill up with data with each call to read() can be written to disk e.g. through a FileOutputStream.

    Licenciado em: CC-BY-SA com atribuição
    Não afiliado a StackOverflow
    scroll top