Add background music to Java Games -- there are pictures and facts

Little demon king wood 2022-02-13 05:53:38 阅读数:662

add background music java games

It's so congested ! It took a long time to finally Java Add background music to achieve . But it's still very Happy!
On the Internet You can't succeed after looking for a lot . Finally, I figured it out by myself
Many only give code , Xiaobai doesn't know where to put it , Create a file or something . in GitHub Up and down good simple code , Just add a file
Go straight to the picture above okay
Case study 1 : Aircraft battle

In the root directory and src Add music files to the bottom Directory

And then to eclipse New workspace in , Add the folder just now

newly build music.java , An error is the wrong package name , Just change it automatically according to the prompt , Don't move the others .

// The only thing that needs to be revised is the first sentence , Don't change the rest
package com.tarena.fly;
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.DataLine;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
public class Music extends Thread {
private String fileName;
private final int EXTERNAL_BUFFER_SIZE = 524288;
public Music(String wavFile) {
this.fileName = wavFile;
}
@SuppressWarnings("unused")
public void run() {
File soundFile = new File(fileName); // The file name of the music to play
if (!soundFile.exists()) {
System.err.println("Wave file not found:" + fileName);
return;
}
while (true) { // Set up looping
AudioInputStream audioInputStream = null; // Create an audio input stream object
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile); // Create audio objects
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat(); // Audio format
SourceDataLine auline = null; // Source data line
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
if (auline.isControlSupported(FloatControl.Type.PAN)) {
FloatControl pan = (FloatControl) auline.getControl(FloatControl.Type.PAN);
}
auline.start();
int nBytesRead = 0;
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
// auline.close();
}
}
}
}

modify main Entry function content

// The background music starts
Music audioPlayWave = new Music("xcdh.wav");// Open music The name of the music
audioPlayWave.start();
@SuppressWarnings("unused")
int musicOpenLab = 1;

Then run successfully

Case 2 : Gold miners

End , Both successfully joined the background music , This may not be the best way , But it can make Xiaobai join the music successfully , I'm only happy to hand in my homework . I hope I can help you

copyright:author[Little demon king wood],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130553366889.html