4

I have a small program that reads from dataline mic to byte array I am not 100% sure it works but just by printing part of the array it seem to chang when am making a sound next to the mic:)

I like to play the sound back how do i play the data from the buffer(byte array to sound)??

 package mic; 
import javax.sound.sampled.*;

public class Mic extends Thread{
    boolean flag;
    TargetDataLine mic;
    byte[] buffer;
    AudioFormat format;

    public static void main(String[] args) {
       Mic a=new Mic();
       a.start();
    }
    @Override
    public void run()
    {
        flag=true;
        startMic();
        while(flag)
        {
            send();
        }
    }
    public void send()
    {
        try{
            mic.read(buffer,0,512);
            System.out.println("1. "+buffer[0]);
        }catch(Exception ex)
        {

        }
    }
    public void startMic()
    {
        try{
            format=new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,8000.0F,16,2,4,8000.0F,true);
            DataLine.Info info=new DataLine.Info(TargetDataLine.class,format);
            mic=(TargetDataLine)AudioSystem.getLine(info);
            mic.open();
            mic.start();
            buffer=new byte[512];
        }catch(Exception exx)
        {
            System.out.println("exx");
        }

    }
}

EDIT:
what i wanted to do in the end is send the byte array to other app and play right away
like live stream of a radio

user1246950
  • 1,022
  • 2
  • 10
  • 19
  • Create/select a `SourceDataLine` with the same `AudioFormat` and write your bytes in there. Or wrap your data in an `AudioInputStream` and use a `WaveFileWriter` to store it as a .WAV file to disk. – mihi Sep 25 '12 at 19:13
  • There are lots of examples here: http://www.jsresources.org/ – Bjorn Roche Sep 26 '12 at 01:36

2 Answers2

8

Based on this:

// Create the AudioData object from the byte array
AudioData audiodata = new AudioData(byteArray);
// Create an AudioDataStream to play back
AudioDataStream audioStream = new AudioDataStream(audioData);
// Play the sound
AudioPlayer.player.start(audioStream);
kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
  • 2
    What i need to import for using AudioPlayer – Sachin Setiya Nov 10 '15 at 22:40
  • @Sachin-Setiya: `import java.io.InputStream` – CFSO6459 Jan 06 '18 at 04:14
  • 5
    `AudioData` and `AudioDataStream` require `sun.*` packages, which is **WAY OUT OF DATE** (~20 years ago) and they have lots of bugs. Please try to avoid using it [[Reference](https://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java#comment4884807_26311)]. (And [this](https://docs.oracle.com/javase/tutorial/sound/playing.html) might be a better way.) – CFSO6459 Jan 06 '18 at 04:25
2

A easy way to test the captured data is to save them into a file and load then in audacity using the RAW file import. Just specify your raw file format as : Sampling rate 8KHz format PCM big endian stereo 16 bits

Julien Vermillard
  • 2,945
  • 1
  • 18
  • 18
  • 1
    theres got to be a way to play it right from the byte array or convert it to some thing els(stay in mem) then play it ,id like to avoid the hd – user1246950 Sep 25 '12 at 21:35
  • yes but you will need to debug both your record and your playback code, this way is much easier to test – Julien Vermillard Sep 27 '12 at 05:24