1

I am making a soundboard prank app. I can get everything else in the Program Working Fine except the playing audio part of the Program. I am using Java Sound and right now I am getting a Unsupportedaudiofileexception when i try to play the sound in a window.(I have the file set to a absolute file location(temporarily)) but there are also lots of more errors that make no sense.

(I have every 25 lines commented on and the ones that are referenced as errors on the console)

Code:

package com.dashboard.rickroll;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class RickRoll { 

    JFrame frame;
    Container con;
    JPanel buttonPanel;
    JButton playButton;
    JLabel lblRickRoll;
    String rRSound; //line 25
    ButtonHandler bHandler = new ButtonHandler();
    RickRollSound rRS = new RickRollSound();
    

    int playX = 110;
    int playY = 120;
    int playW = 60;
    int playH = 60;
    
    int rRlblX = 25;
    int rRlblY = 10;
    int rRlblW = 250;
    int rRlblH = 60;

    public static void main(String[] args) {
        
        new RickRoll(); 

    }
    
    public RickRoll() {
        
        frame = new JFrame();
        frame.setSize(300,300);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); //line 50
        frame.getContentPane().setBackground(Color.white);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setAlwaysOnTop(false);
        frame.setBackground(Color.WHITE);
        con = frame.getContentPane();
        frame.setVisible(true);
        
        playButton = new JButton("Play");
        playButton.setFocusPainted(false);
        playButton.setBounds(playX, playY, playW, playH);
        playButton.setFont(new Font("Times New Roman",Font.PLAIN, 12));
        playButton.setBackground(Color.LIGHT_GRAY);
        playButton.addActionListener(bHandler);
        con.add(playButton);
        
        lblRickRoll = new JLabel("RickRoll");
        lblRickRoll.setBounds(rRlblX, rRlblY, rRlblW, rRlblH);
        lblRickRoll.setFont(new Font("Times New Roman", Font.BOLD, 60));
        con.add(lblRickRoll);
        
        rRSound = "C:\\Users\\bduncan0856\\Desktop\\Sounds\\rickRoll.WAV";
    }
    //line 75
    public class RickRollSound {
        Clip clip;
        
        public void setFile(String soundFileName) {
            
            try {
                File file = new File(soundFileName);
                AudioInputStream rickRoll = AudioSystem.getAudioInputStream(file); //line 83
                clip = AudioSystem.getClip();
                clip.open(rickRoll);
                
            }catch(Exception e) {
                System.out.println("Error");
                e.printStackTrace();
            }
        }
        public void play() {
            
            clip.setFramePosition(0);
            clip.start();
        }
    }
    public class ButtonHandler implements ActionListener{
        
        public void actionPerformed(ActionEvent event) { //line 100
            
            rRS.setFile(rRSound); //line 102
            rRS.play();
        }
    }

    public void setVisible(boolean b) {
        frame.setVisible(true);
        
    }
}

Console:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
Error
    at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
    at com.dashboard.rickroll.RickRoll$RickRollSound.setFile(RickRoll.java:83)
    at com.dashboard.rickroll.RickRoll$ButtonHandler.actionPerformed(RickRoll.java:102)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at com.dashboard.rickroll.RickRoll$RickRollSound.play(RickRoll.java:94)
    at com.dashboard.rickroll.RickRoll$ButtonHandler.actionPerformed(RickRoll.java:103)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
greg-449
  • 109,219
  • 232
  • 102
  • 145
Bryce Duncan
  • 21
  • 1
  • 5
  • The second exception is because `clip` is null as it was not assigned because of the first exception. So the real error is the `UnsupportedAudioFileException` - `AudioSystem.getAudioInputStream` does not recognise the contents of the file. – greg-449 Sep 16 '21 at 14:05
  • See: https://stackoverflow.com/questions/64509691/how-can-i-add-some-sound-to-my-java-jframe/64509714#64509714 for a basic example. – camickr Sep 16 '21 at 14:08
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – George Z. Sep 16 '21 at 14:13
  • Can you show us the details of the audio file you are hoping to load? The error suggests to me that it is not using a supported format. This is happening more often these days as many wav files are being created with higher precision than javax.sound.sampled supports. I would try testing your code with a wav file that has 16-bit encoding and 41000fps stereo. 24-bit or 32-bit are now starting to be seen, also 48000 and high fps rates. These are unreadable AFAIK currently. BTW, you can convert a given wav with Audacity, if the wav is only available in an unsupported format. – Phil Freihofner Sep 16 '21 at 22:39
  • BTW, good job adding the error message and fixing the capitalization of the variables from the last go round. I still think it would be good to switch to URL from File, but this shouldn't affect the basic playback question. It won't become an issue probably until/if you decide to package your program in a jar. – Phil Freihofner Sep 16 '21 at 22:42

0 Answers0