2

https://stackoverflow.com/tags/javasound/infoにあるコードを模倣しようとしましたが、loop() または start() を介して再生することはできません。私は答えを探しましたが、私のものはまぐれか愚かな間違いであり、他の誰もが認識するのに十分なようです.

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 以外のすべてが含まれていますが、それは問題ではないはずです。それでも少なくとも一度はプレイできるはずですよね?

どんな助けでも大歓迎です。ありがとうございました!

--編集-- これは、Web サイトから取得した単純な 2 秒の .wav ファイルです。私はJava7u21を使用しています。

--EDIT v2.0-- つまり、基本的に私が学んだことは... GUI を維持することです。または、アプレットを使用して、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 サウンド情報に表示される (作業中の) ソース。ページは正確です。

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 に答える