0

My goal
I want to do the following in Java: record some sound made by Java itself, and then save it as a wav file.

I've searched for a lot of these kind of programs online, and I've found some good ones, but I get the same problem again and again (the current version can be found here). The problem is always something like this:
In the public class, the function start begins with

AudioFormat format = getAudioFormat();
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
if (!AudioSystem.isLineSupported(info)) {
    System.out.println("Line not supported");
    System.exit(0);
}

, where getAudioFormat() returns AudioFormat(16000,8,2,true,true). If I run the file, it prints Line not supported, but why?

What I've tried:

  • I've searched for loads of different formats online, and tried each of them.
  • I've taken an existing wav file which Java can play, taken the format of that one, and used it as the format for the file I try to create.

Some more information:

System.out.println(AudioSystem.getTargetLineInfo(info));
System.out.println(AudioSystem.getAudioFileTypes());
System.out.println(AudioSystem.getMixerInfo());
System.out.println(AudioSystem.getSourceDataLine(format));

where format is the format of the existing wav file, and info is defined as in the first code snippet, prints:

[Ljavax.sound.sampled.Line$Info;@4eec7777
[Ljavax.sound.sampled.AudioFileFormat$Type;@41629346
[Ljavax.sound.sampled.Mixer$Info;@6d311334
com.sun.media.sound.DirectAudioDevice$DirectSDL@448139f0

I use Windows 7, 64 bits.

I'd like to add that if there's a better, entirely different way to achieve my goal, that's fine too.

Pim
  • 123
  • 1
  • 5
  • The audio format that you are using is not supported by that `DataLine` not by Java. In this case you're trying to record from the default audio input (usually the microphone) and your sound card doesn't support the audio format that you're using. You need to check what audio formats your system supports and use one of those. – Titus May 17 '15 at 13:22
  • This is a bit of a guess, but try changing the number of channels on the AudioFormat from 2 to 1. Using 2 channels might be requesting a stereo input. – Radiodef May 17 '15 at 13:26
  • @Titus: how can I check that? – Pim May 17 '15 at 13:54
  • @Radiodef: thanks for the advice, but it hasn't worked – Pim May 17 '15 at 13:54
  • You can get the supported audio formats like this `info.getFormats()` [more info](http://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/DataLine.Info.html#getFormats()) – Titus May 17 '15 at 14:22
  • @Titus: this doesn't work for me: as info is already defined as a function of format (see line 2 of the first snippet), format = info.getFormats()[0] doesn't change the value of format. – Pim May 17 '15 at 14:57
  • Yes you are right, you have to get the `DataLine.Info` some other way, you're currently creating it using an `AudioFormat` object. – Titus May 17 '15 at 15:08
  • @Titus: and which way would that be? – Pim May 17 '15 at 15:13
  • The second answer to this [question](http://stackoverflow.com/questions/6002444/java-recording-from-mixer) shows how you can do that. – Titus May 17 '15 at 15:15
  • @Titus: thanks, but it still doesn't work. I get the same output (8 supported formats), but none of them work. The question you linked to seems very similar to mine, but also isn't solved. I've tried the code of the first answer, but I don't know where he defines SystemAudioProfile (and I can't comment) – Pim May 17 '15 at 15:36
  • Last time I did this, I've used `new AudioFormat(8000.0f, 16, 1, true, true);` which is supported by most audio cards but the quality is not that good. Also I was referring to the second answer (by Daniel1000) which list all the lines and the supported audio formats. If you want to use the first approach you can remove the `SystemAudioProfile profile` variable, is not used anywhere in the code. – Titus May 17 '15 at 15:56
  • @Titus: Thanks, I've managed to find the excact problem: using the first approach, I find that there are no supported formats for the TargetDataLine. How do I solve this? Is there something I can download, or anything else I can do? And if you don't know the answer, is it okay for me to post a new question about the fact that there are no supported formats? – Pim May 17 '15 at 17:21
  • In your example, you are trying to record from the default audio in line, maybe there is nothing plugged into your sound card's audio in and that is why `isLineSupported(...)` returns `false` regardless of the format. For short, maybe there is no `TargetDataLine` available on your system. – Titus May 17 '15 at 17:42

0 Answers0