Why is my music not stopping when I pick a new song?

122 views Asked by At

I am making a program in Java where every time the user press a button on the keyboard, a song would be played. However, when I test this out, pressing the keys made the songs played on top of each other instead of stopping one before playing the other. Here is my code in the Player:

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);
        }
    }
}

In my Game class, I made an instance of the Player class called play. Here is the class (Method related to music is 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) {

    }

}

Please help me troubleshoot this issue. Thank you in advance.

2

There are 2 answers

2
aran On BEST ANSWER

If you call getClip() inside the method, you'll be stopping a second clip, as you lost the reference to the previous one (and will keep playing in background). AudioSystem.getClip() returns a new clip, not the current one.

When you play the next song, you are playing two clips at the same time: first one and the current one, as the one you stopped was a fresh new one.

Clip[0]      Clip[1]         Clip[2]
Song 1   (nosong-stopped)    Song 2

To resolve that, you could make clip be a global variable, so when stop() and close() is called, the current song's clip is the choosen one (you will just have a single clip).

Clip clip = null;
//...

public void playmusic(String musicfile) {
    File soundFile = new File(musicfile);
    try 
    {
       if(musicfile.equals("stop")) 
       {
           if (clip!=null) //do not nest it to the previous condition ...
           {
             clip.stop();
             clip.flush();
             clip.close();
           }
       }
       else   //...so no song is played if you called stop and clip was null
       {
           AudioInputStream inputStream= AudioSystem.getAudioInputStream(soundFile);
           if (clip == null || !clip.isOpen())
               clip = AudioSystem.getClip();
           clip.open(inputStream);
           //clip.loop(clip.LOOP_CONTINUOUSLY);
           clip.start();
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
0
milan pithadia On

Replace playmusic() method and check

public void playmusic(String musicfile) {
        
        try {
            Clip clip = AudioSystem.getClip();
            if(musicfile.equals("stop")){
                clip.stop();
            }
            else {
                File soundFile = new File(musicfile);
                AudioInputStream inputStream= AudioSystem.getAudioInputStream(soundFile);
                clip.open(inputStream);
                //clip.loop(clip.LOOP_CONTINUOUSLY);
                clip.start();
            }
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }