简体   繁体   中英

How do I record my voice and make it a file in java?

I'm trying this code, but after test 5 and test 9, the code seems to stop functioning at audioStream.write() .

Nothing after

AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);

is executed by the program. How do I fix this or is there another way to record my voice?

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

public class Mic{



    public static void main(String[] args){
        System.out.println("Sound test starting");

        try {
            AudioFormat format = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 44100, 16, 2, 4, 44100, false);

            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            if(!AudioSystem.isLineSupported(info))
            {
                System.err.println("Line is not supported");
            }

            final TargetDataLine targetLine = (TargetDataLine)AudioSystem.getLine(info); 
            targetLine.open();

            System.out.println("Recording voice");
            targetLine.start();

            Thread thread = new Thread();

            {
                //@Override public void run();

                {

                    AudioInputStream audioStream = new AudioInputStream(targetLine);
                    File audioFile = new File("Recording.wav");
                    System.out.println("test5");
                    try { System.out.println("test9");
                        AudioSystem.write(audioStream, AudioFileFormat.Type.WAVE, audioFile);
                        System.out.println("audio writing test");
                    }

                    catch(IOException e){
                        e.printStackTrace();
                    }
                    System.out.println("test6");
                    System.out.println("Recording Stopped");

                }
            };
            thread.start();
            Thread.sleep(5000);
            targetLine.stop();
            targetLine.close();

            System.out.println("Ended sound test");
        }
        catch(InterruptedException ie){
            ie.printStackTrace();
        }
        catch(LineUnavailableException e){
            e.printStackTrace();
        }

    }



}

There are two possible things that could be happening:

  • The write call could be blocking for ever.

  • The write call could be throwing some unchecked exception, that is going unreported.

Temporarily change this:

    catch (IOException e) {
           e.printStackTrace();
    }

to this:

    catch (Throwable e) {
           e.printStackTrace();
    }

and show us the stacktrace (if any) that now gets output.


Here's another couple of possibility:

The write operation you are calling is actually a copy from audioStream to the file:

  • If the source ( audioStream ) is not properly set up, the write could actually be blocked in reading data from your input device.

  • Have you checked the output file? Has anything been written to it? It could be that the recording process is working ...

Either way, the next thing I would do would be to run this under a debugger, and try to figure out what the write call is doing while it is (apparently) blocked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM