音频无法正常工作

时间:2015-12-11 21:48:03

标签: java

上次我问如何正确使用背景音频时,我被告知必须使用一个线程。现在,我已经做到了,但我仍然遇到同样的问题:在歌曲完成之前它不会运行其余的程序,但我需要它在后台播放!帮助任何人? (对不起,代码中没有任何评论。如果你们真的觉得需要一些帮助你,我会用评论更新它)。

import javax.swing.JOptionPane;
import javax.swing.JDialog;
import java.io.*;
import javax.sound.sampled.*;

public class PokemonDemo3 implements LineListener
{
   private boolean done = false;

public  void update(LineEvent event) 
{
    if(event.getType() == LineEvent.Type.STOP || event.getType() == LineEvent.Type.CLOSE) 
    {
         done = true;
    }
}

public void waitonfinish() throws InterruptedException 
{
  while(!done) 
  {
       Thread.sleep(1000);
  } 
}

public static void playSound(final String url) 
{
    try 
    {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(url));
        PokemonDemo3 control = new PokemonDemo3();
        clip.addLineListener(control);
        clip.open(inputStream);
        clip.start();
        control.waitonfinish();

    }
    catch (Exception e) 
    {
        System.err.println(e.getMessage());
    }
}


public static void main(String [] args)
{
    String holder;
    int response;
    String[] options = {"Boy", "Girl"};

    Pokemon intro = new Pokemon();
    playSound("/Users/2018658/Desktop/pokemon.wav");
 }

0 个答案:

没有答案