Program Pemutar Suara pada JavaNetbeans dengan Java Media Framework
package mp3_unite;
import java.io.File;
import java.net.URL;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author masud
*/
public class Mp3 extends javax.swing.JFrame {
/**
* Creates new form Mp3
*/
DefaultListModel model = new DefaultListModel();
Player mp3;
public Mp3() {
initComponents();
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList();
hapus = new javax.swing.JButton();
play = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Tambah List Mp3");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jScrollPane1.setViewportView(list);
hapus.setText("Hapus");
hapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hapusActionPerformed(evt);
}
});
play.setText("Play");
play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(143, 143, 143)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(play, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(hapus)))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(play)
.addComponent(hapus))
.addGap(23, 23, 23))
);
pack();
}// </editor-fold>
private void playActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if (evt.getActionCommand().equalsIgnoreCase("Play")) {
String main = list.getSelectedValue().toString();
try {
mp3 = Manager.createPlayer(new URL("file", null, main));
mp3.start();
} catch (Exception e) {
}
play.setText("Stop");
}
if (evt.getActionCommand().equalsIgnoreCase("Stop")) {
mp3.stop();
play.setText("Play");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "masukkan Mp3");
}
}
private void hapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
int posisi = list.getSelectedIndex();
model.remove(posisi);
}catch(Exception e){
System.out.println("tidak ada Mp3 yang dihapus");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser a = new JFileChooser();
JFrame b = new JFrame();
FileNameExtensionFilter Filemp3 = new FileNameExtensionFilter(".mp4,.wav","mp3","wav");
a.addChoosableFileFilter(Filemp3);
int setuju = a.showOpenDialog(b.add(a));
int posisi = list.getModel().getSize();
if (setuju == JFileChooser.APPROVE_OPTION) {
File file = a.getSelectedFile();
String text = file.getPath().toString();
String[] txt = new String[]{text};
model.add(posisi, text);
}
list.setModel(model);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* 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(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mp3().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton hapus;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList list;
private javax.swing.JButton play;
// End of variables declaration
}
import java.io.File;
import java.net.URL;
import javax.media.Manager;
import javax.media.Player;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
*
* @author masud
*/
public class Mp3 extends javax.swing.JFrame {
/**
* Creates new form Mp3
*/
DefaultListModel model = new DefaultListModel();
Player mp3;
public Mp3() {
initComponents();
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
list = new javax.swing.JList();
hapus = new javax.swing.JButton();
play = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Tambah List Mp3");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jScrollPane1.setViewportView(list);
hapus.setText("Hapus");
hapus.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hapusActionPerformed(evt);
}
});
play.setText("Play");
play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(44, 44, 44)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(143, 143, 143)
.addComponent(jButton1))
.addGroup(layout.createSequentialGroup()
.addGap(108, 108, 108)
.addComponent(play, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(30, 30, 30)
.addComponent(hapus)))
.addContainerGap(51, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 318, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(play)
.addComponent(hapus))
.addGap(23, 23, 23))
);
pack();
}// </editor-fold>
private void playActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
if (evt.getActionCommand().equalsIgnoreCase("Play")) {
String main = list.getSelectedValue().toString();
try {
mp3 = Manager.createPlayer(new URL("file", null, main));
mp3.start();
} catch (Exception e) {
}
play.setText("Stop");
}
if (evt.getActionCommand().equalsIgnoreCase("Stop")) {
mp3.stop();
play.setText("Play");
}
}catch(Exception e){
JOptionPane.showMessageDialog(null, "masukkan Mp3");
}
}
private void hapusActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
int posisi = list.getSelectedIndex();
model.remove(posisi);
}catch(Exception e){
System.out.println("tidak ada Mp3 yang dihapus");
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser a = new JFileChooser();
JFrame b = new JFrame();
FileNameExtensionFilter Filemp3 = new FileNameExtensionFilter(".mp4,.wav","mp3","wav");
a.addChoosableFileFilter(Filemp3);
int setuju = a.showOpenDialog(b.add(a));
int posisi = list.getModel().getSize();
if (setuju == JFileChooser.APPROVE_OPTION) {
File file = a.getSelectedFile();
String text = file.getPath().toString();
String[] txt = new String[]{text};
model.add(posisi, text);
}
list.setModel(model);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/*
* Set the Nimbus look and feel
*/
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/*
* 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(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Mp3.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/*
* Create and display the form
*/
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Mp3().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton hapus;
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JList list;
private javax.swing.JButton play;
// End of variables declaration
}
package mp3_unite;
/**
*
* @author masud
*/
public class Mp3_unite {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
}
}

Komentar
Posting Komentar