0

im using

public static void playAudioFromByteArray(byte[] audio,
        AudioFormat format) throws Exception {
    if(format==null)
        format=new AudioFormat(44000f, 8, 1, true, true);
    final AudioFormat _format = format;
    InputStream input = new ByteArrayInputStream(audio);
    final AudioInputStream ais = new AudioInputStream(input, format,
            audio.length / format.getFrameSize());
    DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
    final SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
    line.open(format);
    line.start();
    if(playThread!=null){

        //playThread.stop();
    }

    runner = new Runnable() {
        int bufferSize = (int) _format.getSampleRate()
                * _format.getFrameSize();
        byte buffer[] = new byte[bufferSize];
        private boolean _playing;
        public void run() {
            try {
                int count;
                _playing = true;
                while ((count = ais.read(buffer, 0, buffer.length)) != -1) {
                    //progress++;
                    if (count > 0) {
                        line.write(buffer, 0, count);
                    }
                    if (! _playing)
                        break;
                }
                _playing = false;
                line.drain();
                line.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    playThread = new Thread(runner);
    playThread.start();
}

to play sounds recorded from the mic

but on some pcs i get

EDIT: i tryied with 32000 also

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44000.0 Hz, 8 bit, mono, 1 bytes/frame,  not supported.
    at com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(Unknown Source)
    at com.sun.media.sound.AbstractDataLine.open(Unknown Source)
    at com.sun.media.sound.AbstractDataLine.open(Unknown Source)

Is there some way to use a poor quality audioformat and be sure that it will work on any machine? or to detect what audioformats are avaliable?

EDIT2:

with Greg code i get

Mixer: Driver audio principale
  interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
  interface Clip supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Speakers (Realtek High Definition Audio)
  interface SourceDataLine supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
  interface Clip supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Driver primario di acquisizione suoni
  interface TargetDataLine supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Microphone (Realtek High Defini
  interface TargetDataLine supporting 8 audio formats, and buffers of at least 32 bytes
    PCM_UNSIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, mono, 1 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, mono, 2 bytes/frame, big-endian
    PCM_UNSIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 8 bit, stereo, 2 bytes/frame, 
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, little-endian
    PCM_SIGNED unknown sample rate, 16 bit, stereo, 4 bytes/frame, big-endian
Mixer: Port Speakers (Realtek High Definiti
  SPEAKER target port
Mixer: Port Microphone (Realtek High Defini
  MICROPHONE source port
  Volume master target port

and 44100f hz is working

Yggdrasil
  • 43
  • 9
  • CD quality is 44.1kHz so 44100f is more likely to work. See my answer [here](http://stackoverflow.com/a/39510109/2670892) for how to list available formats. – greg-449 May 07 '17 at 07:17
  • thanks, that helped a lot, i upvoted your linked answer. – Yggdrasil May 07 '17 at 07:47

0 Answers0