2 Replies - 467 Views - Last Post: 16 June 2020 - 12:05 AM Rate Topic: -----

#1 baki2001   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 11-June 20

How to play multiple WAV sound files when multiple buttons are clicked

Posted 11 June 2020 - 06:04 PM

Hi I am trying to build a simple dial tone software in java using GUI. I have built the panels and buttons everything works fine except I cant seem to play sounds when a button is pressed. I have about 12 WAV files for each button to simulate the keypad tones. I have tried to build a PlaySound method that takes in a file parameter so that I can import the sound file but it doesn't seem to work. I don't know where to put my sound playing code so that each button plays a different sound. I have built two classes one for the frame and one to view. This is my frame class NOT my driver class.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;

/**
 *  
 */
public class dialerFrame extends JFrame
{
	private JTextField display;// creating text field display
	private static final int FRAME_WIDTH = 500;// frame width
	private static final int FRAME_HEIGHT = 500;// frame height
	private JButton numButton, clrButton, backButton, dialButton;//creating all buttons needed
	
	
        
	
	 /**
	 * Constructor to build frame
	 */
	 public dialerFrame()
	 {
	        display = new JTextField ("");//setting text field to empty 
		display.setEditable(false);//setting text field to not take in values
		add(display, BorderLayout.NORTH);//assigning north region for the text field and adding to frame
		createNumButtonPanel();
		createOperatorButtonPanel();
		setSize(FRAME_WIDTH, FRAME_HEIGHT);//
	}
	
	
	 /**
	 * Method to construct all numeric buttons from 1-9 and set layout
	 */
	 private void createNumButtonPanel()
	{
		JPanel numButtonPanel = new JPanel();// creating a panel for numbers and symbols only
		numButtonPanel.setLayout(new GridLayout(4,3));//setting 4 by 3 grid layout for the panel
		
		//adding the buttons on the panel
		numButtonPanel.add(makeDigitButton("1"));
		numButtonPanel.add(makeDigitButton("2"));
		numButtonPanel.add(makeDigitButton("3"));
		numButtonPanel.add(makeDigitButton("4"));
		numButtonPanel.add(makeDigitButton("5"));
		numButtonPanel.add(makeDigitButton("6"));
		numButtonPanel.add(makeDigitButton("7"));
		numButtonPanel.add(makeDigitButton("8"));
		numButtonPanel.add(makeDigitButton("9"));
		numButtonPanel.add(makeDigitButton("*"));
		numButtonPanel.add(makeDigitButton("0"));
		numButtonPanel.add(makeDigitButton("#")); 
		
		
		add(numButtonPanel, BorderLayout.CENTER);//adding panel to frame in the center
	}
	
        /**
	 * Method to construct operator buttons and assign their function separately 
	 */
	private void createOperatorButtonPanel()
	{
		JPanel operatorButtonPanel = new JPanel(new GridLayout(1,3));//creating panel for the operator buttons
	 	OperatorButtonListener listener = new OperatorButtonListener();// listener for operator buttons
		//creating operator buttons and assigning action listener 
		clrButton = new JButton("CLR");
		clrButton.addActionListener(listener);
		backButton = new JButton("BACK");
		backButton.addActionListener(listener);
		dialButton = new JButton("DIAL");
		dialButton.addActionListener(listener);
		
		//adding operator buttons to panel
		operatorButtonPanel.add(clrButton);
		operatorButtonPanel.add(backButton);
		operatorButtonPanel.add(dialButton);
		
		//adding panel to frame in the south region
		add(operatorButtonPanel, BorderLayout.SOUTH);
		
		
	}
	
	
         /**
	 * Inner class listener for the numeric buttons
	 */
	private class DigitButtonListener implements ActionListener
	{
	 	private String digit;
		
		 public DigitButtonListener(String digit)
	 	 {
	             this.digit = digit;
		 }
		 
		 /**
		  * Overriding actionPerformed method to be used by the numeric buttons
		  */
	 	 public void actionPerformed(ActionEvent event) 
		 {
			 display.setText(display.getText() + digit);//displaying the corresponding digit on the text field
			 
		 }
	}
	 
		
		 
         /**
	 *Inner class listener for operator buttons
	 */
	private class OperatorButtonListener implements ActionListener
	 {
	 	/**
		* Overriding actionPerformed method to be used for by the operator Button
		*/
		public void actionPerformed (ActionEvent event)
		{
			
			if(event.getSource() == backButton)
			{
  				display.setText(""+display.getText().substring(0, display.getText ().length() - 1));// remove last character
			}
			if(event.getSource() == clrButton)
			{
		 		display.setText("");// clear text field
			}
			if(event.getSource() == dialButton)
			{
				
			}
		}
	}
	
        /**
	 * Makes a button representing a digit of the dialer.
	 * @param digit takes in digit for dialer 
	 * @return numButton the respective button of the dialer
	 */
	public JButton makeDigitButton(String digit)
	{
		numButton = new JButton(digit);
		ActionListener listener = new DigitButtonListener(digit);
		numButton.addActionListener (listener);
		return numButton;
	}
	
	
	
	
}



And this is the only thing i have come up with so far. I don't even know if its the right way to go about this.
try
		{
			File musicPath = new File(musicLocation);
			if(musicPath.exists())
			{
				AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
				Clip clip = AudioSystem.getClip();
				clip.open(audioInput);
				clip.start();
			}
			else
			{
				System.out.println("Cant find");
			}
		}
		catch (Exception ex) 
		{
		ex.printStackTrace();
	    }


Is This A Good Question/Topic? 0
  • +

Replies To: How to play multiple WAV sound files when multiple buttons are clicked

#2 g00se   User is online

  • D.I.C Lover
  • member icon

Reputation: 3698
  • View blog
  • Posts: 16,952
  • Joined: 20-September 08

Re: How to play multiple WAV sound files when multiple buttons are clicked

Posted 12 June 2020 - 02:22 AM

Try it
Better to load your files/images as resources:
http://technojeeves....a-with-netbeans
http://technojeeves....va-with-eclipse

btw class names begin upper case in Java so that should be DialerFrame

This post has been edited by g00se: 12 June 2020 - 02:28 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#3 Radius Nightly   User is offline

  • D.I.C Regular

Reputation: 53
  • View blog
  • Posts: 356
  • Joined: 07-May 15

Re: How to play multiple WAV sound files when multiple buttons are clicked

Posted 16 June 2020 - 12:05 AM

Try with longer hit, to see if audio files will play at all, because it may be the loading delay. It was a problem that i fight and i did not understand why its not working, because i was not thinking outside from the project. Since you got multiple audio files that will play on the same channel, like effects channel, its most likely loading delay.

I mean, for example, setting an ambient music in the game can be a big file, and can take longer to load it, even with compression, but its not a big deal if ambient music will start playing after 2 or 3 seconds after loading the game, its not a big deal, no one will notice. While in other hand, playing some short, specially snappy audio files, that must play instantly when something happened according to the code requirements is most likely the effect sound, like when you shoot from the gun in the game, you cannot afford any sound delay like you can with ambient music kind of group. You can feel even 25MS delay offset thats annoying, i guess once in your lifetime you watched some movie that had a whole audio offset and you was trying to fix the offset in the player, because its not annoying, out of synchronization.

You can load multiple of them and have them ready, but most likely it will not work, because one channel can have loaded and can play only one at the time, so having multiple of them loaded are actually only the path list where audio files are located, but they are not loaded and prepared to be play, means, when you call another audio file, first one has to be stopped, unloaded, and then new file will be loaded and play, and there is this loading delay.
Having file on the HDD, that have like seek time of 16MS delay, on SSD like 0.2MS, on M.2 like 0.02MS, delay does not sounds that bad, like 1SEC is 1000MS, having 60FPS means each image are updated with delay of 16.67MS, its nothing wrong about it, but in audio world, some things have to be instantly, and this response times can vary from drive to drive, disk to disk, time to time, depending on current disk load and can be only higher, so it not very promising for effects that must always be loaded and prepared to be executed.

Putting audio files in the executable file can affect his size, so its wise to use small files, not a long audio files (i mean, you dont wanna have executable file of a 100MB), and its good to use RAW formats, or some in that close range, such as WAV or OGG, because you dont wanna spend time and memory decompressing highly compressed audio files such as MP3. Having this files in executable, as virtual files or in code as a data, means your audio files will be in RAM, and delay to load that audio file from RAM is like 0.02NS (not MS), or basically like electricity fast, instantly.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1