3

ゲームでのサウンドの再生に問題があります。サウンドの再生を処理するスレッドが終了するとき、その実行メソッドは終了/終了/停止しません。すべてをコメントアウトするとスレッドが作成されなくなるため、このメソッドが問題の原因であることはわかっています。(JVisualVMで確認)。問題は、実行メソッドを終了した後、スレッドが終了しないことです。実際に run() メソッドの最後に到達するように print コマンドを配置しましたが、常に実行されます。

しかし、 でプロセスを確認すると、サウンドを再生するたびJVisualVMにスレッド数が増加します。また、再生されるサウンドごと1にデーモン スレッドの数が増加することにも注目しました。1デーモン スレッドとは何か、どのように機能するのかはわかりませんが、さまざまな方法でスレッドを強制終了しようとしました。リターンによるメソッドのインクルードThread.currentThread .stop() .destroy() .suspend() .interrupt()とリターン。run()

このメッセージを書いているときに、クリップ オブジェクトを閉じる必要があることに気付きました。これにより、余分なスレッドが作成および維持されることはありませんでした。しかし、今では音が時々消えてしまい、その理由がわかりません。現在、サウンドを並行して使用するか、無数のスレッドによって CPU が過負荷になるのを確認するか、新しいサウンドが再生されるたびにサウンドが突然終了するかを選択できます。

誰かが複数のサウンドを並行して再生する別のアプローチを知っているか、私のコードの何が問題なのかを知っている場合は、助けていただければ幸いです。

メソッドは次のとおりです。

public static synchronized void playSound(final String folder, final String name) {
        new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
            @Override
            public void run() {
                Clip clip = null;
                AudioInputStream inputStream = null;
                try{
                    do{
                        if(clip == null || inputStream == null)
                                clip = AudioSystem.getClip();
                                inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                        if(clip != null && !clip.isActive())
                                inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                                clip.open(inputStream);
                                clip.start(); 
                    }while(clip.isActive());
                    inputStream.close();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
4

2 に答える 2

9

Java スレッドに関するいくつかの事項:

  • Run() メソッドを終了すると、スレッドは常に終了します。あなたの場合、呼び出したメソッド内に他のスレッドが作成されますが、スレッドは終了します (名前を付けることで確認し、いつ死ぬかを確認できます)。
  • またはを使用.stop()してスレッドを強制終了しないでください。これらのメソッドは推奨されていないため、使用しないでください。代わりに、基本的に Run() メソッドの最後に到達する必要があります。それが目的ですが、フラグをチェックしてからスローして処理することにより、スレッドの中断をサポートする必要があります(詳細については、How to Stop a Thread を参照してください)。.destroy().suspend()Thread.interrupt()Thread.isInterrupted()InterruptedException
  • 「デーモンスレッドは、プログラムが終了してもスレッドがまだ実行されているときにJVMが終了するのを妨げないスレッドです」 .

コードに関するいくつかのこと:

  • 多くのユーザーが言及しているように、中括弧がありません
  • 何を達成しようとしているのかよくわかりませんでしたが、do-whileループは冗長に見えます。サウンドの再生が終了するのを待つ良い方法は他にもありますが (それが目的の場合)、ループはその 1 つではありません。ing なしで何度も実行されるwhileループSleepは、正当な理由もなく CPU を使い果たします。
  • システムリソースを解放するために、あなたが言及したようにClose()(および Stop())する必要があります。Clip

デバッグ ノート付きの実例:

このコードを実行して、要件を満たしているかどうかを確認してください。System.out.printコードのすべてのビットがいつ発生するかを確認できるように、いくつかのスレッド メソッド呼び出しといくつかの s を追加しました。tryToInterruptSoundとを試してみてmainTimeOut、出力にどのように影響するかを確認してください。

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.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class PlaySound {
    private static boolean tryToInterruptSound = false;
    private static long mainTimeOut = 3000;
    private static long startTime = System.currentTimeMillis();

    public static synchronized Thread playSound(final File file) {

        Thread soundThread = new Thread() {
            @Override
            public void run() {
                try{
                    Clip clip = null;
                    AudioInputStream inputStream = null;
                    clip = AudioSystem.getClip();
                    inputStream = AudioSystem.getAudioInputStream(file);
                    AudioFormat format = inputStream.getFormat();
                    long audioFileLength = file.length();
                    int frameSize = format.getFrameSize();
                    float frameRate = format.getFrameRate();
                    long durationInMiliSeconds = 
                            (long) (((float)audioFileLength / (frameSize * frameRate)) * 1000);

                    clip.open(inputStream);
                    clip.start();
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound started playing!");
                    Thread.sleep(durationInMiliSeconds);
                    while (true) {
                        if (!clip.isActive()) {
                            System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound got to it's end!");
                            break;
                        }
                        long fPos = (long)(clip.getMicrosecondPosition() / 1000);
                        long left = durationInMiliSeconds - fPos;
                        System.out.println("" + (System.currentTimeMillis() - startTime) + ": time left: " + left);
                        if (left > 0) Thread.sleep(left);
                    }
                    clip.stop();  
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound stoped");
                    clip.close();
                    inputStream.close();
                } catch (LineUnavailableException e) {
                    e.printStackTrace();
                } catch (UnsupportedAudioFileException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    System.out.println("" + (System.currentTimeMillis() - startTime) + ": sound interrupted while playing.");
                }
            }
        };
        soundThread.setDaemon(true);
        soundThread.start();
        return soundThread;
    }

    public static void main(String[] args) {
        Thread soundThread = playSound(new File("C:\\Booboo.wav"));
        System.out.println("" + (System.currentTimeMillis() - startTime) + ": playSound returned, keep running the code");
        try {   
            Thread.sleep(mainTimeOut );
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (tryToInterruptSound) {
            try {   
                soundThread.interrupt();
                Thread.sleep(1); 
                // Sleep in order to let the interruption handling end before
                // exiting the program (else the interruption could be handled
                // after the main thread ends!).
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("" + (System.currentTimeMillis() - startTime) + ": End of main thread; exiting program " + 
                (soundThread.isAlive() ? "killing the sound deamon thread" : ""));
    }
}
  • playSound はデーモン スレッドで実行されるため、メイン (および非デーモンのみ) スレッドが終了すると停止します。
  • この人に合わせてサウンド ファイルの長さを計算したので、Clip. このようにThread Sleep()して、CPU を使用せずにそのままにすることができます。私は追加isActive()をテストとして使用して、それが本当に終了したかどうかを確認し、そうでない場合は、残りの時間を何度も計算しますSleep()(次の 2 つの事実により、サウンドはおそらく最初の後にまだ再生されているでしょうSleep: 1. 長さの計算にはマイクロ秒かかりません)。 2. 「スリープを呼び出すと、指定された正確な期間スレッドが中断されると想定することはできません」)。
于 2013-07-19T13:54:40.880 に答える
1

あなたのコードは実際にはこれです

public static synchronized void playSound(final String folder, final String name) {
    new Thread(new Runnable() { // the wrapper thread is unnecessary, unless it blocks on the Clip finishing, see comments
        @Override
        public void run() {
            Clip clip = null;
            AudioInputStream inputStream = null;
            try{
                do{
                    if(clip == null || inputStream == null){
                            clip = AudioSystem.getClip();
                    }
                    inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                    if(clip != null && !clip.isActive()){
                            inputStream = AudioSystem.getAudioInputStream(SoundP.class.getResource(folder + "/" + name));
                    }
                    clip.open(inputStream);
                    clip.start(); 
                }while(clip.isActive());
                inputStream.close();
            } catch (LineUnavailableException e) {
                e.printStackTrace();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

if ステートメントは、それに続く最初のコマンドでのみ機能します。2 番目の 'if' は、ステートメントが既に実行されているため、そのままでは意味がありません。do をループするたびに、クリップがアクティブかどうかに関係なく、クリップが再び「.start」され、別のスレッドが生成されるように見えます。

于 2013-07-19T10:11:05.547 に答える