简体   繁体   中英

Playing audio File in Eclipse

So I have been trying to play a .wav file in eclipse. I dragged the .wav file from my desktop into the src folder in eclipse. Then I found some code that is supposed to play music in java. When I start it nothing plays. The only thing that appears is

Picked up _JAVA_OPTIONS: -Xmx256M

I do not think this is related to my issue because it has appeared before at the end of my program. Anyways it would be great if someone could help. I think my issue is somthing with finding the file in eclipse.

package SeulGame;

import java.io.File;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Music
{
  public static void main(String[] args)
  {
    File sound = new File("/Seul/src/StartScreen.wav");

    PlaySound(sound);

  }

  static void PlaySound(File Sound)
  {
    try{
      Clip clip = AudioSystem.getClip();
      clip.open(AudioSystem.getAudioInputStream(Sound));
      clip.start();

      Thread.sleep(clip.getMicrosecondLength()/1000);
    }catch(Exception e)
    {

    }
  }
}

I have not had great luck with using a File object to get AudioSystem . Generally I think it is better to use getResource() and allow the classloader to find the sound file.

InputStream is = this.getClass().getResourceAsStream(soundName);
if (is == null) {
  throw new IOException("Failed to find "+ soundName);
}
AudioInputSystem ais = AudioSystem.getAudioInputStream(is);

In the alternative, if you need an absolute path, the following has worked for me:

// soundName is the name of the sound; here it is an absolute path to the
// .wav file
File soundFile = new File(soundName);
if (! soundFile.exists() ) {
  throw new FileNotFoundException("Did not find: " + soundName);
}

URL url = soundFile.toURI().toURL();
System.out.println(url);  // just to see what it really thinks
AudioInputStream ais = AudioSystem.getAudioInputStream(url);

// NOTE: the above can all be replaced with the getResourceAsStream() approach

//
// get the speakers -- sound has to go somewhere
//
Mixer.Info speakers = null;
Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
for (Mixer.Info mi : mixerInfo) {
  if (mi.getName().startsWith("Speakers")) {
    speakers = mi;
  }
}

mixer = AudioSystem.getMixer(speakers);

DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);

clip = (Clip) mixer.getLine(dataInfo);

//
// play the clip
//
clip.open(ais);
clip.start();
do {
  try {
    Thread.sleep(50);
  }
  catch (InterruptedException e) {
    e.printStackTrace();
  }
} while (clip.isActive());

I recently had a ton of trouble with audio in java. Here's an implementation for Clips and AudioInputStream. Coded in eclipse so it should not be a problem for you. Alternatively, you can also use MediaPlayer but it requires a bit more knowledge of Java and is a bit more complex.

import java.io.File;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

public class Music {
    public static void main(String[] args) {
        //This gets the path to the project, but not into /src for eclipse
        String path = new File("").getAbsolutePath() + "\\StartScreen.wav";
        //Make a File object with a path to the audio file.
        File sound = new File(path);

        try {
            AudioInputStream ais = AudioSystem.getAudioInputStream(sound);
            Clip c = AudioSystem.getClip();
            c.open(ais); //Clip opens AudioInputStream
            c.start(); //Start playing audio

            //sleep thread for length of the song
            Thread.sleep((int)(c.getMicrosecondLength() * 0.001));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

Commented a bit so it should be pretty self explanatory. If you want the .wav file to be in the "/src" folder, just adding "\\src" right before "\\StartScreen.wav" should put you in the right directory. Doesn't seem as complex as KevinO's solution, but I'm still learning java.

I also find that Clip is pretty picky with regard to what audio files you can play. Some .wav files require me to re-encode as 16-bit .wav, which I did in Audacity.

Hope this helped. Good luck!


edit: just copied and pasted your code to give it a test. Looks like it works just fine, so it looks like you are pointing the file to the wrong directory. Just use the implementation I did for the path String and you should be good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM