Author: ASUS
This class is a simple audio player using Java Swing and the sun.audio package to play and stop audio.
The KelasAudio class extends javax.swing.JFrame and provides a GUI for playing
and stopping an audio file.
private AudioStream audioStream; - Holds the audio stream object.private boolean isPlaying = false; - Indicates whether the audio is currently playing.
public KelasAudio() {
initComponents();
}
The constructor initializes the components of the form.
This method is called from within the constructor to initialize the form. It sets up the GUI components.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if (!isPlaying) {
try {
InputStream in = new FileInputStream(new File("C:\\Users\\bagas\\Music\\Taylor Swift - Style (Taylor's Version).wav"));
audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
isPlaying = true;
} catch (IOException e) {
System.out.println(e);
}
}
}
This method is called when the play button (jButton1) is pressed. It starts playing the
audio if it is not already playing.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
if (isPlaying) {
AudioPlayer.player.stop(audioStream);
isPlaying = false;
}
}
This method is called when the pause button (jButton2) is pressed. It stops the audio if it
is currently playing.
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KelasAudio().setVisible(true);
}
});
}
The main method launches the application by creating an instance of KelasAudio and making it
visible.
The following GUI components are declared and initialized in the initComponents method:
jPanel1 - A JPanel that holds the other components.jButton1 - A JButton for playing audio.jButton2 - A JButton for pausing audio.jLabel1 - A JLabel displaying an image.jLabel2 - A JLabel displaying text.
package audio;
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import sun.audio.*;
/**
*
* @author ASUS
*/
public class KelasAudio extends javax.swing.JFrame {
/**
* Creates new form KelasAudio
*/
public KelasAudio() {
initComponents();
}
private AudioStream audioStream;
private boolean isPlaying = false;
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings('unchecked')
//
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBackground(new java.awt.Color(185, 148, 107));
jButton1.setBackground(new java.awt.Color(185, 148, 107));
jButton1.setForeground(new java.awt.Color(185, 148, 107));
jButton1.setIcon(new javax.swing.ImageIcon("C:\\Users\\bagas\\Downloads\\play.png")); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setBackground(new java.awt.Color(185, 148, 107));
jButton2.setForeground(new java.awt.Color(185, 148, 107));
jButton2.setIcon(new javax.swing.ImageIcon("C:\\Users\\bagas\\Downloads\\pause (2).png")); // NOI18N
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jLabel1.setIcon(new javax.swing.ImageIcon("C:\\Users\\bagas\\Downloads\\style taylor swift (2).jpeg")); // NOI18N
jLabel2.setFont(new java.awt.Font("Segoe Script", 0, 18)); // NOI18N
jLabel2.setText("Taylor Swift - Style (Taylor's Version) ");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(91, 91, 91)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(81, 81, 81)
.addComponent(jLabel1)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
.addGap(0, 63, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(30, 30, 30))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap(24, Short.MAX_VALUE)
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addGap(31, 31, 31)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(22, 22, 22)
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(31, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(37, Short.MAX_VALUE))
);
pack();
}//
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (!isPlaying) {
try {
InputStream in = new FileInputStream(new File("C:\\Users\\bagas\\Music\\Taylor Swift - Style (Taylor's Version).wav"));
audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
isPlaying = true;
} catch (IOException e) {
System.out.println(e);
}
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (isPlaying) {
AudioPlayer.player.stop(audioStream);
isPlaying = false;
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(KelasAudio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(KelasAudio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(KelasAudio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(KelasAudio.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new KelasAudio().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}