hi,
ich muss in java einen musicplayer programmieren.
könnte mir vl. jmd. helfen indem er mir sagt wie man von nem string ne mp3-datei abspielt?? habe meinen bisherigen code angehängt.
danke!!
Java
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
public class Frame extends javax.swing.JFrame {
// neuer Frame
public Frame() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jFileChooser1 = new javax.swing.JFileChooser();
play = new javax.swing.JToggleButton();
pause = new javax.swing.JToggleButton();
stop = new javax.swing.JToggleButton();
browse = new javax.swing.JToggleButton();
pfad = new javax.swing.JFormattedTextField();
slider = new javax.swing.JSlider();
titel = new javax.swing.JLabel();
time = new javax.swing.JLabel();
playlist = new javax.swing.JToggleButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Musicplayer");
play.setText("Play");
play.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playActionPerformed(evt);
}
});
pause.setText("Pause");
pause.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pauseActionPerformed(evt);
}
});
stop.setText("Stop");
stop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
stopActionPerformed(evt);
}
});
browse.setText("Browse");
browse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseActionPerformed(evt);
}
});
pfad.setText("Pfad eingeben oder Datei mittels Browse einbinden");
pfad.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
pfadMouseClicked(evt);
}
});
pfad.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
public void mouseDragged(java.awt.event.MouseEvent evt) {
pfadMouseDragged(evt);
}
});
pfad.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
pfadActionPerformed(evt);
}
});
slider.setValue(0);
titel.setText("Musictitel");
time.setText("Time");
playlist.setText("Playlist");
playlist.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
playlistActionPerformed(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()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(titel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(play)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(pause)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(stop)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(browse)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(playlist))
.addGroup(layout.createSequentialGroup()
.addComponent(time, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(slider, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE)
.addComponent(pfad, javax.swing.GroupLayout.DEFAULT_SIZE, 364, Short.MAX_VALUE))
.addGap(60, 60, 60)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(titel)
.addGap(21, 21, 21)
.addComponent(slider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(time)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 42, Short.MAX_VALUE)
.addComponent(pfad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(play)
.addComponent(pause)
.addComponent(stop)
.addComponent(browse)
.addComponent(playlist))
.addContainerGap())
);
pack();
}// </editor-fold>
private void browseActionPerformed(java.awt.event.ActionEvent evt) {
// action
javax.swing.JFileChooser browse = new javax.swing.JFileChooser();
browse.setAcceptAllFileFilterUsed(false);
FileFilter filter1 = new ExtensionFileFilter("MP3-Musikdateien", new String[] { "MP3" });
FileFilter filter2 = new ExtensionFileFilter("WAV-Musikdateien", new String[] { "WAV" });
FileFilter filter3 = new ExtensionFileFilter("OGG-Musikdateien", new String[] { "OGG" });
browse.setFileFilter(filter3);
browse.setFileFilter(filter2);
browse.setFileFilter(filter1);
if (browse.showOpenDialog(null) == 0) {
File f=browse.getSelectedFile();
String s=f.getAbsolutePath();
pfad.setText(s);
}
}
private void stopActionPerformed(java.awt.event.ActionEvent evt) {
// action
}
private void pauseActionPerformed(java.awt.event.ActionEvent evt) {
// action
}
private void playActionPerformed(java.awt.event.ActionEvent evt) {
// action
}
private void pfadActionPerformed(java.awt.event.ActionEvent evt) {
// action
}
private void pfadMouseClicked(java.awt.event.MouseEvent evt) {
// action
if (pfad.getText().startsWith("Pfad"))
{
pfad.setText("");
}
}
private void pfadMouseDragged(java.awt.event.MouseEvent evt) {
// action
pfad.setText("");
}
private void playlistActionPerformed(java.awt.event.ActionEvent evt) {
//action
new Playlist().setVisible(true);
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JToggleButton browse;
private javax.swing.JFileChooser jFileChooser1;
private javax.swing.JToggleButton pause;
private javax.swing.JFormattedTextField pfad;
private javax.swing.JToggleButton play;
private javax.swing.JToggleButton playlist;
private javax.swing.JSlider slider;
private javax.swing.JToggleButton stop;
private javax.swing.JLabel time;
private javax.swing.JLabel titel;
// End of variables declaration
}
Alles anzeigen