Sound in Java einfügen



  • Sound in Java einfügenhi,
    ich bin neu hier in der java-welt und erstmals ein kleines Programm selber schreiben. Im Moment versuche ich mit Hilfe des Java-Editor einen einfachen 1-armigen Bandit zuprogrammieren.
    In der Schule sind wir leider selbst am Anfang, wodurch mir noch einige Kenntnisse fehlen. Aber jetzt zum Problem: Womit kann man Sounds in applet einfügen?
    Per google hab ich etwas recherchiert, dennoch weiß ich nicht, wie es richtig eingebaut wird. Zum Beispiel wenn man getAudioClip(getDocumentBase(), " ") nimmt, in welchen bestimmten Ordner muss der abgespeichert werden
    oder welchen befehl man nehmen kann, um den ganzen Pfad für den Sounds einzufügen?

    import java.io.*;  //
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;  //
    import java.applet.Applet;
    import java.net.*;   //
    import javax.swing.JFrame; //
    import javax.swing.JOptionPane; //
    import java.applet.*;
    /**
      *
      * Beschreibung
      *
      * @version 1.0 vom 29.04.2009
      * @author 
      */
    
    public class project extends Applet {
      // Anfang Variablen
      private Button button1 = new Button();
      private TextField textField1 = new TextField();
      private TextField textField2 = new TextField();
      private TextField textField3 = new TextField();
      private Label label1 = new Label();
      private TextField textField4 = new TextField();
      private Label label2 = new Label();
      private JOptionPane JOptionPane1= new JOptionPane();
      AudioClip soundFile1;
      AudioClip soundFile2;
      AudioClip soundFile3;
      AudioClip soundFile4;
      AudioClip soundFile5;
      AudioClip soundFile6;
      AudioClip soundFile7;
      AudioClip soundFile8;
      AudioClip soundFile9;
      AudioClip soundFile10;
    
      // Ende Variablen
    
      public void init() {
        Panel cp = new Panel(null);
        cp.setBounds(0, 0, 1192, 493);
        add(cp);
        // Anfang Komponenten
    
        button1.setBounds(456, 72, 227, 73);
        button1.setLabel("Drück auf Glück!");
        cp.add(button1);
        button1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent evt) {
            button1ActionPerformed(evt);
          }
        });
    
        textField1.setBounds(800, 176, 121, 24);
        textField1.setBackground(Color.CYAN);
        textField1.setText("");
        cp.add(textField1);
        textField2.setBounds(512, 176, 121, 24);
        textField2.setBackground(Color.CYAN);
        textField2.setText("");
        cp.add(textField2);
        textField3.setBounds(232, 176, 121, 24);
        textField3.setBackground(Color.CYAN);
        textField3.setText("");
        cp.add(textField3);
        label1.setBounds(520, 32, 98, 16);
        label1.setBackground(Color.CYAN);
        label1.setText("7-armiger Bandit");
        label1.setFont(new Font("MS Sans Serif", Font.PLAIN, 13));
        cp.add(label1);
        textField4.setBounds(0, 248, 1161, 24);
        textField4.setBackground(Color.CYAN);
        textField4.setText("");
        cp.add(textField4);
        label2.setBounds(536, 224, 65, 24);
        label2.setText("Gewinn");
        label2.setFont (new Font("MS Sans Serif", Font.PLAIN, 17));
        label2.setForeground(Color.RED);
        cp.add(label2);
    
        AudioClip soundFile1 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile2 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile3 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile4 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile5 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile6 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile7 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile8 = getAudioClip(getDocumentBase(), " ...wasv");
        AudioClip soundFile9 = getAudioClip(getDocumentBase(), " ...wav");
        AudioClip soundFile10 = getAudioClip(getDocumentBase(), " ...wav");
    
        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
        // Ende Komponenten
    
      }
      // Anfang Ereignisprozeduren
      public void button1ActionPerformed(ActionEvent evt) {
      int k= (int) (Math.random()*9+1);
      String u;
      u=k+"";
      textField1.setText(u);
      int l= (int) (Math.random()*9+1);
      String p;
      p=l+"";
      textField2.setText(p);
      int m= (int) (Math.random()*9+1);
      String i;
      i=m+"";
      textField3.setText(i);
    
      String einer="Die Auszahlung von 5$ folgt in 1 Tag. Bitte haben sie Geduld";
      String zweier="Die Auszahlung von 80$ folgt in 2 Tage. Bitte haben sie Geduld";
      String dreier="Die Auszahlung von 150$ folgt in 2 Wochen. Bitte haben sie Geduld";
      String vierer="Die Auszahlung von 500$ folgt in 1 Monat. Bitte haben sie Geduld";
      String fünfer="Die Auszahlung von 800$ folgt in 2 Monaten. Bitte haben sie Geduld";
      String sechser="Die Auszahlung von 1200$ folgt in 4 Monaten. Bitte haben sie Geduld";
      String siebener="Die Auszahlung von 1800$ folgt in 6 Monaten. Bitte haben sie Geduld";
      String achter="Die Auszahlung von 2500$ folgt in 8 Monaten. Bitte haben sie Geduld";
      String neuner="Die Auszahlung von 10000$ folgt in 10 Monaten...Der gewonnene Betrag kann leider nicht weiter geleitet werden.Wir bitten sie um Verständnis";
      String achtung="Dann haben Sie leider Pech gehabt!";
    
      if (u==p & p==i & i==u )
      {
       if (k==1 & l==1 & m==1)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 5§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(einer);}
       }
       if (k==2 & l==2 & m==2)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 80§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(zweier);}
       }
       if (k==3 & l==3 & m==3)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 150§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(dreier);}
       }
       if (k==4 & l==4 & m==4)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 500§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(vierer);}
       }
       if (k==5 & l==5 & m==5)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 800§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(fünfer);}
       }
       if (k==6 & l==6 & m==6)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 1200§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(sechser);}
       }
       if (k==6 & l==6 & m==6)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 1800§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(siebener);}
       }
       if (k==6 & l==6 & m==6)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 2500§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(achter);}
       }
       if (k==6 & l==6 & m==6)
       {int selectedValue = JOptionPane.showConfirmDialog(null,"Sie gewinnen 10000§!", "Choose one!", JOptionPane.YES_NO_OPTION);
         if(selectedValue == JOptionPane.YES_OPTION) {
            textField4.setText(neuner);}
       }
      }
      else {textField4.setText(achtung);}
    
      String r=textField3.getText();
      if(r.equals(einer))
      {
       soundFile1.play();
       soundFile1.stop();
      }
      if(r.equals(zweier))
      {
       soundFile2.play();
       soundFile2.stop();
      }
      if(r.equals(dreier))
      {
       soundFile3.play();
      soundFile3.stop();
      }
      if(r.equals(vierer))
      {
       soundFile4.play();
       soundFile4.stop();
      }
      if(r.equals(fünfer))
      {
       soundFile5.play();
       soundFile5.stop();
      }
      if(r.equals(sechser))
      {
       soundFile6.play();
       soundFile6.stop();
      }
      if(r.equals(siebener))
      {
       soundFile7.play();
       soundFile7.stop();
      }
      if(r.equals(achter))
      {
       soundFile8.play();
       soundFile8.stop();
      }
      if(r.equals(neuner))
      {
       soundFile9.play();
       soundFile9.stop();
      }
       if(r.equals(achtung))
      {
       soundFile10.play();
      }
    
    }
    }
    

    Bin für jeden Lösungsvorschlag oder eventuelle Verbesserungsvorschläge sehr dankbar.

    mfg
    chris



  • ich hatte das gleiche Prob...

    bei mir ging's mit:

    public (static) AudioClip sound_file;
    
    init(){
       //...
       sound_file = getAudioClip(getClass().getResource("sound.wav"), "sound.wav");
       //...
    }
    

    das static brauchte ich nur um aus einer anderen Klasse das Soundfile zu starten, du brauchst es wahrscheinlich nicht... Wenn du das nicht auch willst ^^

    greez


Anmelden zum Antworten