首页 > 解决方案 > 向按下按钮时播放的java程序添加声音

问题描述

我正在制作一个音板恶作剧应用程序。除了播放程序的音频部分外,我可以在程序中正常工作。我正在使用 Java Sound,现在当我尝试在窗口中播放声音时,我得到了 Unsupportedaudiofileexception。(我将文件设置为绝对文件位置(暂时))但还有很多更多的错误没有感觉。

(我对每 25 行进行了评论,而那些在控制台上被引用为错误的行)

代码:

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

安慰:

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)

标签: javaswingaudiojavasound

解决方案


推荐阅读