gpt4 book ai didi

java - 在运行中停止播放剪辑

转载 作者:太空宇宙 更新时间:2023-11-04 08:38:30 25 4
gpt4 key购买 nike

我目前陷入了我的项目,目前我可以播放音乐,但我希望能够交换歌曲,这样我就可以在主菜单主题和游戏主题之间做出区分。

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

我已经在待办事项中编写了用于停止剪辑的代码,如果可能的话,您可以写下代码来停止我的剪辑在运行中播放吗?

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();
*/
}
}

最佳答案

您确定使用单例版本访问程序吗,因为单例在您的类(class)中是私有(private)的,如果您不这样做,您将不会停止剪辑,只会制作新的剪辑

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

关于java - 在运行中停止播放剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5855387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com