Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: Creating an audio hash set with list panel and buttons

  1. #1
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating an audio hash set with list panel and buttons

    Hello,
    I am new to this forum. What I want to know is how do hash sets work with audio files, how can they be listed in a list panel control, and how can they be played when users click on play, pause, and stop button controls.

    It would need to include a frame with three buttons, a list panel, and audio files in a hash set.

    If it is possible, then can anyone provide me some examples?

    The audio files would be implemented in a list panel from as a hash set.

    Its purpose is to include multiple tracks in a customized album setting; one track can be played at a time.

    Users are welcome to assist me with some ideas and concepts.

    Regards,

    JohnDBCTX

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    how do hash sets work with audio files
    Are you asking how the HashSet class stores references to File objects? I imagine there is no difference that depends on the type of object being stored.

    can anyone provide me some examples?
    Have you done a search?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    I have searched and found only shell based audio files. Anyway I should continue searching.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    Sorry, I am not understanding what you are trying to do.
    I thought you wanted to write a java GUI to allow audio files to be played.
    Your last post sounds like you are trying to find audio files.
    I'm not sure what searching for audio files has to do with java programming.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    This is how I have gotten far with this code snippet, but it has a lot of compiling errors.

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;
    import javax.swing.*;
    import java.awt.event.*;

    public class SoundClipTest extends JFrame {


    final JButton stopBtn = new JButton("Stop");
    final JButton playBtn = new JButton("Play");


    final DefaultListModel<String> l1 = new DefaultListModel<>();
    l1.addElement("Punch");


    final JList<String> list1 = new JList<>(l1);
    list1.setBounds(100,100, 75,75);

    // Constructor
    public SoundClipTest() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setTitle("Test Sound Clip");
    this.setSize(300, 200);
    this.setVisible(true);
    playBtn.setEnabled(true);

    playBtn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    if (list1.getSelectedIndex() != -1) && (playBtn.enabled(true))
    {
    data = "Programming language Selected: " + list1.getSelectedValue();
    label.setText(data);
    // Open an audio input stream.
    URL url = this.getClass().getClassLoader().getResource("punc h.wav");
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    // Open audio clip and load samples from the audio input stream.
    clip.open(audioIn);
    clip.start();
    }
    }//end actionPerformed
    )}//end ActionListener




    );//end addActionListener()
    public static void main(String[] args) {
    new SoundClipTest();
    }

    }

    Can anyone improve this code snippet?

    Regards,

    JohnDBCTX

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    has a lot of compiling errors.
    Please copy the full text of the error message and paste it here. It has important info about the errors.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;
    import javax.swing.*;
    import java.awt.event.*;
     
    public class SoundClipTest extends JFrame {
     
     
    final JButton stopBtn = new JButton("Stop");
    final JButton playBtn = new JButton("Play");
     
     
    final DefaultListModel<String> l1 = new DefaultListModel<>();
    l1.addElement("Punch");
     
     
    final JList<String> list1 = new JList<>(l1);
    list1.setBounds(100,100, 75,75);
     
    // Constructor
    public SoundClipTest() {
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE );
    this.setTitle("Test Sound Clip");
    this.setSize(300, 200);
    this.setVisible(true);
    playBtn.setEnabled(true);
     
    playBtn.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    if (list1.getSelectedIndex() != -1) && (playBtn.enabled(true))
    {
    data = "Programming language Selected: " + list1.getSelectedValue();
    label.setText(data);
    // Open an audio input stream.
    URL url = this.getClass().getClassLoader().getResource("punc h.wav");
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
    Clip clip = AudioSystem.getClip();
    // Open audio clip and load samples from the audio input stream.
    clip.open(audioIn);
    clip.start();
    }
    }//end actionPerformed
    )}//end ActionListener
     
     
     
     
    );//end addActionListener()
    public static void main(String[] args) {
    new SoundClipTest();
    }
     
    }

    I am still learning how to use the forums correctly, since I am new to this forum.

    By the way, this has to do with audio files within a list control.
    When user clicks a command button and is a list is selected within its control, then music gets to play that file.

    Regards,
    JohnDBCTX

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    Can you copy and paste here the full text of the error messages?

    Also please keep the code's formatting when posting it. The code in post#7 has lost all its formatting - all statements start in column 1 instead of being indented. Properly Indented code is easier to read and understand.

    When user clicks a command button and is a list is selected within its control, then music gets to play that file
    From that point of view, start with developing a GUI with the needed controls and assume at the end you can get the code to play the music.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    I had to redo the entire code snippet.

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class SoundTest01{
     
     
     
     
    public static void main(String args[])
    {
    		JFrame frmX=new JFrame();
    		frmX.setSize(1000, 1000);
    		frmX.setVisible(true);
    		JButton playBtn = new JButton("Play");
    		playBtn.setBounds(50,100,95,30);
     
     
    		JLabel lbl=new JLabel();
    		lbl.setSize(500,100);  
    		DefaultListModel<String> listEm=new DefaultListModel<>();
    		frmX.add(playBtn);
     
    		playBtn.addActionListener(new ActionListener()
     		{
    		public void actionPerformed(ActionEvent e)
    		{
    		//this line below generates a compiler error.
    		 if(listEm.getSelectedIndex()!=-1) 
     
    			//audio content to be played in app.
    		}
    		});		
     
    		listEm.addElement("Item1"); 
    		listEm.addElement("Item2");
    		listEm.addElement("Item3");
    		listEm.addElement("Item4");
            	JList<String> list = new JList<>(listEm);  
    	        list.setBounds(100,100, 75,75);  
    		frmX.add(list);
     
     
     
     
     
    }
     
     
     
    }

    This time, I included a list and button controls to the frame.
    Now I need to get the right method of the list control, so that when user clicks the button and with the item selected, then the audio file will play.
    Anyone is willing to assist me on how to perform this.

    Regards,

    JohnDBCTX

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    What happens when you compile and execute that code?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    Multiple error messages while I have tried to compile it.
    Does the forum allow users screenshots?

    Regards,
    JohnDBCTX

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    Please copy the full text of the error messages and paste it here. No images please, because text can not be copied from an image to include in a response.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Aug 2020
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    import java.io.*;
    import java.net.URL;
    import javax.sound.sampled.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    public class SoundTest01{
     
     
     
     
    public static void main(String args[])
    {
    		JFrame frmX=new JFrame();
    		frmX.setSize(1000, 1000);
    		frmX.setVisible(true);
    		JButton playBtn = new JButton("Play");
    		playBtn.setBounds(50,100,95,30);
     
     
    		JLabel lbl=new JLabel();
    		lbl.setSize(1000,500);
    		DefaultListModel<String> listEm=new DefaultListModel<>();
    		frmX.add(playBtn);
    		frmX.add(lbl);
     
     
    		listEm.addElement("Item1"); 
    		listEm.addElement("Item2");
    		listEm.addElement("Item3");
    		listEm.addElement("Item4");
    		listEm.addElement("Punch");
            	JList<String> list = new JList<>(listEm);  
    	        list.setBounds(100,100, 75,75);  
    		frmX.add(list);
     
     
     
    playBtn.addActionListener(new ActionListener(){  
    	public void actionPerformed(ActionEvent e){  
    		String data="User has not selected any items yet";
               	String choiceSelected1 = "User has selected item 1";
    		String choiceSelected2 = "User has selected item 2";
    		String choiceSelected3 = "User has selected item 3";
    		String choiceSelected4 = "User has selected item 4";
     
     
     
    		int index = list.getSelectedIndex();
    		if (index==0) 
    		{
     
    			lbl.setText(choiceSelected1);
    		}
    		if (index==1)
    		{
    			lbl.setText(choiceSelected2);
    		}
    		if (index==2)
    		{
    			lbl.setText(choiceSelected3);
    		}
    		if (index==3)
    		{
    			lbl.setText(choiceSelected4);
    		}
    		if (index==4)
    		{
     
    	    try {
            	 // Open an audio input stream.
             	URL url = this.getClass().getClassLoader().getResource("punch.wav");
             	AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
             	// Get a sound clip resource.
             	Clip clip = AudioSystem.getClip();
             	// Open audio clip and load samples from the audio input stream.
             	clip.open(audioIn);
             	clip.start();
          		} catch (UnsupportedAudioFileException x) {
             		x.printStackTrace();
          		} catch (IOException x) {
             		x.printStackTrace();
          		} catch (LineUnavailableException x) {
             		x.printStackTrace();
          		}
     
     
    		}
     
    }  		
    });  
     
     
     
    }
     
     
     
    }

    What I have done is to include an audio file after I have added another option to the list.
    I have compiled and run it; it worked.

    Not bad for a beginner in this forum.

    Regards,

    JohnDBCTX

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Creating an audio hash set with list panel and buttons

    It looks like you are making progress. The next area to work on is the layout of the frame. Use a LayoutManager to control where components are placed on the page instead of using the setBounds method.
    See the tutorial: https://docs.oracle.com/javase/tutor...out/index.html

    Here's a link to a list of tutorial pages: http://docs.oracle.com/javase/tutori...ybigindex.html
    You should copy and save that link for future usages. The tutorial has lots of sample code for many of the java component classes.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 6
    Last Post: October 2nd, 2014, 08:12 AM
  2. Threads, Audio Application, Audio loading twice error
    By dpjmie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 19th, 2013, 01:18 PM
  3. problem of creating matrix with JButton or Panel
    By ms_ceng in forum AWT / Java Swing
    Replies: 3
    Last Post: December 16th, 2011, 04:53 PM
  4. [SOLVED] Creating Dynamically Radio Buttons, Labels
    By Onur in forum Java Theory & Questions
    Replies: 15
    Last Post: August 25th, 2011, 08:39 AM
  5. Creating a custom panel:
    By xterradaniel in forum AWT / Java Swing
    Replies: 19
    Last Post: October 3rd, 2010, 07:15 PM

Tags for this Thread