java - 如何将声音融入到程序中

标签 java swing audio jmf

因此,我尝试使用另一个类合并声音,以便稍后可以在游戏中使用它,但我不知道如何在不出现错误的情况下执行任何操作,这就是我到目前为止所得到的

主类(Project1.class)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;



public class Project1 {



    public static void main(String[] args) throws Exception{

        Music m = new Music();
        m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        m.setSize(300,300);
        JButton button = new JButton("Click here for 4 second part of the music");
        m.add(button);
        button.addActionListener(new AL());
        m.setVisible(true);


    }

    public static class AL implements ActionListener{

        public final void actionPerformed(ActionEvent e){
            //What do I put here so that I could play the Music from the other class?
        }
    }
}

这是实际播放音乐的类(Music.class)

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.swing.JButton;
import javax.swing.JFrame;

import sun.audio.*;
import sun.*;


public class Music extends JFrame{

    private JButton button;

    public Music() throws Exception {
    super("The title");



    String filename = "/Users/seb/Documents/workspace(NormalJava)/practs/res/backgroundMusic.wav";

    InputStream in = new FileInputStream(new File(filename));
    AudioStream audioStream = new AudioStream(in);


    AudioPlayer.player.start(audioStream);

    Thread.sleep(4000);

    AudioPlayer.player.stop(audioStream);

    }   
}

如果有人可以帮助我,我会非常感激,我从来没有做过声音,我不知道如何做到这一点,我对此非常感到困惑,我会放入什么按钮 ActionListener 类,这样只有当我按下它时,音乐才会开始,然后在 4 秒后停止?如果我把 Thread.sleep(4000);在音乐类中,然后它开始播放音乐,等待 4 秒,停止,然后向我显示按钮

所以,如果有人可以帮助我了解音频,或者也许是另一种更简单的方法。我将不胜感激!

最佳答案

音乐首先播放的原因是因为构造函数中有 play 方法。所以:

public static void main(String[] args) throws Exception{

    Music m = new Music(); // ****** Music plays here
    m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    m.setSize(300,300);
    JButton button = new JButton("Click here for 4 second part of the music");
    m.add(button);
    button.addActionListener(new AL());
    m.setVisible(true);


}

然后,在您设置尺寸等之后。

主方法中的所有内容都应该位于 Music() 的构造函数中。您的音乐播放代码应该位于 ActionListener 类 AL 中。

您还需要确保不会占用事件线程。所以在你的 ActionListener 中你会有类似的东西:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    String filename = "/Users/seb/Documents/workspace(NormalJava)/practs/res/backgroundMusic.wav";

    InputStream in = new FileInputStream(new File(filename));
    AudioStream audioStream = new AudioStream(in);
    AudioPlayer.player.start(audioStream);

    Thread.sleep(4000);

    AudioPlayer.player.stop(audioStream);
  }
}

关于java - 如何将声音融入到程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26808848/

相关文章:

java - 使用音频信号创建随 secret 钥

c# - Dialogic ADPCM VOX 文件 6000 赫兹到 Alvas.Audio 中的 Wave GSM

java - 查找尝试连接到 ServerSocket 的程序的 IP

java - IN 子句在PreparedStatement 上仅返回 1 行

java - 创建随机形状

java - 如何在 Java 中创建自定义 JButton 类?

JavaFX 获取输入

java - 如何从给定的 WEEK_OF_YEAR、DAY_OF_WEEK、YEAR 获取 Calendar.MONTH

java - JSpinner float 输入可能吗?如何?

javascript - 如何设置动态创建的<audio>元素的默认下载文件名?