2

我试图模仿这里找到的代码https://stackoverflow.com/tags/javasound/info但我不能让它通过循环()或开始()播放。我一直在寻找答案,但似乎我的只是侥幸或愚蠢的错误,其他人都足以识别。

import javax.sound.sampled.*;
import java.net.URL;

public class AudioTest
{
    public static void main(String[] args) throws Exception
    {
        URL url = new URL("http://www.public.asu.edu/~wnjones1/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
        clip.open(audioIn);
        clip.start();
    }
}

它具有示例缺少 GUI 的所有内容,但这不重要,不是吗?它至少应该还能玩一次吧?

任何帮助将非常感激。谢谢!

--EDIT-- 这是我从我的网站上提取的一个简单的两秒 .wav 文件。我正在使用 Java7u21。

--EDIT v2.0-- 所以基本上我学到的是......保留GUI。或者使用 Applet,这样您就不必担心 main() 的结尾。

import javax.swing.*;

public class Assignment6me extends JApplet
{
    private int APPLET_WIDTH = 400, APPLET_HEIGHT = 160;

    private AudioPanel ap;

    //The method init initializes the Applet with a Pane with two tabs
    public void init()
    {
        try
        {
            ap = new AudioPanel();
        }
        catch(Exception e)
        {}
        getContentPane().add(ap);
        setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size
    }
}



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


public class AudioPanel extends JPanel
{
    public AudioPanel() throws Exception
    {
        File file = new File("Don't Stop Believin'.wav");
        Clip clip = AudioSystem.getClip();
         // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.getAudioInputStream( file );
        clip.open(ais);
        clip.start();
    }
}
4

1 回答 1

1

在 Java Sound 信息中看到的(工作)源。页正是。

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

我提请您注意:

                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");

添加那部分,它应该没问题。


所以我不能在没有 GUI 的情况下播放任何文件?

我不记得基于命令行的应用程序。确实会播放声音,但这是可能的。

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
import java.util.Scanner;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        Scanner scanner = new Scanner (System.in);
        scanner.nextInt();
    }
}
于 2013-04-28T06:28:04.250 回答