0

I am unable to play a sound file after selecting from JComboBox and pressing the button. This programs purpose is to do exactly that. However, after I press the button, nothing plays and there are no errors being reported. My main problem is with these classes.

SMPanel.java

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;

public class SMPanel
{
    private JPanel mainPanel;
    private JButton selectionButton;

    SMListenerClass listener = new SMListenerClass(this);
    public JComboBox comboBox;

    static Object soundSelect;



    public SMPanel()
    {
        mainPanel = new JPanel();
        mainPanel.setLayout(new GridLayout(2, 2));

        JComboBox comboBox = new JComboBox();
        comboBox.addItem("1");
        comboBox.addItem("2");
        comboBox.addItem("3");

        comboBox.setEditable(false);
        soundSelect = comboBox.getSelectedItem().toString();
        comboBox.addActionListener(listener);

        selectionButton = new JButton("Test (Play)");
        selectionButton.addActionListener(listener);

        mainPanel.add(comboBox);
        mainPanel.add(selectionButton);


    }


    public JPanel getPanel()
    {
        return mainPanel;
    }

    public static Object getSoundSelect() 
    {
        return soundSelect;
    }

}

SMListenerClass

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

public class SMListenerClass implements ActionListener
{
    SMPanel play;
    File soundFilePlay;
    AudioClip soundPlay;

    public SMListenerClass(SMPanel smPanel) 
    {
        play = smPanel;
    }

    public void actionPerformed(ActionEvent arg0) 
    {
        if(SMPanel.soundSelect.equals("1"))
        {
            soundFilePlay = new File("/Sounds/Railroad_Crossing_Bell.wav"); 
            try
            {
                soundPlay = Applet.newAudioClip(soundFilePlay.toURL());
            }  
            catch(Exception e)
            {
                e.printStackTrace();
            }
            soundPlay.play();
            soundPlay.stop();
        }
    }
} 

I am attempting to use java.applet to play to sound, is there a way to fix this or change the code in a way where the sound can play.

Additional notes: I am using Java SDK 8, some of the other solutions on this site would not work for my program. If you need anymore info, just ask.

RDSparkle
  • 3
  • 2
  • See if this [thread](http://stackoverflow.com/questions/26828282/why-do-i-get-only-hear-noise-after-changing-the-audio-input-in-java/26832131?noredirect=1#comment42286661_26832131) is of any help first. If it isn't, come back and post a comment here. – hfontanez Nov 22 '14 at 00:17
  • 1
    Applets are fickle little creates and have very restrictive sandbox security settings, you may not be alloed to load a file from the disk – MadProgrammer Nov 22 '14 at 00:21
  • The thread didn't help much with the problem, especially with me still developing skills and understanding with this field. Anyways, since applets are restrictive as you said, are you suggesting taking off applets all together and re-write with a different library? – RDSparkle Nov 22 '14 at 00:29

1 Answers1

0

Look at this question: How can I play sound in Java?

Also why are you calling .stop() right after .play()? Wouldn't that be the cause of not playing the sound? Anyway, no need to use Applets. Also play the sound using a new thread, so loading and streaming doesn't crumble your user interface.

Community
  • 1
  • 1
mthandr
  • 3,062
  • 2
  • 23
  • 33