I'm trying to get a better understanding of Java by creating a small 2D game. I feel like the best way to learning is by struggling, so I try not to get help on any problems I have; I just solve them myself. However, I've tried almost everything any I can't get my background music to play in my exported version of my game. All the images work and everything else pulled using the " getClass()" method works, but the music doesn't.
Sorry I have to put all of the code from my BackgroundMusicManager class, but because I want to do things without help, my first version of my programs are usually very messy and require a lot of optimization. In short, you kinda have to see where things are called and initialized and whatnot to really see how the things work.
package com.cs.dd.audioManager;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import javax.sound.sampled.*;
import com.nickgirga.dd.main.*;
import sun.applet.Main;
public class BackgroundMusicManager implements Runnable {
//URLs
public URL mmBgMusicURL = getClass().getResource("/audio/backgroundMusic/casual.wav");
//Files
public File[] bgMusics = { new File(mmBgMusicURL.getFile()) };
public File bgMusic = bgMusics[0];
private static Clip clip;
public static int newGameState;
public static boolean enabled = true;
public static boolean asPlaying = false;
public static Thread bgMusicPlayer = new Thread(new BackgroundMusicManager());
public void play () {
if (enabled) {
try {
if (bgMusic != null) clip = AudioSystem.getClip();
if (bgMusic != null) clip.open(AudioSystem.getAudioInputStream(bgMusic));
if (bgMusic != null) clip.start();
//System.out.println(bgMusics[0].getAbsolutePath());
loopChecker();
bgMusicPlayer.wait();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (Exception e) {
//e.printStackTrace();
}
}
}
public void loopChecker () {
while (true) {
if (clip.getMicrosecondPosition() >= clip.getMicrosecondLength()) {
bgMusicPlayer.notify();
}
if (!(GameState.state == newGameState)) {
setClip();
}
}
}
public void setClip () {
//System.out.println("setClip");
newGameState = GameState.state;
if (GameState.state == 0) {
clip.stop();
bgMusic = bgMusics[0];
bgMusicPlayer.notify();
} else {
if (GameState.state == 1) {
clip.stop();
bgMusic = bgMusics[0];
bgMusicPlayer.notify();
} else {
clip.stop();
bgMusic = null;
bgMusicPlayer.notify();
}
}
}
#Override
public void run() {
while (true) {
play();
}
}
}
After exporting this as a JAR with the libraries packed into it, the console reads:
java.io.FileNotFoundException: audio\backgroundMusic\casual.wav (The system cannot find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at com.sun.media.sound.WaveFloatFileReader.getAudioInputStream(Unknown Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at com.cs.dd.audioManager.BackgroundMusicManager.play(BackgroundMusicManager.java:33)
at com.cs.dd.audioManager.BackgroundMusicManager.run(BackgroundMusicManager.java:89)
at java.lang.Thread.run(Unknown Source)
I extracted the JAR and found the file exactly where the FileNotFoundException points, so I'm left very confused. The audio works while running in Eclipse. The images still show in the export. Why will it work with the images, but not the audio?
Any help is very appreciated! Thank you!!!
Instead of
getClass().getResource("/audio/backgroundMusic/casual.wav");
use
getClass().getClassLoader().getResourceAsStream("/audio/backgroundMusic/casual.wav")
In this way it will look from the root, not from the path of the current invoking class
Related
I'm working on a text-based RPG with some friends using Netbeans. It works all fine and dandy in Netbeans but when I export it to a .jar file I get this error.
Jan 28, 2019 2:27:15 PM Operator.DragonsHead startActionPerformed
SEVERE: null
java.io.FileNotFoundException: File "src\Operator\files\Opening.mid" does not exist!
This happens when the game starts, as we have a "theme" that plays at boot up.
The song plays on Netbeans but not when exported.
I'm relatively new to Java programming, I took a course on it last year.
I've tried looking around the web for people having the same issue, but I can't quite get it to duplicate with my code.
Here's the midi class:
import javax.sound.midi.*;
import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
public class MIDI {
private File file = null;
private Sequencer sequencer = null;
public MIDI (String midiFile) throws FileNotFoundException {
this.file = new File(midiFile);
if (!file.isFile()) {
throw new FileNotFoundException("File \"" + midiFile + "\" does not exist!");
}
try{
sequencer = MidiSystem.getSequencer();
if (sequencer == null){
System.err.println("Error: Sequencer not supported");
return;
}
sequencer.open();
Sequence sequence = MidiSystem.getSequence(file);
sequencer.setSequence(sequence);
}
catch (MidiUnavailableException | InvalidMidiDataException | IOException ex){
}
}
public void play(){
sequencer.start();
}
public void stop() {
sequencer.stop();
}
public void waitAndStop(int millis) {
Runnable song = () -> {
try {
Thread.sleep(millis);
}
catch (InterruptedException e) {
System.err.println("MIDI playback interrupted");
}
stop();
};
Thread t = new Thread(song);
t.start();
}
public long songLengthMicroseconds() {
return sequencer.getMicrosecondLength();
}
public Sequence getSequence(String resource) {
try {
return MidiSystem.getSequence(new File(resource));
}
catch (InvalidMidiDataException | IOException ex) {
return null;
}
}
}
Here's the lines that initialize it and call the song to play:
MIDI midiTest;
midiTest = new MIDI("src\\Operator\\files\\Opening.mid");
midiTest.play();
I'm not sure what the API is of 'MIDI', but unless you want to go through the rigamarole of writing an installer, you cannot use direct file access for resources like icons, pictures, music, and datafiles.
Instead, use the getResource/getResourceAsStream mechanism, which returns URLs/InputStreams. Well written libraries take these just as well as files.
Basic format:
try (InputStream resource = MyClassName.class.getResourceAsStream("Opening.mid")) {
// do something with resource here.
}
where Opening.mid is in the exact same place that MyClassName.class is (so, if you are shipping as a jar, it's in the jar, in the same folder structure as myClassName.class. If you prefer to have a root dir 'music' in your jar, you can pass for example: /music/Opening.mid, with the leading slash to indicate you're going off of the jar root.
secondary observation, if you don't know what to do with an exception, best solution is to add the exception(s) you cannot handle to your method's 'throws' line. If that is somehow not possible, the proper body for a catch block is:
throw new RuntimeException("unhandled checked exception", e);
because right now if an error occurs, your code will silently just keep going. If that was your intent (because, hey, music is optional I guess), I'd still log it SOMEWHERE, right now if an error occurs, you just won't know about it.
I have some questions about playing sound in Java and I hope you can help me out.
1. How can I stop a playing sound with a "Stop" button?
2. How can I slow down (or cooldown time) a sound?
3. I want to create a option frame where I can adjust volume and have mute option, how can I do that?
This is my code:
private void BGM() {
try {
File file = new File(AppPath + "\\src\\BGM.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
Any help will be greatly appreciated, and, Have a nice day!
You're working in an Object Oriented programming lanuage, so let's take advantage of that and encapsulate the management of the clip/audio into a simple class...
public class AudioPlayer {
private Clip clip;
public AudioPlayer(URL url) throws IOException, LineUnavailableException, UnsupportedAudioFileException {
clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(url.openStream()));
}
public boolean isPlaying() {
return clip != null && clip.isRunning();
}
public void play() {
if (clip != null && !clip.isRunning()) {
clip.start();
}
}
public void stop() {
if (clip != null && clip.isRunning()) {
clip.stop();
}
}
public void dispose() {
try {
clip.close();
} finally {
clip = null;
}
}
}
Now, to use it, you need to create a class instance field which will allow you to access the value from anywhere within the class you want to use it...
private AudioPlayer bgmPlayer;
Then, when you need it, you create an instance of AudioPlayer and assign it to this variable
try {
bgmPlayer = new AudioPlayer(getClass().getResource("/BGM.wav"));
} catch (IOException | LineUnavailableException | UnsupportedAudioFileException ex) {
ex.printStackTrace();
}
Now, when you need to, you simply call bgmPlayer.play() or bgmPlayer.stop()
I'm trying to build a game that uses sound effects. I haven't dealt with the Java API before so I may be making some mistakes. That said though, the effects work great — my only problem is that I get a strange buzzing sound for maybe a second whenever my program exits.
Any idea how I might get rid of it? Right now I'm trying to kill any playing sounds just before the exit takes place with the killLoop() method, but that isn't getting me anywhere.
I'd appreciate your help!
public class Sound
{
private AudioInputStream audio;
private Clip clip;
public Sound(String location)
{
try {
audio = AudioSystem.getAudioInputStream(new File(location));
clip = AudioSystem.getClip();
clip.open(audio);
}
catch(UnsupportedAudioFileException uae) {
System.out.println(uae);
}
catch(IOException ioe) {
System.out.println(ioe);
}
catch(LineUnavailableException lua) {
System.out.println(lua);
}
}
public void play()
{
clip.setFramePosition(0);
clip.start();
}
public void loop()
{
clip.loop(clip.LOOP_CONTINUOUSLY);
}
public void killLoop()
{
clip.stop();
clip.close();
}
}
public class Athenaeum
{
public static void main(String[] args) throws IOException
{
final Game game = new Game();
GUI athenaeumGui = new GUI(game);
athenaeumGui.setSize(GUI.FRAME_WIDTH, GUI.FRAME_HEIGHT);
athenaeumGui.setTitle("Athenaeum");
athenaeumGui.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
athenaeumGui.setLocationRelativeTo(null);
athenaeumGui.setMinimumSize(new Dimension(GUI.FRAME_WIDTH, GUI.FRAME_HEIGHT));
athenaeumGui.buildGui();
athenaeumGui.setVisible(true);
athenaeumGui.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we)
{
game.killAudio(); // method calls Sound.killLoop()
System.exit(0);
}
});
}
}
In Java api, they say that the AudioInputStream class has a ".close()" method that "Closes this audio input stream and releases any system resources associated with the stream". Maybe this is something you can try.
Okay, I'm trying to create a custom client for Minecraft (don't worry, my question has nothing to do with Minecraft in particular), and I added an abstract class to manage a configuration file using Java's built-in Properties system. I have a method that loads a properties file or creates it if it doesn't already exist. This method is called at the beginning of all my other methods (although it only does anything the first time its called).
The properties file gets created just fine when I run Minecraft the first time, but somehow when I run it the second time, the file gets blanked out. I'm not sure where or why or how I'm wiping the file clean, can someone please help me? Here's my code; the offending method is loadConfig():
package net.minecraft.src;
import java.util.*;
import java.util.regex.*;
import java.io.*;
/**
* Class for managing my custom client's properties
*
* #author oxguy3
*/
public abstract class OxProps
{
public static boolean configloaded = false;
private static Properties props = new Properties();
private static String[] usernames;
public static void loadConfig() {
System.out.println("loadConfig() called");
if (!configloaded) {
System.out.println("loading config for the first time");
File cfile = new File("oxconfig.properties");
boolean configisnew;
if (!cfile.exists()) {
System.out.println("cfile failed exists(), creating blank file");
try {
configisnew = cfile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
configisnew=true;
}
} else {
System.out.println("cfile passed exists(), proceding");
configisnew=false;
}
FileInputStream cin = null;
FileOutputStream cout = null;
try {
cin = new FileInputStream(cfile);
cout = new FileOutputStream(cfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (!configisnew) { //if the config already existed
System.out.println("config already existed");
try {
props.load(cin);
} catch (IOException e) {
e.printStackTrace();
}
} else { //if it doesn't exist, and therefore needs to be created
System.out.println("creating new config");
props.setProperty("names", "oxguy3, Player");
props.setProperty("cloak_url", "http://s3.amazonaws.com/MinecraftCloaks/akronman1.png");
try {
props.store(cout, "OXGUY3'S CUSTOM CLIENT\n\ncloak_url is the URL to get custom cloaks from\nnames are the usernames to give cloaks to\n");
cout.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
String names = props.getProperty("names");
System.out.println("names: "+names);
try {
usernames = Pattern.compile(", ").split(names);
} catch (NullPointerException npe) {
npe.printStackTrace();
}
System.out.println("usernames: "+Arrays.toString(usernames));
configloaded=true;
}
}
public static boolean checkUsername(String username) {
loadConfig();
System.out.println("Checking username...");
for (int i=0; i<usernames.length; i++) {
System.out.println("comparing "+username+" with config value "+usernames[i]);
if (username.startsWith(usernames[i])){
System.out.println("we got a match!");
return true;
}
}
System.out.println("no match found");
return false;
}
public static String getCloakUrl() {
loadConfig();
return props.getProperty("cloak_url", "http://s3.amazonaws.com/MinecraftCloaks/akronman1.png");
}
}
If it's too hard to read here, it's also on Pastebin: http://pastebin.com/9UscXWap
Thanks!
You are unconditionally creating new FileOutputStream(cfile). This will overwrite the existing file with an empty one. You should only invoke the FileOutputStream constructor when writing a new config file.
if (configloaded)
return;
File cfile = new File("oxconfig.properties");
try {
if (cfile.createNewFile()) {
try {
FileOutputStream cout = new FileOutputStream(cfile);
props.setProperty("names", "oxguy3, Player");
props.setProperty("cloak_url", "http://...");
...
cout.flush();
} finally {
cout.close();
}
} else {
FileInputStream cin = new FileInputStream(cfile);
try {
props.load(cin);
} finally {
cin.close();
}
}
configloaded=true;
} catch(IOException ex) {
e.printStackTrace();
}
I am trying to use Reflection on the server side only of a GWT app. I have a basic example working in a non-GWT example which can be seen below.
package com.xyz.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class EntryPoint {
/**
* #param args
*/
public static void main(String[] args) {
ClassLoader dynClassLoader = ClassLoader.getSystemClassLoader();
Class<?> dynClass = null;
try {
dynClass = dynClassLoader.loadClass("com.xyz.reflection.RunMe");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Object dynInstance = null;
try {
dynInstance = dynClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
Method dynMethod = null;
try {
try {
dynMethod = dynInstance.getClass().getMethod("returnSid",
new Class[] { PassMe.class });
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
String returnValue = (String) dynMethod.invoke(dynInstance,
new Object[] { new PassMe() });
System.out.println("Return Value: " + returnValue.toString());
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
using the aptly named:
package com.xyz.reflection;
public class PassMe {
private String sid = "DEFAULT_SID";
public PassMe() {
this.sid = "INITIATED_SID";
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
}
and:
package com.xyz.reflection;
public class RunMe {
public String returnSid(PassMe s) {
return s.getSid();
}
}
This runs fine. When I try running this from a GWT server side class it doesn't work, and instead returns
java.lang.NoSuchMethodException: com.xyz.reflection.RunMe.returnSid(com.xyz.reflection.PassMe)
If I change the parameter to a String (instead of the 'PassMe' class) it works fine. Why does it not like passing my 'PassMe' class? I thought it might be an issue with serialization despite being 100% server code, but I haven't had any luck with this either.
Thanks in advance for any help anyone can give me with this.
In addition to finrod's solution, you can also change your class loader to something like:
ClassLoader dynClassLoader = PassMe.class.getClassLoader();
And you can use the PassMe.class style lookup again. The class used to find a loader doesn't seem to matter though. Just not the system loader.
Weird stuff. I wouldn't doubt if GWT is doing something weird with the class loader though.
Edit: Yep. GWT sets the system class loader to com.google.appengine.tools.development.IsolatedAppClassLoader in dev mode.
I think this could be related to Class loading - but it is just a hunch as I cannot experiment with it in context similar to yours.
Here are some suggestions to try:
You use:
ClassLoader dynClassLoader = ClassLoader.getSystemClassLoader();
dynClass = dynClassLoader.loadClass("com.xyz.reflection.RunMe");
To load the RunMe Class.
However to load the PassMe class you use:
PassMe.class
Try to load the PassMe Class through the dynClassLoader and use that instance in the getMethod() instead of the PassMe.class.
I wonder, do you need to use the dynClassLoader?
This is pretty much a shot in the dark, but does it help if you replace
dynMethod = dynInstance.getClass().getMethod("returnSid",
new Class[] { PassMe.class });
with
dynMethod = dynInstance.getClass().getMethod("returnSid", PassMe.class );
?
It doesn't make a difference outside of a GWT server, but it may exercise the container's VM differently.
Did you put your reflection file in the server side package?
For example:
org.myproject.client - your gwt client package (put here your java files that are able to be complied into java script NO REFLECTION)
org.myproject.server - put here any java files including reflection
org.myproject.shared - put here java classes that are able to be compiled into java script