嗨,我正在尝试制作一个播放一首歌曲,然后暂停或关闭以换成另一首歌曲的应用程序!
这是我的完整项目!
公共类SimpleAudioPlayer {
Long currentFrame;
Clip clip;
//status of the clip
String status;
AudioInputStream audioInputStream;
static String filePath;
// constructor to initialize streams and clip
public SimpleAudioPlayer(String filePath)
throws UnsupportedAudioFileException,
IOException, LineUnavailableException
{
// create AudioInputStream object
audioInputStream =
AudioSystem.getAudioInputStream(new File(filePath).getAbsoluteFile());
// create clip reference
clip = AudioSystem.getClip();
// open audioInputStream to the clip
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
public void play()
{
clip.start();
status = "play";
}
public void pause()
{
if (status.equals("paused"))
{
System.out.println("audio is already paused");
return;
}
this.currentFrame =
this.clip.getMicrosecondPosition();
clip.stop();
status = "paused";
}
// Method to resume the audio
public void resumeAudio() throws UnsupportedAudioFileException,
IOException, LineUnavailableException
{
if (status.equals("play"))
{
System.out.println("Audio is already "+
"being played");
return;
}
clip.close();
resetAudioStream();
clip.setMicrosecondPosition(currentFrame);
this.play();
}
// Method to restart the audio
public void restart() throws IOException, LineUnavailableException,
UnsupportedAudioFileException
{
clip.stop();
clip.close();
resetAudioStream();
currentFrame = 0L;
clip.setMicrosecondPosition(0);
this.play();
}
// Method to stop the audio
public void stop() throws UnsupportedAudioFileException,
IOException, LineUnavailableException
{
currentFrame = 0L;
clip.stop();
clip.close();
System.out.println("Stoped");
}
IOException,
LineUnavailableException
{
if (c > 0 && c < clip.getMicrosecondLength())
{
clip.stop();
clip.close();
resetAudioStream();
currentFrame = c;
clip.setMicrosecondPosition(c);
this.play();
}
}
public void resetAudioStream() throws UnsupportedAudioFileException, IOException,
LineUnavailableException
{
audioInputStream = AudioSystem.getAudioInputStream(
new File(filePath).getAbsoluteFile());
clip.open(audioInputStream);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
}
//我这个项目的主要Gui
公共类MainGui扩展JFrame实现ActionListener {
static final int FRAME_WIDTH = 1200; 静态最终int FRAME_HEIGHT = 800;
JButton exit, stop, s1, s2, s3, s4;
JLabel background;
MainGui(String title) {
super(title);
setLayout(null);
setVisible(true);
setSize(FRAME_WIDTH, FRAME_HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//gif
Icon imgIcon = new ImageIcon(this.getClass().getResource("gifs/giphy.gif"));
JLabel label = new JLabel(imgIcon);
label.setBounds(FRAME_WIDTH/2 -300, FRAME_HEIGHT - 800, 600, 500);
getContentPane().add(label);
setBackground(Color.DARK_GRAY);
//Song Butons
s1 = new JButton("Hooked on a Feeling");
this.add(s1);
s1.setBounds(FRAME_WIDTH/2 -100 ,FRAME_HEIGHT -200, 200, 50 );
s1.addActionListener(this);
s2 = new JButton("I Want You Back");
this.add(s2);
s2.setBounds(FRAME_WIDTH/2-100, FRAME_HEIGHT- 150, 200, 50);
s2.addActionListener(this);
s3 = new JButton("The Pina Colada Song");
this.add(s3);
s3.setBounds(FRAME_WIDTH/2-100, FRAME_HEIGHT- 100, 200, 50);
s3.addActionListener(this);
s4 = new JButton("Come and get your Love ");
this.add(s4);
s4.setBounds(FRAME_WIDTH/2-100, FRAME_HEIGHT- 250, 200, 50);
s4.addActionListener(this);
stop = new JButton("STOP");
this.add(stop);
stop.setBounds(FRAME_WIDTH/2 -55 ,FRAME_HEIGHT/2 +40, 100, 50);
stop.addActionListener(this);
exit = new JButton("Close");
this.add(exit);
exit.setBounds(FRAME_WIDTH/2 -55 ,FRAME_HEIGHT/2-400, 100, 50);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Object src = e.getSource();
if(src.equals(exit)) {
System.exit(0);
}else if(src.equals(s1)) {
try {
SimpleAudioPlayer sap = new SimpleAudioPlayer("src/wavs/Hooked.wav");
sap.play();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
;
}else if(src.equals(s2)) {
}else if(src.equals(s3)) {
try {
SimpleAudioPlayer sap = new SimpleAudioPlayer("src/wavs/Escape.wav");
sap.play();
} catch (UnsupportedAudioFileException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (LineUnavailableException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}else if(src.equals(s4)) {
}else if(src.equals(stop)) {
}
}
}
我的声音可以播放,但是当我播放新歌时,第一首歌曲会随着我输入的新歌曲而继续播放,并且stopClip
方法不起作用。