Advertisement
Guest User

AudioButton

a guest
Oct 1st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.62 KB | None | 0 0
  1. import java.awt.Button;
  2. import java.awt.Color;
  3. import java.awt.FileDialog;
  4. import java.awt.Frame;
  5. import java.awt.Label;
  6. import java.awt.event.ActionEvent;
  7. import java.awt.event.ActionListener;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.util.ArrayList;
  11.  
  12. import javax.sound.sampled.AudioInputStream;
  13. import javax.sound.sampled.AudioSystem;
  14. import javax.sound.sampled.Clip;
  15. import javax.sound.sampled.LineUnavailableException;
  16. import javax.sound.sampled.UnsupportedAudioFileException;
  17.  
  18. import de.szut.ita13.sommerfeld.gui_soundboard.GUI;
  19.  
  20. public class AudioButton extends Button {
  21.  
  22.     private static final long serialVersionUID = 1L;
  23.     public static final String AUDIOFILE_FORMAT = "*.wav";
  24.     public static FileDialog filedialog;
  25.    
  26.     private String name;
  27.     private int counter;
  28.     private File audioFile;
  29.     private boolean editable;
  30.     private boolean removable;
  31.     private Label counterLabel;
  32.  
  33.    
  34.     public AudioButton(String test) {
  35.         filedialog = new FileDialog(new Frame(), "Select a File");
  36.         filedialog.setDirectory("C:\\");
  37.         filedialog.setFile(AUDIOFILE_FORMAT);
  38.         editable = false;
  39.         removable = false;
  40.         setLabel(test);
  41.         addActionListener(new ActionListener() {
  42.             public void actionPerformed(ActionEvent e) {
  43.                 AudioButton clickedAudioButton = (AudioButton) e.getSource();
  44.                 if(!clickedAudioButton.hasAudioFile() || (clickedAudioButton.hasAudioFile() && clickedAudioButton.isEditable())) {
  45.                     filedialog.setVisible(true);
  46.                     if(filedialog.getFile() != null) {
  47.                         clickedAudioButton.setAudioFile(new File(filedialog.getDirectory() + "/" + filedialog.getFile()));
  48.                         clickedAudioButton.setName(clickedAudioButton.getName());
  49.                         clickedAudioButton.setLabel(name);
  50.                         clickedAudioButton.resetCounter();
  51.                     }
  52.                 } else if((clickedAudioButton.hasAudioFile() && !clickedAudioButton.isEditable() && clickedAudioButton.isRemovable())) {
  53.                     clickedAudioButton.removeAudioFile();
  54.                     if(!AudioButton.hasAudioButtonWithAudioFile(GUI.audioButtons)) {
  55.                         GUI.REMOVABLE = false;
  56.                         GUI.removeAudioButton.setForeground(Color.RED);
  57.                     }
  58.  
  59.                 } else {
  60.                     play(clickedAudioButton.getAudioFile());
  61.                     clickedAudioButton.incrementCounter();
  62.                 }
  63.             }
  64.         });
  65.     }
  66.    
  67.     public static void play(File audioFile) {  
  68.         try {
  69.             Clip clip = AudioSystem.getClip();
  70.             AudioInputStream stream = AudioSystem.getAudioInputStream(audioFile);
  71.             clip.open(stream);
  72.             clip.start();
  73.             GUI.editAudioButton.setEnabled(false);
  74.             GUI.removeAudioButton.setEnabled(false);
  75.             for(Button b : GUI.audioButtons) {
  76.                 b.setEnabled(false);
  77.             }
  78.             clip.drain();
  79.             for(Button b : GUI.audioButtons) {
  80.                 b.setEnabled(true);
  81.             }
  82.             GUI.editAudioButton.setEnabled(true);
  83.             GUI.removeAudioButton.setEnabled(true);
  84.            
  85.         } catch(UnsupportedAudioFileException uafe) {
  86.             System.out.println("wird nicht unterstützt");
  87.         } catch(LineUnavailableException luae) {
  88.             System.out.println("Line ist nicht vorhanden!");
  89.         } catch(IOException ioe) {
  90.             System.out.println("IO Fehler!");
  91.         }
  92.     }
  93.    
  94.     public static boolean hasAudioButtonWithAudioFile(ArrayList<AudioButton> buttons) {
  95.         for(AudioButton ab : buttons) {
  96.             if(ab.hasAudioFile()) {
  97.                 return true;
  98.             }
  99.         }
  100.         return false;
  101.     }
  102.    
  103.     public boolean hasAudioFile() {
  104.         return audioFile != null;
  105.     }
  106.    
  107.     public void removeAudioFile() {
  108.         audioFile = null;
  109.         counterLabel.setText(GUI.NO_PLAYED);
  110.         setLabel(GUI.NO_FILE);
  111.         counter = 0;
  112.         name = "";
  113.         editable = false;
  114.     }
  115.    
  116.     public void incrementCounter() {
  117.         counter++;
  118.         counterLabel.setText(String.valueOf(counter));
  119.     }
  120.    
  121.     public void resetCounter() {
  122.         counter = 0;
  123.     }
  124.    
  125.     public boolean isRemovable() {
  126.         return removable;
  127.     }
  128.    
  129.     public void setRemovable(boolean removable) {
  130.         this.removable = removable;
  131.     }
  132.    
  133.     public boolean getRemoveable() {
  134.         return removable;
  135.     }
  136.    
  137.     public boolean isEditable() {
  138.         return editable;
  139.     }
  140.    
  141.     public void setEditable(boolean editable) {
  142.         this.editable = editable;
  143.     }
  144.    
  145.     public boolean getEditable() {
  146.         return editable;
  147.     }
  148.    
  149.     public void setAudioFile(File audioFile) {
  150.         this.audioFile = audioFile;
  151.         this.name = audioFile.getName().substring(0, audioFile.getName().indexOf("."));
  152.     }
  153.    
  154.     public File getAudioFile() {
  155.         return audioFile;
  156.     }
  157.    
  158.     public String getName() {
  159.         return name;
  160.     }
  161.  
  162.     public void setName(String name) {
  163.         this.name = name;
  164.     }
  165.  
  166.     public int getCounter() {
  167.         return counter;
  168.     }
  169.  
  170.     public void setCounter(int counter) {
  171.         this.counter = counter;
  172.     }
  173.  
  174.     public Label getCounterLabel() {
  175.         return counterLabel;
  176.     }
  177.  
  178.     public void setCounterLabel(Label counterLabel) {
  179.         this.counterLabel = counterLabel;
  180.     }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement