开发者

Stopping a clip from playing in the run

I am currently stuck at my project, currently I can play music, but I want to be able to swap song, that way I can make a difference between main menu theme, and game theme.

I have written understanding application, is there a way to stop the clip from playing (from another part of my software) for example, by changing a boolean from true to false. I tried a lot, like making the boolean volatile, that way it would change inside the run, but nothing helped. I need to find a way to stop the clip in the run, can someone help me with this problem?

I have written todo's where the code should come for stopping the clip, if possible can you write down the code to stop my clip from playing inside the run?

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();
  开发者_StackOverflow社区          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();
        */
    }
    }


Are you sure you access the program with the singleton version, cause the singleton is on private in your class, if you don't do that you won't stop the clip but only make new ones

The "getPlayer()" method has the same funcion as an "getInstance()" in sigletons. So are you sure you access the running obkect with the getplayer() call? Because this method is on private in your class, if you don't use this method that you won't stop the clip but only make new ones.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜