• 0

[java] Robocode - Playing a sound


Question

Perhaps you all can help me with this. I am using the program Robocode, to make my little death, bringing mayhem causing robot. I would like for it to play a sound to add to the effect.

I know that it can play sounds, I have gotten a Mac to play a 6kb random wave file that I have found. However, it will not play on my computer, and this is where I need to develop and test it.

I found the below code on the internet, and it worked sucessfully on the Mac.

However, whenever I try to play the sound on my pc, I get this error:

java.lang.NullPointerException

at com.sun.media.sound.DirectAudioDevice$DirectClip.open(Unknown Source)

at com.sun.media.sound.DirectAudioDevice$DirectClip.open(Unknown Source)

at myRobot.myRobot.playSound(myRobot.java:206)

at myRobot.myRobot.run(myRobot.java:23)

at robocode.peer.RobotPeer.run(RobotPeer.java:633)

at java.lang.Thread.run(Unknown Source)

import javax.sound.sampled.*;	

public void playSound(String filename)
{
	try
	{
		AudioInputStream in = AudioSystem.getAudioInputStream(getDataFile(filename));
		DataLine.Info info = new DataLine.Info(Clip.class, in.getFormat(), ((int)in.getFrameLength()*in.getFormat().getFrameSize()));
		Clip clip = (Clip) AudioSystem.getLine(info);
		clip.open(in);
		clip.setFramePosition(0);
		clip.loop(0);
		clip.drain();
		clip.flush();
	}
	catch (Exception e)
	{
		System.out.println("Exceptioning sound:");
		e.printStackTrace();
	}
}

The file is in the same relative directory on my as it was on the mac.

Do you guys have any idea as to shy this might be happening?

I have Java 1.5.0 update 6 installed on my pc. I know that robocode only works with v1.4.2, so I have update 11 or 13 of that installed on my pc and robocode is configured to use the rt.jar of the 1.4.2 JRE.

Thanks for the help! :)

Also, the wave file that sucessfully played on the Mac is attached.

ringout.wavFetching info...

Link to comment
https://www.neowin.net/forum/topic/453817-java-robocode-playing-a-sound/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Robocode is installed to C:\robocode\.

My robot resides in C:\robocode\robots\myRobot\.

The call to play the file is as such: playSound("ringout.wav");

This wave file is located in both C:\robocode\robots\myRobot\ and C:\robocode\robots\myRobot\myRobot.data\.

It worked on the Mac, so I know its in the right place.

I started wondering if maybe it was my pieced-together Java 5 / 1.4 mix, so I setup XP in a virtual machine, installing only Java 1.4.2_11 and Robocode. Now, it gives me this error:

java.security.AccessControlException: Preventing myRobot.myRobot from access: (java.io.FilePermission ringout.wav read): You may only read files in your own root package directory.

at robocode.security.RobocodeSecurityManager.checkPermission(RobocodeSecurityManager.java:322)

at java.lang.SecurityManager.checkRead(Unknown Source)

at java.io.FileInputStream.<init>(Unknowne)

at com.sun.media.sound.WaveFileReader.getAudioInputStream(Unknown Source)

at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)

at myRobot.myRobot.playSound(myRobot.java:196)

at myRobot.myRobot.run(myRobot.java:23)

at robocode.peer.RobotPeer.run(RobotPeer.java:633)

at java.lang.Thread.run(Unknown Source)

Looks like it can sort-of find the file but not open it for whatever reason... :unsure:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.