【发布时间】:2021-10-14 01:32:07
【问题描述】:
我正在尝试将我的代码最小化以处理多个音频。这看起来很糟糕。我怎样才能将这么多代码最小化为一个清晰易读的代码。
public class Sound {
public static int playerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Spieler x.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Spieler x.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Spieler x.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Spieler x.wav" + ioe.getMessage());
ioe.printStackTrace();
}
return 0;
}
public static int computerSound (char[][] gameBoard) {
try (AudioInputStream inStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream("Bot.wav")) ){
Clip clip = AudioSystem.getClip();
clip.open(inStream);
clip.start();
} catch (LineUnavailableException lue) {
System.out.println("Bot.wav" + lue.getMessage());
lue.printStackTrace();
} catch (UnsupportedAudioFileException uafe) {
System.out.println("Bot.wav" + uafe.getMessage());
uafe.printStackTrace();
} catch (IOException ioe) {
System.out.println("Bot.wav" + ioe.getMessage());
ioe.printStackTrace();
}
return 0;
}
【问题讨论】: