您好我正在尝试创建一个简单的游戏,现在我正在尝试添加几首歌曲在游戏过程中在后台播放,可以根据玩家的意愿停止和跳过。似乎无处不在我看起来似乎无法使示例代码工作。并且唯一可行的示例只能在循环中播放一个文件。
public void loadmedia ()
{
song1 = getAudioClip (getCodeBase (), "song1.au");
song1.play ();
song2 = getAudioClip (getCodeBase (), "song2.au");
song2.play ();
}
song1将正常播放但是song2将无效,即使我通过按钮告诉歌曲1停止播放歌曲2.
答案 0 :(得分:0)
当您需要更改背景歌曲时?或者在歌曲1完成后,歌曲2开始? 抱歉,我可以将其添加为评论,因为我的声誉< 50 这段代码可供您在组合框中选择几首歌曲。试试看。如果它不适合你。再次问我这个问题:)
希望你喜欢它!
import java.applet.AudioClip;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.net.MalformedURLException;
import java.awt.GridBagLayout;
public class SoundApplication extends JPanel
implements ActionListener,
ItemListener {
SoundList soundList;
String auFile = "spacemusic.au";
String aiffFile = "flute+hrn+mrmba.aif";
String midiFile = "trippygaia1.mid";
String rmfFile = "jungle.rmf";
String wavFile = "bottle-open.wav";
String chosenFile;
AudioClip onceClip, loopClip;
URL codeBase;
JComboBox formats;
JButton playButton, loopButton, stopButton;
JLabel status;
boolean looping = false;
public SoundApplication() {
String [] fileTypes = {auFile,
aiffFile,
midiFile,
rmfFile,
wavFile};
formats = new JComboBox(fileTypes);
formats.setSelectedIndex(0);
chosenFile = (String)formats.getSelectedItem();
formats.addItemListener(this);
playButton = new JButton("Play");
playButton.addActionListener(this);
loopButton = new JButton("Loop");
loopButton.addActionListener(this);
stopButton = new JButton("Stop");
stopButton.addActionListener(this);
stopButton.setEnabled(false);
status = new JLabel(
"Click Play or Loop to play the selected sound file.");
JPanel controlPanel = new JPanel();
controlPanel.add(formats);
controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);
JPanel statusPanel = new JPanel();
statusPanel.add(status);
add(controlPanel);
add(statusPanel);
startLoadingSounds();
}
public void itemStateChanged(ItemEvent e){
chosenFile = (String)formats.getSelectedItem();
soundList.startLoading(chosenFile);
}
void startLoadingSounds() {
//Start asynchronous sound loading.
try {
codeBase = new URL("file:" + System.getProperty("user.dir") + "/");
} catch (MalformedURLException e) {
System.err.println(e.getMessage());
}
soundList = new SoundList(codeBase);
soundList.startLoading(auFile);
soundList.startLoading(aiffFile);
soundList.startLoading(midiFile);
soundList.startLoading(rmfFile);
soundList.startLoading(wavFile);
}
public void stop() {
onceClip.stop(); //Cut short the one-time sound.
if (looping) {
loopClip.stop(); //Stop the sound loop.
}
}
public void start() {
if (looping) {
loopClip.loop(); //Restart the sound loop.
}
}
public void actionPerformed(ActionEvent event) {
//PLAY BUTTON
Object source = event.getSource();
if (source == playButton) {
//Try to get the AudioClip.
onceClip = soundList.getClip(chosenFile);
stopButton.setEnabled(true);
onceClip.play(); //Play it once.
status.setText("Playing sound " + chosenFile + ".");
if (onceClip == null) {
status.setText("Sound " + chosenFile + " not loaded yet.");
}
return;
}
//START LOOP BUTTON
if (source == loopButton) {
loopClip = soundList.getClip(chosenFile);
looping = true;
loopClip.loop(); //Start the sound loop.
loopButton.setEnabled(false); //Disable start button.
stopButton.setEnabled(true);
status.setText("Playing sound " + chosenFile + " continuously.");
if (loopClip == null) {
status.setText("Sound " + chosenFile + " not loaded yet.");
}
return;
}
//STOP LOOP BUTTON
if (source == stopButton) {
if (looping) {
looping = false;
loopClip.stop(); //Stop the sound loop.
loopButton.setEnabled(true); //Enable start button.
} else if (onceClip != null) {
onceClip.stop();
}
stopButton.setEnabled(false);
status.setText("Stopped playing " + chosenFile + ".");
return;
}
}
public static void main(String s[]) {
WindowListener l = new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
};
JFrame f = new JFrame("SoundApplication");
f.addWindowListener(l);
f.getContentPane().add(new SoundApplication());
f.setSize(new Dimension(400,100));
f.show();
}
}
答案 1 :(得分:0)
我找到了一个合适的答案,非常感谢那些试图帮助我的人,我最终使用了音频流和输入流。
定义我的输入流:
private AudioStream as;
在应用程序启动时以及按下下一首歌曲按钮时调用的方法更改歌曲
public void musicconfig ()
{
try
{
if (mutestate == false)
{
AudioPlayer.player.stop (as);
InputStream in = new FileInputStream (filename [count]);
as = new AudioStream (in);
AudioPlayer.player.start (as);
}
}
catch (IOException b)
{
b.printStackTrace ();
}
count++;
if (count == 12)
{
count = 1;
}
}
在应用首次运行时设置我的歌曲列表数组
public void mediaload ()
{
filename [1] = "song1.wav";
filename [2] = "song2.wav";
filename [3] = "song3.wav";
filename [4] = "song4.wav";
filename [5] = "song5.wav";
filename [6] = "song6.wav";
filename [7] = "song7.wav";
filename [8] = "song8.wav";
filename [9] = "song9.wav";
filename [10] = "song10.wav";
filename [11] = "song11.wav";
}