在java中停止循环声音

时间:2013-11-21 18:06:31

标签: java

所以我有一个类来播放声音,我可以选择循环或播放一次和停止选项,但停止选项不会停止循环这里是停止声音的代码: 编辑添加了整个事情     包me.welsar55.story;

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Sound {

public static synchronized void clip(final String fileName, final String action) 
{
    new Thread(new Runnable()
    { 
        public void run() 
        {

            if(action.equalsIgnoreCase("play"))
            {
                try 
                {
                    Clip clip = AudioSystem.getClip();
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));
                    clip.open(inputStream);
                    clip.start();
                } 
                catch (Exception e)
                {
                    System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                }
            }
            else if(action.equalsIgnoreCase("loop"))
            {
                try 
                {
                    Clip clip = AudioSystem.getClip();
                    AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));
                    clip.open(inputStream);
                    clip.loop(Clip.LOOP_CONTINUOUSLY);
                } 
                catch (Exception e)
                {
                    System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                }
            }
 else if(action.equalsIgnoreCase("stop"))
{
    try 
    {
        Clip clip = AudioSystem.getClip();
        AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));
        clip.open(inputStream);
        clip.stop();
    } 
    catch (Exception e)
    {
        System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
    }
}
        }
    }).start();
}

}

3 个答案:

答案 0 :(得分:0)

if是一个声明而不是一个循环,我不确定你的意思是不停止循环

答案 1 :(得分:0)

您收到错误了吗?循环的代码在哪里?我想你可能正在创建一个新的文件流来关闭。您可能希望返回用于播放文件的实际流并将其关闭。我不能在没有看到完整代码的情况下确认这一点

        public class Sound {

        private static Map<String,Clip> clipMap = new HashMap();

    public static synchronized void clip(final String fileName, final String action) 
    {
        new Thread(new Runnable()
        { 
            public void run() 
            {

                if(action.equalsIgnoreCase("play"))
                {
                    try 
                    {
                        Clip clip = getClip(fileName);
                        clip.start();
                    } 
                    catch (Exception e)
                    {
                        System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                    }
                }
                else if(action.equalsIgnoreCase("loop"))
                {
                    try 
                    {
                        Clip clip = getClip(fileName);
                        clip.loop(Clip.LOOP_CONTINUOUSLY);
                    } 
                    catch (Exception e)
                    {
                        System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                    }
                }
             else if(action.equalsIgnoreCase("stop"))
            {
                try 
                {
                    Clip clip = getClip(fileName);
                    clip.stop();
                } 
                catch (Exception e)
                {
                    System.out.println("play sound error: " + e.getMessage() + " for " + fileName);
                }
            }
            }

            private Clip getClip(final String fileName)
                    throws LineUnavailableException, UnsupportedAudioFileException,
                    IOException {

                Clip clip = clipMap.get(fileName);
                if(clip == null)
                {
                clip = AudioSystem.getClip();
                AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File(fileName));
                clip.open(inputStream);
                clipMap.put(fileName, clip);
                }
                return clip;
            }
        }).start();
    }

    }

检查是否有效

答案 2 :(得分:0)

而不是clip.stop(),请尝试

if (clip.isOpen()) {
    clip.close();
    inputStream.close();
}