Czesc zrobilem music player, podajac sciezke w programie muzyka jest odtwarzana. Chce zrobic tak zeby po nacisnieciu przycisku open bedzie mozliwosc wyboru piosenki, jednak nie wiem jak to zrobic
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.applet.*;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
public class mpetrzyplayer extends JApplet {
public void init()
{
Sound testsong = new Sound("/DC/mp3.wav");
// testsong.playSound();
System.out.println("tewkrof "+songPath);
}
public URL songPath;
public class Sound
{
private AudioClip song;
Sound(String filename)
{
try
{
songPath = new URL(getCodeBase(),filename); // Get the Sound URL
song = Applet.newAudioClip(songPath); // Load the Sound
}
catch(Exception e){} // Satisfy the catch
setSize(330,100);
Container zawartosc = getContentPane();
panel =new JPanel();
zawartosc.add(panel);
setLayout(new BorderLayout());
poleNazwy = new JTextArea("");
poleNazwy.setEditable(false);
add(poleNazwy, BorderLayout.NORTH);
panel = new JPanel();
panel.setLayout(new GridLayout(1,4));
//##################PLAY
JButton play=new JButton("play");
play.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent zd)
{
song.loop(); // Play
poleNazwy.setText("Playing ... "+nazwaUtworu);
}
});
panel.add(play);
//##################STOP
JButton stop=new JButton("stop");
stop.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent zd)
{
song.stop();
poleNazwy.setText("Stop");
}
});
panel.add(stop);
//##################PAUSE
JButton pause=new JButton("pause");
pause.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent zd)
{
song.stop();
poleNazwy.setText("Pause");
}
});
panel.add(pause);
//##################OTWORZ
JButton otworz=new JButton("open");
otworz.addActionListener(new FileOpenListener());
panel.add(otworz);
add(panel, BorderLayout.CENTER);
wybor= new JFileChooser();
}
private class FileOpenListener implements ActionListener
{
public void actionPerformed(ActionEvent zd)
{
wybor.setCurrentDirectory(new File("."));
final FiltrRozszerzenia filtr = new FiltrRozszerzenia();
filtr.dodajRozszerzenie(".wav");
filtr.zmienOpis("Only wav files");
wybor.setFileFilter(filtr);
int wynik= wybor.showOpenDialog(getContentPane());
if(wynik==JFileChooser.APPROVE_OPTION)
{
nazwa=wybor.getSelectedFile().getPath();
nazwaUtworu=wybor.getSelectedFile().getName();
sciezka = nazwa.substring(2,nazwa.length());
sciezka= sciezka.replaceAll("/","\\");
System.out.println(nazwaUtworu);
}
poleNazwy.append("");
sciez="file:DC/"+nazwaUtworu;
System.out.println(sciez +"dsadsa");
URL temp = null;
try {
temp = new URL(sciez);
}
catch (MalformedURLException e){
}
}
}
public URL temp;
public String sciez;
public String nazwaUtworu;
public String nazwa;
private JPanel panel;
private JTextArea poleNazwy;
private JFileChooser wybor;
}
public String sciezka;
}