Czy jest jakiś sposób, aby zmodyfikować ten kod tak aby mógł odtwarzać MP3 przy użyciu javafx.
Chodzi mi o to że nie chce mi się pisać całego GUI od początku, bo jak rozumiem muszę mieć obiekt Scene
Bardzo proszę o pomoc, projekt mam na zaliczenie, a już dużo czasu mi nie zostało.
EDIT:
Trochę poskracałem kod, bardziej skrócić go nie potrafię.
Byłbym wdzięczny gdyby ktoś pomógł mi napisać metodę która będzie odtwarzała pliki MP3
Kopiuj
import javafx.scene.media.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
public class MyFrame extends JFrame implements ActionListener{
boolean ROUND = false;
boolean RANDOM = false;
void setRandom(){
if (RANDOM == true){
RANDOM = false;
}
else {RANDOM = true;}
}
void setRandom(boolean state){RANDOM = state;}
void setRound(boolean state){ROUND = state;}
void setRound(){
if (ROUND == true){
ROUND = false;
}
else {ROUND = true;}
}
boolean getRandom(){return RANDOM;}
boolean getRound(){return ROUND;}
final int SPACEX = 5;
public File[] playlist;
public File file;
JButton [] button = new JButton [7];
JProgressBar bar;
JCheckBoxMenuItem cbRound, cbRandom;
JMenuBar menuBar;
JMenu menu1, menu2 ;
JMenuItem [] menuItem = new JMenuItem [10];
JLabel sliderLabel;
JSlider volume;
JPanel volume_panel;
public MyFrame(){
super("Odtwarzacz MP3");
button [0] = new JButton(new ImageIcon("images/rewind.png"));
button [0].setActionCommand("Prev");
button [1] = new JButton(new ImageIcon("images/play.png"));
button [1].setActionCommand("Play");
button [2] = new JButton(new ImageIcon("images/pause.png"));
button [2].setActionCommand("Pause");
button [3] = new JButton(new ImageIcon("images/stop.png"));
button [3].setActionCommand("Stop");
button [4] = new JButton(new ImageIcon("images/forward.png"));
button [4].setActionCommand("Next");
button [5]= new JButton(new ImageIcon("images/random.png"));
button [5].setActionCommand("Random");
button [6] = new JButton(new ImageIcon("images/round.png"));
button [6].setActionCommand("Round");
sliderLabel = new JLabel(" Głośność ", JLabel.CENTER);
volume = new JSlider();
volume.setValue(100);
volume.setPreferredSize(new Dimension(230,20));
volume_panel = new JPanel();
volume_panel.add(sliderLabel);
volume_panel.add(volume);
volume_panel.setBackground( Color.WHITE );
bar = new JProgressBar();
bar.setValue(50);
bar.setStringPainted(true);
bar.setPreferredSize(new Dimension(350,20));
bar.setString("Tutuł odtwarzanego utworu");
menuItem [0] = new JMenuItem("Otwórz plik");
menuItem [0].setActionCommand("Open");
menuItem [0].setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_O, ActionEvent.CTRL_MASK));
menuItem [1]= new JMenuItem("Nowa playlista");
menuItem [1].setActionCommand("New_Playlist");
menuItem [1].setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_N, ActionEvent.CTRL_MASK));
menuItem [2]= new JMenuItem("Zakończ");
menuItem [2].setActionCommand("Close");
menuItem [2].setAccelerator(KeyStroke.getKeyStroke(
KeyEvent.VK_4, ActionEvent.ALT_MASK));
menuItem [3]= new JMenuItem("Odtwarzaj");
menuItem [3].setActionCommand("Play");
menuItem [3].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Z, 0));
menuItem [4]= new JMenuItem("Pauza");
menuItem [4].setActionCommand("Pause");
menuItem [4].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, 0));
menuItem [5]= new JMenuItem("Stop");
menuItem [5].setActionCommand("Stop");
menuItem [5].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, 0));
menuItem [6]= new JMenuItem("Następny");
menuItem [6].setActionCommand("Next");
menuItem [6].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, 0));
menuItem [7]= new JMenuItem("Poprzedni");
menuItem [7].setActionCommand("Prev");
menuItem [7].setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_B, 0));
cbRandom = new JCheckBoxMenuItem("Odtwarzanie losowe");
cbRound = new JCheckBoxMenuItem("Zapętlanie");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(400, 200);
setLocation(200,200);
setLayout(new FlowLayout());
setResizable(false);
setVisible(true);
getContentPane().setBackground( Color.WHITE );
for (JButton b : button){
b.setBorder(BorderFactory.createEmptyBorder());
b.setContentAreaFilled(false);
b.addActionListener(this);
}
add(Box.createRigidArea(new Dimension(400,5)));
add(Box.createRigidArea(new Dimension(SPACEX,0)));
add(button [0]);
add(Box.createRigidArea(new Dimension(SPACEX,0)));
add(button [1]);
add(Box.createRigidArea(new Dimension(SPACEX,0)));
add(button [2]);
add(Box.createRigidArea(new Dimension(SPACEX,0)));
add(button [3]);
add(Box.createRigidArea(new Dimension(SPACEX,0)));
add(button [4]);
add(Box.createRigidArea(new Dimension(SPACEX-3,0)));
add(button [5]);
add(volume_panel);
add(button [6]);
add(bar);
//Tworzenie paska narzędzi
menuBar = new JMenuBar();
//Menu Plik
menu1 = new JMenu("Plik");
menuBar.add(menu1);
for (int i = 0 ; i<=2 ; i++){
menuItem [i].addActionListener(this);
menu1.add(menuItem [i]);
menu1.addSeparator();
}
//Menu Odtwarzanie.
menu2 = new JMenu("Odtwarzanie");
menuBar.add(menu2);
for (int i = 3 ; i<=7 ; i++){
menuItem [i].addActionListener(this);
menu2.add(menuItem [i]);
}
menu2.addSeparator();
menu2.add(cbRandom);
menu2.add(cbRound);
setJMenuBar(menuBar);
cbRandom.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (!cbRandom.getState()) {
setRandom(false);
} else {
setRandom(true);
}
}
});
cbRound.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
if (!cbRound.getState()) {
setRound(false);
} else {
setRound(true);
}
}
});
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Close")) {
System.exit(0);
}
if(e.getActionCommand().equals("Open")){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Tylko pliki MP3", "mp3");
chooser.setFileFilter(filter);
chooser.showOpenDialog(this);
file = chooser.getSelectedFile();
System.out.println(file.getPath());
}
if(e.getActionCommand().equals("New_Playlist")) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Tylko pliki MP3", "mp3");
chooser.setFileFilter(filter);
chooser.showOpenDialog(MyFrame.this);
playlist = chooser.getSelectedFiles();
}
if(e.getActionCommand().equals("Play")) {
bar.setString(file.getName().split(".mp3")[0]);
System.out.println(file.getPath());
playSound(file);
}
if(e.getActionCommand().equals("Stop")) {
bar.setString("Tytuł odtwarzanego utworu");
}
if(e.getActionCommand().equals("Random")) {
setRandom();
cbRandom.setSelected(getRandom());
}
if(e.getActionCommand().equals("Round")) {
setRound();
cbRound.setSelected(getRound());
}
}
public void playSound(File file){
String bip = file.toURI().toString();
Media hit = new Media(bip);
final MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();
}
}
import java.awt.EventQueue;
public class Player {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame();
}
});
}
}