import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.List; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import javax.sound.sampled.AudioFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.DataLine; import javax.sound.sampled.SourceDataLine; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTextField; public class Play extends JFrame implements Runnable { public static int running; JButton select_sound; public List sounds; public JFileChooser chooser; public static String directory; Thread th; public String played; public static JTextField actual_sound; public static JButton play_stop_button; public JButton prew; public JButton next; public Play() { //zakladni nastaveni GUI setTitle("Wav Player"); setSize(300, 290); this.setLayout(new FlowLayout()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //pole pro prehravany soubor actual_sound = new JTextField(22); add(actual_sound); actual_sound.setEditable(false); //button pro prehravani play_stop_button = new JButton(" Přehrát "); //button pro posun sklateb dopredu next = new JButton(">"); next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { //prestane se prehravat th.stop(); //zjisteni umisteni a nastaveni aktualniho souboru if (sounds.getSelectedIndex() == -1) sounds.select(0); else sounds.select(sounds.getSelectedIndex() + 1); played = sounds.getSelectedItem(); running = 0; actual_sound.setText(sounds.getSelectedItem()); play_stop_button.setText(" Přehrát "); } }); //button pro posun sklateb zpet prew = new JButton("<"); prew.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { th.stop(); if (sounds.getSelectedIndex() == -1) sounds.select(0); else sounds.select(sounds.getSelectedIndex() - 1); played = sounds.getSelectedItem(); running = 0; actual_sound.setText(sounds.getSelectedItem()); play_stop_button.setText(" Přehrát "); } }); add(prew); add(play_stop_button); add(next); //nastaveni buttonu pro prehravani a zastaveni prehravani play_stop_button.addActionListener(new ActionListener() { @SuppressWarnings("deprecation") public void actionPerformed(ActionEvent arg0) { //running je 1 pokud se prehrava if (running == 1) { th.stop(); play_stop_button.setText(" Přehrát "); running = 0; } else { create_thread(); th.start(); play_stop_button.setText(" Stop "); } } }); //button pro JChooser pro vyber hudby select_sound = new JButton("Vybrat hudbu"); select_sound.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { select_folder(); } }); select_sound.setPreferredSize(new Dimension(280, 20)); JScrollPane scrool = new JScrollPane(); //list pro seznam sklateb sounds = new List(10); scrool.add(sounds); sounds.add(" "); add(sounds); //action listener pro prehrani po kliknuti sounds.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { if (running == 1) { th.stop(); running = 0; } create_thread(); played = sounds.getSelectedItem(); th.start(); play_stop_button.setText(" Stop "); } }); add(select_sound); setVisible(true); } public void create_thread() { th = new Thread(this); } public void select_folder() { //filechooser pro vyber adresare s hudbou chooser = new JFileChooser(); chooser.setCurrentDirectory(new java.io.File(".")); chooser.setDialogTitle("Vyberte adresář s hudbou"); //nastavime, aby fungoval pouze pro vyber adresare chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); chooser.setAcceptAllFileFilterUsed(false); if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { //setlabel(String.valueOf(chooser.getSelectedFile())); selected(String.valueOf(chooser.getSelectedFile())); } else { selected(""); } } //funkce pro nacteni souboru z adresare private void selected(String string) { directory = string; File dir = new File(directory); String[] files; files = dir.list(); //nacteme vsechny soubory z adresare for (int i = 0; i < files.length; i++) { File f; f = new File(dir, files[i]); if (f.isDirectory()) { ; } else { //pokud se jedna o soubor .wav nebo .au, pridam je do seznamu if (files[i].toLowerCase().endsWith(".wav") || files[i].toLowerCase().endsWith(".au")) sounds.add(files[i]); } } } public static void play(String url) { //nastavime text do JLabelu actual_sound.setText(url); running = 1; try { //pro prehravani pouzivame AudioInputStream ais = AudioSystem.getAudioInputStream(new File( directory + "\\" + url)); //zjistime format audia AudioFormat af = ais.getFormat(); //zjistime, zda je podpora ze systemu DataLine.Info info = new DataLine.Info(SourceDataLine.class, af); if (!AudioSystem.isLineSupported(info)) { System.out.println("Neni podporovan"); System.exit(0); } int frameRate = (int) af.getFrameRate(); int frameSize = af.getFrameSize(); int bufSize = frameRate * frameSize / 10; SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); line.open(af, bufSize); line.start(); byte[] data = new byte[bufSize]; int bytesRead; while ((bytesRead = ais.read(data, 0, data.length)) != -1 ) line.write(data, 0, bytesRead); line.drain(); line.stop(); line.close(); } catch (Exception e) { System.out.println(e.toString()); } //prenastavime hodnotu na tlacitku play_stop_button.setText(" Přehrát "); running = 0; } public void run() { play(played); } public static void main(String[] args) { new Play(); } }