1

Доброго времени суток! Как считывать аудио с микрофона в буфер?

0

1 ответ 1

3

Да когда уже люди научатся пользоваться Гуглом?! На первой странице по запросу java запись с микрофона ссылка на статью с конкретным примером кода.

import javax.sound.sampled.*;
import java.io.*;
import javax.sound.sampled.AudioFileFormat.Type;

public class JRecorder extends Thread    {

    private TargetDataLine        m_line;
    private AudioFileFormat.Type    m_targetType;
    private AudioInputStream    m_audioInputStream;
    private File            m_outputFile;

    public JRecorder(TargetDataLine m_line, Type m_targetType,  File m_outputFile) {
        this.m_line = m_line;
        this.m_targetType = m_targetType;
        this.m_audioInputStream = new AudioInputStream(m_line);
        this.m_outputFile = m_outputFile;
    }

    public void start()
    {
        m_line.start();

        super.start();
    }

    public void stopRecording()
    {
        m_line.stop();
        m_line.close();
    }


    public void run()
    {
            try
            {
                AudioSystem.write(
                    m_audioInputStream,
                    m_targetType,
                    m_outputFile);
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    }



    public static void main(String args[])
    {
       File outputFile = new File("c:\\audiorec.wav");

        AudioFormat    audioFormat = new AudioFormat(
            AudioFormat.Encoding.PCM_SIGNED,
            44100.0F, 16, 2, 4, 44100.0F, false);

        DataLine.Info    info = new DataLine.Info(TargetDataLine.class, audioFormat);
        TargetDataLine    targetDataLine = null;
        try
        {
            targetDataLine = (TargetDataLine) AudioSystem.getLine(info);
            targetDataLine.open(audioFormat);
        }
        catch (LineUnavailableException e)
        {
            System.out.println("unable to get a recording line");
            e.printStackTrace();
            System.exit(1);
        }

        AudioFileFormat.Type    targetType = AudioFileFormat.Type.WAVE;

        JRecorder j = new JRecorder(targetDataLine,targetType,outputFile);



        System.out.println("Press ENTER to start the recording.");

        try
        {
            System.in.read();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        /* Here, the recording is actually started.
         */
        j.start();
        System.out.println("Recording...");


        System.out.println("Press ENTER to stop the recording.");
        try
        {
            System.in.read();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        j.stopRecording();
        System.out.println("Recording stopped.");
    }

}
6
  • есть у меня этот пример - я не могу понять, как считывать в буфер, а не в файл
    – woland
    17 июн 2013 в 10:11
  • 1
    Тогда почему бы вам не почитать [документацию][1], в которой чёрным по белому описан метод, пишущий не в файл, а в OutputStream? [1]: docs.oracle.com/javase/6/docs/api/javax/sound/sampled/…
    – fori1ton
    17 июн 2013 в 10:15
  • такая проблема - AudioSystem.write() принимает OutputStream, а не нужно получить объект Buffer. что можно придумать?
    – woland
    17 июн 2013 в 11:11
  • Что мешает методу AudioSystem.write() передать ByteArrayOutputStream, а затем методом ByteArrayOutputStream.toByteArray() вытащить из него содержимое и записать в Buffer?
    – fori1ton
    17 июн 2013 в 11:23
  • пока главная проблема в том, что Buffer абстрактный класс и не имеет реализации
    – woland
    17 июн 2013 в 11:34

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.