1

我正在尝试在程序中添加声音。我没有经验如何在程序中添加声音。我正在尝试这个,但这段代码给了我异常“null”。我不知道我必须在 playSound 函数中传递什么参数。所以请帮帮我。

import java.io.InputStream;
import sun.audio.*; //import the sun.audio package
import java.io.*;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Main {

    public Main() {

    }

    public static void main(String[] args) {
        System.out.println("hello there");
        playSound("Hi there");

    }

    public static synchronized void playSound(final String url) {
        new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
                                    // it blocks on the Clip finishing, see
                                    // comments
                    public void run() {
                        try {
                            Clip clip = AudioSystem.getClip();
                            AudioInputStream inputStream = AudioSystem
                                    .getAudioInputStream(Main.class
                                            .getResourceAsStream("pacman_chomp.wav"
                                                    + url));
                            clip.open(inputStream);
                            clip.start();
                        } catch (Exception e) {
                            System.err.println(e.getMessage());
                        }
                    }
                }).start();
    }

}
4

4 回答 4

1

它可能会给您一个空异常,因为它找不到该文件。目前它正在寻找一个名为:

“pacman_chomp.wav你好”

因为 url 的值被设置为“Hi there”,并且您正在像这样构造它:

AudioInputStream inputStream = AudioSystem
                                    .getAudioInputStream(Main.class
                                            .getResourceAsStream("pacman_chomp.wav"
                                                    + url));

我想你想要文件“pacman_chomp.wav”?在这种情况下删除“+ url”。

于 2012-10-02T15:08:32.777 回答
1

既然你说你的 playSound 方法不起作用,这是我编写的一个声音类。

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AePlayWave extends Thread {

    protected static final boolean DEBUG = false;

    protected InputStream inputStream;

    public AePlayWave(InputStream inputStream) {
        this.inputStream = inputStream;
        if (DEBUG) System.out.println("AePlayWave constructor");
    }

    @Override
    public void run() {
        if (DEBUG) System.out.println("AePlayWave running");

        AudioInputStream audioInputStream = verifyInputStream();
        if (audioInputStream == null) {
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine audioLine = openInputStream(format);

        if (DEBUG) System.out.println(audioLine.getLineInfo());

        if (audioLine != null) {
            audioLine.start();
            playInputStream(audioInputStream, audioLine);
        }
    }

    protected AudioInputStream verifyInputStream() {
        if (DEBUG) System.out.println("AePlayWave verifyInputStream");
        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem.getAudioInputStream(inputStream);
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return audioInputStream;
    }

    protected SourceDataLine openInputStream(AudioFormat format) {
        if (DEBUG) System.out.println("AePlayWave openInputStream");
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
        SourceDataLine audioLine = null;
        if (DEBUG) System.out.println("AePlayWave openInputStream try");
        try {
            audioLine = (SourceDataLine) AudioSystem.getLine(info);
            if (DEBUG) System.out.println("AePlayWave openInputStream getLine");
            audioLine.open(format);
            if (DEBUG) System.out.println("AePlayWave openInputStream open");
        } catch (LineUnavailableException e) {
            e.printStackTrace();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return audioLine;
    }

    protected void playInputStream(AudioInputStream audioInputStream,
            SourceDataLine audioLine) {
        if (DEBUG) System.out.println("AePlayWave playInputStream");
        int externalBufferSize = (int) audioInputStream.getFrameLength() * 4;
        if (DEBUG) System.out.println("AePlayWave playInputStream externalBufferSize: " 
                + externalBufferSize);
        int nBytesRead = 0;
        byte[] abData = new byte[externalBufferSize];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) {
                    if (DEBUG) System.out.println("Bytes read: " + nBytesRead);
                    audioLine.write(abData, 0, nBytesRead);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            return;
        } finally {
            audioLine.drain();
            audioLine.close();
        }
    }
}

这就是它的名称:

InputStream inputStream = getClass().getResourceAsStream("alarm-clock-1.wav");
AePlayWave playWave = new AePlayWave(inputStream);
playWave.run();
于 2012-10-02T15:12:37.183 回答
0

剪辑不允许长音频文件。查看这些来自我的游戏引擎的类。

SoundState WavPlayer WavSound

然后使用此代码。

// First load the sound
WavSound snd = WavPlayer.loadSound("pacman_chomp.wav");
// play it then
snd.play();

您可以免费使用这些课程。希望他们有所帮助。

于 2012-10-02T15:03:39.840 回答
0
playSound("pacman_chomp.wav");


public static synchronized void playSound(final String url) {
    new Thread(new Runnable() { // the wrapper thread is unnecessary, unless
                                // it blocks on the Clip finishing, see
                                // comments
                public void run() {
                    try {
                        Clip clip = AudioSystem.getClip();
                        AudioInputStream inputStream = AudioSystem
                                .getAudioInputStream(Main.class
                                        .getResourceAsStream(url));
                        clip.open(inputStream);
                        clip.start();
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }
                }
            }).start();
}

a: 如上所述,它寻找“pacman_chomp.wavHi there”

b:这里不推荐硬编码。你失去了重用代码的能力。例如,如果您在 playSound 方法中硬编码,则无法执行此操作:

    playSound("pacman_chomp.wav");
    playSound("pacman_dies.wav");

此外,方法名称“playSound”的命名很糟糕(在硬编码文件中),因为它总是会播放 pacman_chomp 文件(如果指向正确)。

c: 无论如何,“你好”的目的是什么?

于 2012-10-02T16:17:37.923 回答