Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Button;
- import java.awt.Color;
- import java.awt.FileDialog;
- import java.awt.Frame;
- import java.awt.Label;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineUnavailableException;
- import javax.sound.sampled.UnsupportedAudioFileException;
- import de.szut.ita13.sommerfeld.gui_soundboard.GUI;
- public class AudioButton extends Button {
- private static final long serialVersionUID = 1L;
- public static final String AUDIOFILE_FORMAT = "*.wav";
- public static FileDialog filedialog;
- private String name;
- private int counter;
- private File audioFile;
- private boolean editable;
- private boolean removable;
- private Label counterLabel;
- public AudioButton(String test) {
- filedialog = new FileDialog(new Frame(), "Select a File");
- filedialog.setDirectory("C:\\");
- filedialog.setFile(AUDIOFILE_FORMAT);
- editable = false;
- removable = false;
- setLabel(test);
- addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- AudioButton clickedAudioButton = (AudioButton) e.getSource();
- if(!clickedAudioButton.hasAudioFile() || (clickedAudioButton.hasAudioFile() && clickedAudioButton.isEditable())) {
- filedialog.setVisible(true);
- if(filedialog.getFile() != null) {
- clickedAudioButton.setAudioFile(new File(filedialog.getDirectory() + "/" + filedialog.getFile()));
- clickedAudioButton.setName(clickedAudioButton.getName());
- clickedAudioButton.setLabel(name);
- clickedAudioButton.resetCounter();
- }
- } else if((clickedAudioButton.hasAudioFile() && !clickedAudioButton.isEditable() && clickedAudioButton.isRemovable())) {
- clickedAudioButton.removeAudioFile();
- if(!AudioButton.hasAudioButtonWithAudioFile(GUI.audioButtons)) {
- GUI.REMOVABLE = false;
- GUI.removeAudioButton.setForeground(Color.RED);
- }
- } else {
- play(clickedAudioButton.getAudioFile());
- clickedAudioButton.incrementCounter();
- }
- }
- });
- }
- public static void play(File audioFile) {
- try {
- Clip clip = AudioSystem.getClip();
- AudioInputStream stream = AudioSystem.getAudioInputStream(audioFile);
- clip.open(stream);
- clip.start();
- GUI.editAudioButton.setEnabled(false);
- GUI.removeAudioButton.setEnabled(false);
- for(Button b : GUI.audioButtons) {
- b.setEnabled(false);
- }
- clip.drain();
- for(Button b : GUI.audioButtons) {
- b.setEnabled(true);
- }
- GUI.editAudioButton.setEnabled(true);
- GUI.removeAudioButton.setEnabled(true);
- } catch(UnsupportedAudioFileException uafe) {
- System.out.println("wird nicht unterstützt");
- } catch(LineUnavailableException luae) {
- System.out.println("Line ist nicht vorhanden!");
- } catch(IOException ioe) {
- System.out.println("IO Fehler!");
- }
- }
- public static boolean hasAudioButtonWithAudioFile(ArrayList<AudioButton> buttons) {
- for(AudioButton ab : buttons) {
- if(ab.hasAudioFile()) {
- return true;
- }
- }
- return false;
- }
- public boolean hasAudioFile() {
- return audioFile != null;
- }
- public void removeAudioFile() {
- audioFile = null;
- counterLabel.setText(GUI.NO_PLAYED);
- setLabel(GUI.NO_FILE);
- counter = 0;
- name = "";
- editable = false;
- }
- public void incrementCounter() {
- counter++;
- counterLabel.setText(String.valueOf(counter));
- }
- public void resetCounter() {
- counter = 0;
- }
- public boolean isRemovable() {
- return removable;
- }
- public void setRemovable(boolean removable) {
- this.removable = removable;
- }
- public boolean getRemoveable() {
- return removable;
- }
- public boolean isEditable() {
- return editable;
- }
- public void setEditable(boolean editable) {
- this.editable = editable;
- }
- public boolean getEditable() {
- return editable;
- }
- public void setAudioFile(File audioFile) {
- this.audioFile = audioFile;
- this.name = audioFile.getName().substring(0, audioFile.getName().indexOf("."));
- }
- public File getAudioFile() {
- return audioFile;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getCounter() {
- return counter;
- }
- public void setCounter(int counter) {
- this.counter = counter;
- }
- public Label getCounterLabel() {
- return counterLabel;
- }
- public void setCounterLabel(Label counterLabel) {
- this.counterLabel = counterLabel;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement