Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente bannato
    Registrato dal
    Dec 2009
    Messaggi
    43

    problema con audio (capture)

    Salve, ho questo codice, che quando faccio capture per la prima volta tutto ok,
    ma se lo faccio la seconda volta mi esce dal programma e il debug mi da questo:

    --------------------Configuration: <Default>--------------------
    Line unavailable: javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 8000.0 Hz, 8 bit, mono, 1 bytes/frame, not supported.

    Process completed.
    ------------------------------------------------------------------------------------------

    USO Jcreator Pro con licenza. e java 6

    Mi dite come dovrei risolvere?

    codice:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.io.*;
    import javax.sound.sampled.*;
    
    public class Capture extends JFrame {
    
      protected boolean running;
      ByteArrayOutputStream out;
    
      public Capture() {
        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");
    
        capture.setEnabled(true);
        stop.setEnabled(false);
        play.setEnabled(false);
    
        ActionListener captureListener = 
            new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            capture.setEnabled(false);
            stop.setEnabled(true);
            play.setEnabled(false);
            captureAudio();
          }
        };
        capture.addActionListener(captureListener);
        content.add(capture, BorderLayout.NORTH);
    
        ActionListener stopListener = 
            new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            capture.setEnabled(true);
            stop.setEnabled(false);
            play.setEnabled(true);
            running = false;
          }
        };
        stop.addActionListener(stopListener);
        content.add(stop, BorderLayout.CENTER);
    
        ActionListener playListener = 
            new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            playAudio();
          }
        };
        play.addActionListener(playListener);
        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 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);
      }
    
      public static void main(String args[]) {
        JFrame frame = new Capture();
        frame.pack();
        frame.show();
      }
    }
    Mi dite come posso risolvere il problema che per me è difficile ad risolverlo.

    grazie.

  2. #2
    Utente di HTML.it
    Registrato dal
    Nov 2009
    Messaggi
    755
    non so risolvere il tuo problema...ma nel metodo main quando fai:
    codice:
    frame.show();
    converrebbe fare piuttosto:
    codice:
    frame.setVisible(true);
    come potrai vedere anche dalla documentazione on-line della sun il metodo "show()" è stato "sostituito/deprecated" dal metodo "setVisible(boolean b);"
    ciao

    Ps. quello che ho detto vale per versioni java successive alla 1.5...

  3. #3
    Utente bannato
    Registrato dal
    Dec 2009
    Messaggi
    43
    altro?,


    OT

    Sai che mi è sucesso....
    Ero ad provare ad cambiare le impostazioni del microfono in qualità professionale,
    e dopo windows vista tutte le volte che ricercavo di metterlo a posto in qualità normale, si inchiodava il sistema
    ed quindi ho dovuto fare un ripristino perchè non c'èra niente da fare.

    FINE OT.

    Sapete che problema è ?

    grazie

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.