Audio playback in java not working correctly

拥有回忆 提交于 2019-12-11 02:36:16

问题


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

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!

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

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

回答1:


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

I draw your attention to:

                // 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.


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


来源:https://stackoverflow.com/questions/16260038/audio-playback-in-java-not-working-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!