Java中的音频播放无法正常工作 [英] Audio playback in java not working correctly

查看:92
本文介绍了Java中的音频播放无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图模仿此处 https://stackoverflow.com/tags/javasound/info 中的代码,但我无法通过loop()或start()使其播放.我一直在寻找答案,但似乎我的is幸或愚蠢的错误,其他所有人都足以识别.

I attempted to mimic the code found here https://stackoverflow.com/tags/javasound/info but I cannot make it play either through loop() or start(). I've looked for answers but it seems like mine is just a fluke or a stupid mistake that everyone else is good enough to recognize.

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的所有内容,但这不重要,对吗?它仍然应该至少可以播放一次吗?

It has everything that the example has short of the GUI but that shouldn't matter, should it? It should still be able to play at least once right?

任何帮助将不胜感激.谢谢!

Any help would be much appreciated. Thank you!

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

--EDIT-- It is a simple two second .wav file that I am pulling from my website. I am using Java7u21.

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

--EDIT v2.0-- So basically what I learned is... Keep the GUI. Or use an Applet so you don't have to worry about the main() ending.

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

推荐答案

在Java Sound信息中看到的(有效)源.页面正好.

The (working) source seen on the Java Sound info. page is precisely.

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!");

添加该部分,就可以了.

Add that part and it should be fine.

因此,没有GUI,我无法播放任何文件吗?

So I can't play any files without a GUI?

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

I can't recall a command line based app. that does play sound, but it is possible.

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

这篇关于Java中的音频播放无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆