Bom, fiz usando o netbeans! mas vc tmb pode copiar o código no eclipse!
Para fazer, vc vai percisar criar um projeto Java aplication (playerWav) e de 2 Classes, JPlayerWav(principal) e class Wave, e tmb de 3 imagens jpg(chamadas play, stop e sair). Agora é só copiar os códigos:
/***CLASSE JplayerWave***/
package playerwav;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JOptionPane;
import javax.swing.Timer;
/*@autor marcelognob@yahoo.com*/
public class JPlayerWav extends javax.swing.JFrame {
String[] files = new String[100];
int m = -1, i = 0;
Wave wav = new Wave(null);
String pega, pegimg;
Thread[] tocae = new Thread[99999];
public JPlayerWav() {/** cria form JPlayerWav */
initComponents();
t.start();
}
ActionListener action = new ActionListener() {
public void actionPerformed(@SuppressWarnings("unused") java.awt.event.ActionEvent e) {
if(!wav.isAlive()){
bm2ActionPerformed(null);
};}};
Timer t = new Timer(100, action);
@SuppressWarnings("unchecked")
//
private void initComponents() {
jFrsom = new javax.swing.JFrame();
jFileChoosermp = new javax.swing.JFileChooser();
btstop = new javax.swing.JButton();
btsort = new javax.swing.JButton();
comb = new javax.swing.JComboBox();
bm1 = new javax.swing.JButton();
bm2 = new javax.swing.JButton();
btpasta = new javax.swing.JButton();
txatua = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jFrsom.setSize(500, 400);
setLayout(null);
jFrsom.setTitle("Direrório pra MP3");
jFileChoosermp.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
jFileChoosermp.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jFileChoosermpActionPerformed(evt);
}
});
javax.swing.GroupLayout jFrsomLayout = new javax.swing.GroupLayout(jFrsom.getContentPane());
jFrsom.getContentPane().setLayout(jFrsomLayout);
jFrsomLayout.setHorizontalGroup(
jFrsomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jFrsomLayout.createSequentialGroup()
.addComponent(jFileChoosermp, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jFrsomLayout.setVerticalGroup(
jFrsomLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jFrsomLayout.createSequentialGroup()
.addComponent(jFileChoosermp, javax.swing.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
.addContainerGap(31, Short.MAX_VALUE))
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Player!!! mrtutori.blogspot.com");
getContentPane().setLayout(null);
btstop.setFont(new java.awt.Font("VTC Tribal", 0, 14));
btstop.setIcon(new javax.swing.ImageIcon(getClass().getResource("/playerwav/stop.jpg"))); // NOI18N
btstop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btstopActionPerformed(evt);
}
});
getContentPane().add(btstop);
btstop.setBounds(110, 60, 90, 50);
btsort.setFont(new java.awt.Font("VTC Tribal", 0, 14));
btsort.setIcon(new javax.swing.ImageIcon(getClass().getResource("/playerwav/play.JPG"))); // NOI18N
btsort.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btsortActionPerformed(evt);
}
});
getContentPane().add(btsort);
btsort.setBounds(200, 60, 100, 50);
comb.setBackground(new java.awt.Color(107, 124, 157));
comb.setFont(new java.awt.Font("SansSerif", 1, 14));
comb.setForeground(new java.awt.Color(255, 255, 255));
comb.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
comb.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
combActionPerformed(evt);
}
});
getContentPane().add(comb);
comb.setBounds(10, 0, 360, 50);
bm1.setBackground(new java.awt.Color(0, 0, 0));
bm1.setFont(new java.awt.Font("VTC Tribal", 0, 14));
bm1.setForeground(new java.awt.Color(255, 255, 255));
bm1.setText("<<");
bm1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bm1ActionPerformed(evt);
}
});
getContentPane().add(bm1);
bm1.setBounds(150, 110, 50, 30);
bm2.setBackground(new java.awt.Color(0, 0, 0));
bm2.setFont(new java.awt.Font("VTC Tribal", 0, 14));
bm2.setForeground(new java.awt.Color(255, 255, 255));
bm2.setText(">>");
bm2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bm2ActionPerformed(evt);
}
});
getContentPane().add(bm2);
bm2.setBounds(200, 110, 50, 30);
btpasta.setBackground(new java.awt.Color(0, 0, 0));
btpasta.setFont(new java.awt.Font("VTC Tribal", 1, 14));
btpasta.setForeground(new java.awt.Color(255, 255, 255));
btpasta.setText("Diretotio");
btpasta.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btpastaActionPerformed(evt);
}
});
getContentPane().add(btpasta);
btpasta.setBounds(190, 230, 160, 30);
txatua.setFont(new java.awt.Font("SansSerif", 1, 14));
txatua.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txatuaActionPerformed(evt);
}
});
getContentPane().add(txatua);
txatua.setBounds(160, 260, 200, 30);
jButton1.setBackground(new java.awt.Color(0, 0, 0));
jButton1.setFont(new java.awt.Font("VTC Tribal", 0, 12));
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/playerwav/sair.gif"))); // NOI18N
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
jButton1.setBounds(10, 230, 140, 60);
java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
setBounds((screenSize.width-399)/2, (screenSize.height-339)/2, 399, 339);
}//
private void btstopActionPerformed(java.awt.event.ActionEvent evt) {
tocae[i].suspend();
}
private void btsortActionPerformed(java.awt.event.ActionEvent evt) {
String combss = (String) comb.getSelectedItem();
File arqv = new File(pega + combss);
wav = new Wave(String.valueOf(arqv));
run();
}
private void combActionPerformed(java.awt.event.ActionEvent evt) {
/*@autor MrTutori.blogspot.com*/
}
private void bm1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
comb.setSelectedIndex(comb.getSelectedIndex() - 1);
String combss = (String) comb.getSelectedItem();
File arqv = new File(pega + combss);
wav = new Wave(String.valueOf(arqv));
run();
} catch (Exception eeee) {
JOptionPane.showMessageDialog(null, "OPss");
}
}
private void bm2ActionPerformed(java.awt.event.ActionEvent evt) {
try {
comb.setSelectedIndex(comb.getSelectedIndex() + 1);
String combss = (String) comb.getSelectedItem();
File arqv = new File(pega + combss);
wav = new Wave(String.valueOf(arqv));
run();
} catch (Exception eeee) {
}
}
private void btpastaActionPerformed(java.awt.event.ActionEvent evt) {
jFrsom.show();
}
private void txatuaActionPerformed(java.awt.event.ActionEvent evt) {
}
/*@autor marcelognob@yahoo.com*/
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}
private void jFileChoosermpActionPerformed(java.awt.event.ActionEvent evt) {
txatua.setText(""+jFileChoosermp.getSelectedFile()+"/");
pega = txatua.getText();
atuaAction();
jFrsom.hide();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JPlayerWav().setVisible(true);
}});}
public void run() {
try {
tocae[i].stop();
i++;
tocae[i] = (wav);
tocae[i].start();
} catch (Exception e) {
e.printStackTrace(System.err);
}}
public void atuaAction(){
i = 0;
tocae[0] = (wav);
files = new String[100];
pega = txatua.getText();
File search = new File(pega + "/");
if (search.exists()) {
files = search.list();
comb.removeAllItems();//Romover Itens
int w = -1;
for (String bb : files) {//Add itens .wav
w = bb.lastIndexOf(".wav");
System.out.println("encontrou: " + bb);
if (w > -1) {
comb.addItem(bb);
}}}
run();}
// Variables declaration - do not modify
private javax.swing.JButton bm1;
private javax.swing.JButton bm2;
private javax.swing.JButton btpasta;
private javax.swing.JButton btsort;
private javax.swing.JButton btstop;
private javax.swing.JComboBox comb;
private javax.swing.JButton jButton1;
private javax.swing.JFileChooser jFileChoosermp;
private javax.swing.JFrame jFrsom;
private javax.swing.JTextField txatua;
// End of variables declaration
/*@autor mrtutori.blogspot.com*/
}
/****CLASSE WAVE****/
package playerwav;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
/*@autor marcelognob@yahoo.com*/
public class Wave extends Thread {
private String filename;
private Position curPosition;
private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb
enum Position {
LEFT, RIGHT, NORMAL};
public Wave (String wavfile) {
filename = wavfile;
curPosition = Position.NORMAL;
}
public Wave (String wavfile, Position p) {
filename = wavfile;
curPosition = p;
}
public void run() {
File soundFile = new File(filename);
if (!soundFile.exists()) {
System.err.println("Erro no Arqv: " + filename);
return;
}/*@autor marcelognob@yahoo.com*/
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline
.getControl(FloatControl.Type.PAN);
if (curPosition == Position.RIGHT)
pan.setValue(1.0f);
else if (curPosition == Position.LEFT)
pan.setValue(-1.0f);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
/*@autor marcelognob@yahoo.com*/
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}}}
0 comentários:
Postar um comentário