| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 
 | import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
 
import javax.sound.sampled.*;
 
public class Capture extends JFrame {
 
  AudioFormat format;
  AudioInputStream ais;
  DataLine.Info info;
  byte[] buffer;
  ByteArrayOutputStream baos;
  Clip klip;
 
  public Capture() {
    super("Sound Demo");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container content = getContentPane();
 
    final JButton capture = new JButton("Load wave");
    final JButton play = new JButton("Play");
 
    ActionListener captureListener = 
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
         try {
            captureAudio(new URL("http://pscode.org/media/leftright.wav"));
        } catch (UnsupportedAudioFileException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
      }
    };
    capture.addActionListener(captureListener);
    content.add(capture, BorderLayout.NORTH);
 
    ActionListener playListener = 
        new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        playAudio();
      }
    };
    play.addActionListener(playListener);
    content.add(play, BorderLayout.SOUTH);
  }
 
  private void captureAudio(URL fichier) throws UnsupportedAudioFileException, IOException {
    try {
      ais = AudioSystem.getAudioInputStream(fichier);
      format = ais.getFormat();
      ais = AudioSystem.getAudioInputStream(format, ais);
      baos = new ByteArrayOutputStream();
     int bufferSize = 2^32;
     buffer = new byte[bufferSize];
     int iCount = 0;
     int iTotal = 0;
 
     iCount = ais.read(buffer, 0, buffer.length);
     while (iCount>0) {
         baos.write(buffer, 0, iCount);
         iTotal += iCount;
         System.out.println("read " + iCount + " \ttotal " + iTotal );
         iCount = ais.read(buffer, 0, buffer.length);
    }
    ais.close();
     } catch (Exception e) {
      e.printStackTrace();
      System.exit(-2);
    }
  }
 
  private void playAudio(){
	  InputStream byteArrayInputStream = new ByteArrayInputStream(buffer);
	  AudioInputStream ais2 = new AudioInputStream(byteArrayInputStream, 
			  					format, buffer.length/format.getFrameSize());
	  DataLine.Info dataLineInfo = new DataLine.Info( Clip.class, format);
	  Clip klip = null;
	try {
		klip = (Clip)AudioSystem.getLine(dataLineInfo);
		klip.open(ais2);
	} catch (LineUnavailableException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  klip.start();
 
  }
 
  public static void main(String args[]) {    
    JFrame frame = new Capture();
    frame.pack();
    frame.setVisible(true);
  }
} | 
Partager