0

私はオンラインで検索しようとしましたが、リソースは非常に限られています。アクション実行方法で:

actionPerformed(){
---------------
new Sound();}

そしてサウンドクラスで

 public Sound(){
   try {
    String filePath="path\\file.wav";
    File path= new File(filePath);
    AudioInputStream stream;
    AudioFormat format;
    DataLine.Info info;
    Clip clip;

    stream = AudioSystem.getAudioInputStream(path);
    format = stream.getFormat();//becomes formatted
    info = new DataLine.Info(Clip.class, format);// info
    clip = (Clip) AudioSystem.getLine(info);// casting for clip
    clip.open(stream); // clip opens stream with path
    clip.start();
}
    catch (Exception e) {
        System.out.println("errors");
    }

}

ありがとうございました。

4

2 に答える 2

2

メソッドのJavadocによると.start()

回線がデータ I/O を実行できるようにします。すでに実行中の回線で呼び出された場合、このメソッドは何もしません。バッファー内のデータがフラッシュされていない限り、ラインは停止時に未処理だった最初のフレームから I/O を再開します。オーディオのキャプチャまたは再生が開始されると、START イベントが生成されます。

オーディオが非同期で再生されることについては言及されていません。これは、メソッドを呼び出すスレッドによってサウンドが処理されることを意味します。これはstart、イベント ハンドラーでメソッドを呼び出しているため、最終的にはEvent Dispatching Threadオーディオの再生を処理していることを意味します。

このスレッドは、画面上での描画も担当するため、その上で実行される他のもの、場合によってはオーディオは、レンダリングに悪影響を及ぼします。

以下のようなコード (ここから取得) を使用して、オーディオを別のスレッドで再生することができます。これは、遅延の問題に対応する必要があります。

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class AePlayWave extends Thread { 

    private String filename;

    private Position curPosition;

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

    enum Position { 
        LEFT, RIGHT, NORMAL
    };

    public AePlayWave(String wavfile) { 
        filename = wavfile;
        curPosition = Position.NORMAL;
    } 

    public AePlayWave(String wavfile, Position p) { 
        filename = wavfile;
        curPosition = p;
    } 

    public void run() { 

        File soundFile = new File(filename);
        if (!soundFile.exists()) { 
            System.err.println("Wave file not found: " + filename);
            return;
        } 

        AudioInputStream audioInputStream = null;
        try { 
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) { 
            e1.printStackTrace();
            return;
        } catch (IOException e1) { 
            e1.printStackTrace();
            return;
        } 

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try { 
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) { 
            e.printStackTrace();
            return;
        } catch (Exception e) { 
            e.printStackTrace();
            return;
        } 

        if (auline.isControlSupported(FloatControl.Type.PAN)) { 
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT) 
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT) 
                pan.setValue(-1.0f);
        } 

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try { 
            while (nBytesRead != -1) { 
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) 
                    auline.write(abData, 0, nBytesRead);
            } 
        } catch (IOException e) { 
            e.printStackTrace();
            return;
        } finally { 
            auline.drain();
            auline.close();
        } 

    } 
}
于 2013-08-02T05:53:00.510 に答える