alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.examples.modules.audioloader;
import java.io.*;
import java.util.ResourceBundle;
import javax.sound.sampled.*;
import javax.swing.JPanel;
import org.openide.*;
import org.openide.util.NbBundle;
public class RecordPanel extends JPanel implements Runnable {
    private static final AudioFormat simpleFormat = new AudioFormat (44100, 16, 2, true, true);
    private MicrophoneTemplateIterator mti;
    private boolean running;
    private TargetDataLine line;
    public RecordPanel (MicrophoneTemplateIterator mti) {
        this.mti = mti;
        running = false;
        initComponents();
        setName (bundle.getString ("RecordPanel.name"));
        recordButton.setEnabled (true);
        stopButton.setEnabled (false);
    }
    private void initComponents () {//GEN-BEGIN:initComponents
        recordButton = new javax.swing.JButton ();
        stopButton = new javax.swing.JButton ();
        setLayout (new java.awt.GridBagLayout ());
        java.awt.GridBagConstraints gridBagConstraints1;
        
        recordButton.setText (bundle.getString ("RecordPanel.recordButton.text"));
        recordButton.addActionListener (new java.awt.event.ActionListener () {
            public void actionPerformed (java.awt.event.ActionEvent evt) {
                recordButtonActionPerformed (evt);
            }
        }
        );
        
        gridBagConstraints1 = new java.awt.GridBagConstraints ();
        gridBagConstraints1.gridx = 0;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.SOUTH;
        add (recordButton, gridBagConstraints1);
        
        
        stopButton.setText (bundle.getString ("RecordPanel.stopButton.text"));
        stopButton.addActionListener (new java.awt.event.ActionListener () {
            public void actionPerformed (java.awt.event.ActionEvent evt) {
                stopButtonActionPerformed (evt);
            }
        }
        );
        
        gridBagConstraints1 = new java.awt.GridBagConstraints ();
        gridBagConstraints1.gridx = 2;
        gridBagConstraints1.gridy = 0;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.SOUTH;
        add (stopButton, gridBagConstraints1);
        
    }//GEN-END:initComponents

    private void stopButtonActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopButtonActionPerformed
        if (! running) return;
        running = false;
        stopButton.setEnabled (false);
    }//GEN-LAST:event_stopButtonActionPerformed

    private void recordButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_recordButtonActionPerformed
        if (running) return;
        try {
            line = (TargetDataLine) AudioSystem.getLine (new DataLine.Info (TargetDataLine.class, simpleFormat));
            line.open (simpleFormat);
            line.start ();
            new Thread (this).start ();
            running = true;
            recordButton.setEnabled (false);
            stopButton.setEnabled (true);
        } catch (Exception e) {
            ErrorManager.getDefault ().notify (e);
        }
    }//GEN-LAST:event_recordButtonActionPerformed

    public void run () {
        ByteArrayOutputStream baos = new ByteArrayOutputStream ();
        byte[] buf = new byte[1024];
        int read;
        try {
            while (running && ((read = line.read (buf, 0, 1024)) > 0)) {
                baos.write (buf, 0, read);
            }
            line.stop ();
        } finally {
            line.close ();
            line = null;
            running = false;
        }
        mti.setRecorded (baos.toByteArray (), simpleFormat);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton recordButton;
    private javax.swing.JButton stopButton;
    // End of variables declaration//GEN-END:variables

    private static final ResourceBundle bundle = NbBundle.getBundle ("org.netbeans.examples.modules.audioloader.Bundle");    

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.