Registrazione audio in chiamata

  • Creatore Discussione Creatore Discussione Provy
  • Data di inizio Data di inizio

Provy

Nuovo Utente
6 Mar 2010
13
0
0
Salve a tutti, devo fare un applet che registri l'audio in una chiamata voip... Fino al catturare l'audio dal microfono ci sono, nessun problema.. Ma come si fa con l'audio dalle casse/cuffie?? Sono 3 giorni che mi danno!!:incazz: Qualcuno può darmi una mano??
Grazie in anticipo!!
 
ciao, che sistema usi ? ubuntu e semplice devi mettere l'ingresso ad Line-in, se non mi ricordo male,

e questo è il mio codice che mi hanno aiutato ad fare su ubuntu-it.

capture.java
Codice:
public class Capture{
  public static void main(String args[]) {
    CaptureFrame frame = new CaptureFrame();
    frame.pack();
    frame.setSize(400,400);
    frame.setVisible(true);
  }
}

CaptureFrame.java
Codice:
import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.sound.sampled.*;
import javax.sound.sampled.spi.AudioFileWriter;
import javax.swing.*;

import com.sun.media.sound.WaveFileWriter;

public class CaptureFrame extends JFrame {

    private static  final long serialVersionUID = 1L;
    protected boolean running;
    ByteArrayOutputStream out;

    public CaptureFrame() {
        super("Capture Sound Demo");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container content = getContentPane();
    
        final JButton capture = new JButton("Capture");
        final JButton stop = new JButton("Stop");
        final JButton play = new JButton("Play");
        final JButton save = new JButton("Save");
    
        capture.setEnabled(true);
        stop.setEnabled(false);
        play.setEnabled(false);
        save.setEnabled(false);
    
        capture.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                capture.setEnabled(false);
                stop.setEnabled(true);
                play.setEnabled(false);
                save.setEnabled(false);
                captureAudio();
            }
        });
        content.add(capture, BorderLayout.NORTH);
    
        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                capture.setEnabled(true);
                stop.setEnabled(false);
                play.setEnabled(true);
                save.setEnabled(true);
                running = false;
            }
        });
        content.add(stop, BorderLayout.CENTER);
    
        play.addActionListener( new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                playAudio();
            }
        });
    
        save.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                saveAudio();
            }
        });
        content.add(save, BorderLayout.EAST);
        content.add(play, BorderLayout.SOUTH);
    }

    private void captureAudio() {
        try {
            final AudioFormat format = getFormat();
            DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
            final TargetDataLine line = (TargetDataLine)AudioSystem.getLine(info);
            line.open(format);
            line.start();
            Runnable runner = new Runnable() {
                int bufferSize = (int)format.getSampleRate() * format.getFrameSize();
                byte buffer[] = new byte[bufferSize];
            
                public void run() {
                    out = new ByteArrayOutputStream();
                    running = true;
                    try {
                        while (running) {
                            int count = line.read(buffer, 0, buffer.length);
                            if (count > 0) {
                                out.write(buffer, 0, count);
                            }
                        }
                        out.close();
                    } catch (IOException e) {
                        System.err.println("I/O problems: " + e);
                        System.exit(-1);
                    }
                }
            };
            Thread captureThread = new Thread(runner);
            captureThread.start();
        } catch (LineUnavailableException e) {
            System.err.println("Line unavailable: " + e);
            System.exit(-2);
        }
    }

    private void playAudio() {
        try {
            byte audio[] = out.toByteArray();
            InputStream input = new ByteArrayInputStream(audio);
            final AudioFormat format = getFormat();
            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();
        
            Runnable runner = new Runnable() {
                int bufferSize = (int) format.getSampleRate() 
                * format.getFrameSize();
                byte buffer[] = new byte[bufferSize];
            
                public void run() {
                    try {
                        int count;
                        while ((count = ais.read(
                            buffer, 0, buffer.length)) != -1) {
                            if (count > 0) {
                                line.write(buffer, 0, count);
                            }
                        }
                        line.drain();
                        line.close();
                    } catch (IOException e) {
                        System.err.println("I/O problems: " + e);
                        System.exit(-3);
                    }
                }
            };
            Thread playThread = new Thread(runner);
            playThread.start();
        } catch (LineUnavailableException e) {
            System.err.println("Line unavailable: " + e);
            System.exit(-4);
        } 
    }

    private void saveAudio() {
        File file = new File("./test.wav");
        try {
            byte audio[] = out.toByteArray();
            InputStream input = new ByteArrayInputStream(audio);
            final AudioFormat format = getFormat();
            final AudioInputStream ais = new AudioInputStream(input, format, audio.length / format.getFrameSize());
            final AudioFileWriter afw = new WaveFileWriter();
            afw.write(ais, AudioFileFormat.Type.WAVE, file );
            ais.close();
        } catch (IOException e) {
            System.err.println("Impossibile salvare il file '"+file+"': " + e);
            System.exit(-4);
        }
    }

    private AudioFormat getFormat() {
        float sampleRate = 8000;
        int sampleSizeInBits = 8;
        int channels = 1;
        boolean signed = true;
        boolean bigEndian = true;
        return new AudioFormat(sampleRate,sampleSizeInBits, channels, signed, bigEndian);
    }
}

vedi devi fare cosi:
javac *.java
java Capture.

quando fai Salva di fa un file Test.wav nella solita cartella.

vedi se ti è utile,
un po di codice lo trovato nei siti inglesi.

saluti.
luigi.
 

Discussioni simili