ユーザーがキーボードのボタンを押すたびに曲が再生されるプログラムを Java で作成しています。ただし、これをテストすると、キーを押すと、一方の曲を停止してから他方の曲を再生するのではなく、曲を重ねて再生しました。プレーヤーでの私のコードは次のとおりです。
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.*;
public class Player
{
public void playmusic(String musicfile) {
File soundFile = new File(musicfile);
try {
Clip clip = AudioSystem.getClip();
if(musicfile.equals("stop")){
clip.stop();
}
else {
AudioInputStream inputStream= AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);
//clip.loop(clip.LOOP_CONTINUOUSLY);
clip.start();
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
私の Game クラスでは、play という Player クラスのインスタンスを作成しました。これがクラスです(音楽に関連するメソッドはkeyPressed()):
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.event.*;
public class Game extends JPanel implements Runnable, KeyListener{
private BufferedImage back;
private int key;
private String[] song;
private Player play;
private ImageIcon menu, choose;
private String screen;
public Game() {
new Thread(this).start();
this.addKeyListener(this);
key =-1;
play = new Player();
song = new String[]{"the_greatest_show.wav","a_million_dreams.wav","a_million_dreams_reprise.wav","come_alive.wav","the_other_side.wav","never_enough.wav","this_is_me.wav","rewrite_the_star.wav","tightrope.wav", "never_enough_reprise.wav","from_now_on.wav"};
letter = 'Z';
menu = new ImageIcon("menu.png");
choose = new ImageIcon("select.png");
screen = "Menu";
}
public void run()
{
try
{
while(true)
{
Thread.currentThread().sleep(5);
repaint();
}
}
catch(Exception e)
{
}
}
public void paint(Graphics g){
Graphics2D twoDgraph = (Graphics2D) g;
if( back ==null)
back=(BufferedImage)( (createImage(getWidth(), getHeight())));
Graphics g2d = back.createGraphics();
g2d.clearRect(0,0,getSize().width, getSize().height);
switch(screen){
case("Menu"):
g2d.drawImage(menu.getImage(), 0, 0, 1000, 700, this);
break;
case("Choose"):
g2d.drawImage(choose.getImage(), 0, 0, 1000, 700, this);
}
twoDgraph.drawImage(back, null, 0, 0);
}
public void stopMusic(){
play.playmusic("stop");
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
key= e.getKeyCode();
char c = (char)(key);
switch(c){
case 'A':
stopMusic();
play.playmusic(song[0]);
break;
case 'S':
stopMusic();
play.playmusic(song[1]);
break;
case 'D':
stopMusic();
play.playmusic(song[2]);
break;
case 'F':
stopMusic();
play.playmusic(song[3]);
break;
case 'G':
stopMusic();
play.playmusic(song[4]);
break;
case 'H':
stopMusic();
play.playmusic(song[5]);
break;
case 'J':
stopMusic();
play.playmusic(song[6]);
break;
case 'K':
stopMusic();
play.playmusic(song[7]);
break;
case 'L':
stopMusic();
play.playmusic(song[8]);
break;
case 'V':
stopMusic();
play.playmusic(song[9]);
break;
case 'B':
stopMusic();
play.playmusic(song[10]);
break;
default:
stopMusic();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
}
この問題のトラブルシューティングを手伝ってください。前もって感謝します。