阻止剪辑在运行中播放

时间:2011-05-02 09:13:49

标签: java multithreading audio oop

我目前卡在我的项目中,目前我可以播放音乐,但我希望能够交换歌曲,这样我可以在主菜单主题和游戏主题之间有所区别。

我已经写了理解应用程序,有没有办法阻止剪辑播放(从我的软件的另一部分),例如,通过将布尔值从true更改为false。我尝试了很多,比如使boolean volatile,这样它会在运行中改变,但没有任何帮助。我需要找到一种在运行中停止剪辑的方法,有人可以帮我解决这个问题吗?

我已经编写了todo用于停止剪辑的代码,如果可能的话,你可以写下代码来阻止我的剪辑在游戏中播放吗?

package sound;

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.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

    public class Music implements LineListener, Runnable
    {

    private File soundFile;
    private Thread thread;
    private static Music player;
    private Music audio;
    private Clip clip;
    private boolean stop;

    /**
    * Private because of the singleton
    */
    public Music()
    {
    }

    /**
    * Play a siren sound
    */
    public void playSiren(String musicFileName)
    {
        Music p = getPlayer();
        p.playSirenFile(musicFileName);
    }

    /**
    * Play the siren file
    */
    private void playSirenFile(String musicFileName)
    {
        this.soundFile = new File("Music/"+musicFileName+".wav");
        stop = true;
        thread = new Thread(this);
        thread.setName("SoundPlayer");
        thread.start();

    }

    public Clip getClip()
    {
        return this.clip;
        }
    /**
    * Invoked when the thread kicks off
    */
    @SuppressWarnings("deprecation")
    public void run()
    {
        try
        {
            AudioInputStream stream = AudioSystem.getAudioInputStream(this.soundFile);
            AudioFormat format = stream.getFormat();
            if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW))
            {
                AudioFormat tmp = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                format.getSampleRate(),
                format.getSampleSizeInBits() * 2, format.getChannels(),
                format.getFrameSize() * 2, format.getFrameRate(), true);
                stream = AudioSystem.getAudioInputStream(tmp, stream);
                format = tmp;
            }
            DataLine.Info info = new DataLine.Info(Clip.class, stream
            .getFormat(), ((int) stream.getFrameLength() * format
            .getFrameSize()));

            this.clip = (Clip) AudioSystem.getLine(info);
            this.clip.addLineListener(this);
            this.clip.open(stream);

            this.clip.start();
            try
            {
                thread.sleep(99);
            }
            catch (Exception e)
            {
            }
            while (clip.isActive() && thread != null)
            {
                try
                {
                    thread.sleep(99);
                }
                catch (Exception e)
                {
                    break;
                }
            }
            clip.loop(99999999);
        }

        catch (UnsupportedAudioFileException e)
        {
        e.printStackTrace();
        }
        catch (IOException e)
        {
        e.printStackTrace();
        }
        catch (LineUnavailableException e)
        {
        e.printStackTrace();
        }   

    }

    private static Music getPlayer()
    {
        if (player == null)
        {
            player = new Music();
        }
        return player;
    }

    public void update(LineEvent event)
    {
    }

    public void stopClip()
    {
        // TODO NEED HELP HERE
    }

    public void closeClip()
    {
        clip.close();
    }

    public void startClip()
    {
        clip.start();
    }
    public void volume(float volume)
    {
            //TODO NEED HELP HERE

        /*
        FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
        gainControl.setValue(-50.0f); // Reduce volume IN DECIBELS
        clip.start();
        */
    }
    }

1 个答案:

答案 0 :(得分:1)

您确定使用单件版本访问该程序,因为您的班级中的单身人士是私人的,如果您不这样做,您将不会停止剪辑但只会制作新的

“getPlayer()”方法与sigletons中的“getInstance()”具有相同的功能。那么你确定你通过getplayer()调用访问正在运行的obkect吗?因为此方法在您的类中是私有的,如果您不使用此方法,您将不会停止剪辑但只创建新剪辑。