private void loadAndPlayAudio(String audioResource) {
   try (AudioInputStream soundStream =
       AudioSystem.getAudioInputStream(getClass().getResource(audioResource))) {
     DataLine.Info info = new DataLine.Info(Clip.class, soundStream.getFormat());
     Clip clip = (Clip) AudioSystem.getLine(info);
     clip.open(soundStream);
     clip.start();
   } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
     ex.printStackTrace();
   }
 }
  @Test
  public void playWavTest() {
    // pathname写你想播放的文件路径
    File file = new File("C://XXX");
    try {
      AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
      // audioInputStream就是你要播放的流了在这里面声明你的类,然后调用类方法传入audioInputStream

    } catch (UnsupportedAudioFileException | IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
Exemple #3
0
  public void play(boolean loop) {
    try {
      audio = AudioSystem.getAudioInputStream(url);
      clip = AudioSystem.getClip();
      clip.open(audio);

      if (loop) {
        clip.loop(Clip.LOOP_CONTINUOUSLY);
      }
      clip.start();
      if (!loop) {
        if (clip.getFramePosition() == clip.getFrameLength()) {
          clip.close();
        }
      }

    } catch (UnsupportedAudioFileException | IOException e) {
      e.printStackTrace();
    } catch (LineUnavailableException e) {
      e.printStackTrace();
    }
  }
Exemple #4
0
  /**
   * Loads and returns a Clip object, you can use this to play a sound or music, file must be a WAV.
   *
   * <p>MP3 and other sound formats are not yet supported
   *
   * @param path
   * @return
   */
  public static Clip playSound(String path) {

    AudioInputStream audioStream = null;
    try {
      audioStream = AudioSystem.getAudioInputStream(Class.class.getResource(path));
    } catch (UnsupportedAudioFileException | IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    AudioFormat format = audioStream.getFormat();

    DataLine.Info info = new DataLine.Info(Clip.class, format);

    Clip aClip = null;
    try {
      aClip = (Clip) AudioSystem.getLine(info);
      aClip.open(audioStream);
    } catch (LineUnavailableException | IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return aClip;
  }
  public void actionPerformed(ActionEvent e) {
    if (dados != null) {
      if (e.getSource() == dados.pane.rollButton) {
        Thread t =
            new Thread(
                () -> {
                  try {
                    Thread.sleep(3000);
                  } catch (InterruptedException e1) {
                    e1.printStackTrace();
                  }

                  if (Objects.equals(dados.pane.text.getText(), "3")) {
                    dados.label.setBounds(70, 316 + 50, 507, 41);
                    dados.label.setText("Tie, Roll again");
                    dados.label.setVisible(true);
                    try {
                      Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                      e1.printStackTrace();
                    }
                    b2.doClick();
                    dados.label.setBounds(70, 316 + 50, 507, 41);
                    dados.label.setText("Tie, Roll again");
                    dados.label.setVisible(true);
                  } else {
                    dados.label.setBounds(150, 316, 507, 41);
                    dados.pane.rollButton.setVisible(false);
                    dados.pane.setVisible(false);
                    try {
                      turno = new FileWriter("turno.txt");
                      pw = new PrintWriter(turno);
                    } catch (IOException e1) {
                      e1.printStackTrace();
                    }

                    if (Objects.equals(dados.pane.text.getText(), "1")) {
                      dados.label.setText("Congratulations , fate is on your side");
                      dados.label.setVisible(true);
                      try {
                        Thread.sleep(500);
                      } catch (InterruptedException e1) {
                        e1.printStackTrace();
                      }

                      dados.label.setText("Now select who is playing first");
                      dados.label.setBounds(295, 316, 507, 41);
                      add(dados.label);

                      Aifirst = new JButton();
                      Aifirst.setBounds(650, 400, 200, 200);
                      Aifirst.setIcon(new ImageIcon("seccond.png"));
                      add(Aifirst);

                      playerfirst = new JButton();
                      playerfirst.setBounds(250, 400, 200, 200);
                      playerfirst.setIcon(new ImageIcon("first.png"));
                      add(playerfirst);

                      player = new JLabel("Player");
                      player.setBounds(350, 620, 200, 30);
                      player.setAlignmentX(CENTER_ALIGNMENT);
                      player.setForeground(Color.WHITE);
                      add(player);

                      ai = new JLabel("Ai");
                      ai.setBounds(750, 620, 200, 30);
                      ai.setAlignmentX(CENTER_ALIGNMENT);
                      ai.setForeground(Color.WHITE);
                      add(ai);

                      playerfirst.addActionListener(this);
                      Aifirst.addActionListener(this);

                      repaint();
                      pw.println(1);
                    } else {
                      dados.label.setText("AI player gets the  first turn");
                      dados.pane.setVisible(true);
                      dados.label.setVisible(true);
                      dados.btnPlay.setVisible(true);
                      repaint();
                      pw.println(2);
                    }

                    try {
                      turno.close();
                    } catch (IOException e1) {
                      e1.printStackTrace();
                    }
                  }
                });
        t.start();
      }

      if (e.getSource() == Aifirst) {
        dados.pane.text.setText("2");
        dados.btnPlay.doClick();
      }

      if (e.getSource() == playerfirst) {
        dados.pane.text.setText("1");
        dados.btnPlay.doClick();
      }

      if (e.getSource() == dados.btnPlay) {
        try {
          player1 = new PlayGui(0, 0, Nombre1, this);
          Thread t =
              new Thread(
                  () -> {
                    try {
                      Thread.sleep(1000);
                    } catch (InterruptedException e1) {
                      e1.printStackTrace();
                    }
                    if (Objects.equals(dados.pane.text.getText(), "2")) {
                      try {
                        player1.Aiturn();
                        player1.contTurn++;
                      } catch (IOException
                          | UnsupportedAudioFileException
                          | LineUnavailableException
                          | InterruptedException e1) {
                        e1.printStackTrace();
                      }
                    } else {
                      player1.firstPlayerTurn();
                    }
                  });
          t.start();
        } catch (IOException | UnsupportedAudioFileException | LineUnavailableException e1) {
          e1.printStackTrace();
        }

        PlayGui.player.pdeck.btnNewButton_1.addMouseListener(this);
        PlayGui.player.pdeck.btnNewButton.addMouseListener(this);
        PlayGui.player.pdeck.textField.addMouseListener(this);

        try {
          addbackground3(this);
        } catch (IOException e1) {
          e1.printStackTrace();
        }
        getContentPane().setBackground(new Color(153, 204, 204));
        getContentPane().setLayout(null);

        player1.repaint.addActionListener(this);
        add(player1);

        setVisible(true);
      }
    }

    if (e.getSource() == b2) {
      try {
        dados = new RollDice();
        addbackground4(this);
      } catch (IOException e1) {
        e1.printStackTrace();
      }
      getContentPane().setLayout(null);

      dados.pane.rollButton.addActionListener(this);
      dados.btnPlay.addActionListener(this);
      add(dados);

      setVisible(true);
    }

    if (player1 != null) {
      if (e.getSource() == player1.repaint) {
        repaint();
      }

      if (e.getSource() == accionarAgarreAutomatico) {
        if (this.player1.getPhaseActual() == 0) {
          if (player1.cardDrawn == 0) {
            if (PlayGui.player.pdeck.Deck.cardsLeft() != 0) {
              CardGui nueva = null;
              try {
                nueva = new CardGui(PlayGui.player.pdeck.Deck.extractCard(), 0, 0);
              } catch (IOException e1) {
                e1.printStackTrace();
              }
              appear(nueva);
              final CardGui finalNueva = nueva;
              Thread t1 =
                  new Thread(
                      () -> {
                        try {
                          Thread.sleep(1000);
                        } catch (InterruptedException e1) {
                          e1.printStackTrace();
                        }
                        try {
                          PlayGui.player.hand.draw(finalNueva);
                        } catch (UnsupportedAudioFileException
                            | IOException
                            | LineUnavailableException e1) {
                          e1.printStackTrace();
                        }
                        repaint();
                      });
              t1.start();
            } else {
              doGameOver();
            }
          } else {
            JOptionPane.showMessageDialog(null, "Sorry , u can pick only a card per turn");
          }
        } else {
          JOptionPane.showMessageDialog(null, "Sorry , u can only pick cards on the draw phase");
        }
      }
    }
  }