0

I've looked around on stack overflow, and a few other sites, but I can't really find anything helpful. I need to have code that can play an audio file that is inside the same as my Class.java file. In other words, I need it to play a file without typing in the exact location of the file, llike if I was sending it to a friend. Here is what I have:

import java.applet.*;
import java.net.*;
public class MainClass extends Applet {

public void init() {
  try {
     AudioClip clip = Applet.newAudioClip(
                    new URL(“file://C:/sound.wav”));
     clip.play();
  } catch (MalformedURLException murle) {
  murle.printStackTrace();
}
}

But I can't get it to play from just anywhere, only that specific folder. Is there a way to do this without typing "URL" before the file location?

public static void
  • 95
  • 1
  • 6
  • 13
  • Have you tried `file:///sound.wav` ? – ItachiUchiha Aug 21 '14 at 19:42
  • Yes, it didn't play anything :( – public static void Aug 21 '14 at 19:43
  • Do you have the audio file located in your project directory or is it located in `C:/` – Jonny Henly Aug 21 '14 at 19:43
  • My directory in src/. I put it right next to the .java file. C:/ was just used as an example. – public static void Aug 21 '14 at 19:44
  • Please post a **complete** code example. This means it needs to include enough code that we can just copy and paste it and compile without any extra compiler errors. (For Java, import statements can be left out.) – Code-Apprentice Aug 21 '14 at 19:49
  • 2
    This link might help, it's about embedding resources into a Java project. Link - http://stackoverflow.com/questions/3721706/embedding-resources-images-sound-bits-etc-into-a-java-project-then-use-those – Jonny Henly Aug 21 '14 at 19:49
  • @Code-Apprentice I can't do complete because I don't know where any viewers have audio files. Thus, it will *always* get an error. Substitute your own URL and it will work. – public static void Aug 21 '14 at 19:50
  • You can certainly make it more complete than what you have posted here. For example, you are missing the enclosing class. You are also missing a `main()` method, if this is a desktop app, or the appropriate methods if this is an Applet. – Code-Apprentice Aug 21 '14 at 19:53
  • Note that "complete" means that it reproduces **exactly** the problem which you are asking about. In this case, the code you have given will produce many compiler errors unrelated to your question because you are missing some key elements of syntax. – Code-Apprentice Aug 21 '14 at 19:55
  • I've had this problem too. If you are on a desktop pc and you have a pci-e or usb sound card, you shouldn't use it but you should use your motherboard built-in sound card. It worked for me, sorry for my english. – Marcello Davi Aug 21 '14 at 20:06

2 Answers2

1

I believe Applet.audioClip() is intended for use inside Applets, rather than a desktop app. One of the limitations is that you can only use a URL to locate the sound resource. On the other hand the Java Sound API is more versatile. It allows you to locate the sound resource with a File object as well as many other options.

You also need to figure out how to refer to your file. In particular, if you want to use a relative path, you need to figure out what base path your environment will start from. Embedding resources (images, sound bits, etc) into a Java project then use those resources will give you more details about how to resolve this issue.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

Change your URL declaration , change "file://C:/sound.wav" to "file:C:/sound.wav"

import java.applet.*;
import java.net.*;
public class MainClass {
public static void main(String[] args) {
  try {
     AudioClip clip = Applet.newAudioClip(
                    new URL("file:C:/sound.wav"));
     clip.play();
  } catch (MalformedURLException murle) {
  murle.printStackTrace();
}}}

*I had tested it and working great under NetBeans IDE

Fevly Pallar
  • 3,059
  • 2
  • 15
  • 19