簡體   English   中英

在Eclipse中播放音頻文件

[英]Playing audio File in Eclipse

因此,我一直試圖在Eclipse中播放.wav文件。 我將.wav文件從桌面拖到eclipse中的src文件夾中。 然后,我發現了一些應該在Java中播放音樂的代碼。 當我開始時,沒有任何作用。 唯一出現的是

Picked up _JAVA_OPTIONS: -Xmx256M

我認為這與我的問題無關,因為它已經出現在程序結尾。 無論如何,如果有人可以幫助,那就太好了。 我認為我的問題是在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)
    {

    }
  }
}

使用File對象獲取AudioSystem並不太好。 通常,我認為最好使用getResource()並允許類加載器找到聲音文件。

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

或者,如果您需要絕對路徑,則可以使用以下方法:

// 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());

最近,我在Java音頻方面遇到了很多麻煩。 這是Clips和AudioInputStream的實現。 用eclipse編碼,因此對您來說應該不是問題。 另外,您也可以使用MediaPlayer,但它需要更多的Java知識並且更加復雜。

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());
        }
    }
}

評論了一下,所以應該很自我解釋。 如果您希望.wav文件位於“ / src”文件夾中,則只需在“ \\ StartScreen.wav”之前添加“ \\ src”,即可將您放置在正確的目錄中。 看起來不像KevinO的解決方案那么復雜,但是我仍在學習Java。

我還發現Clip在播放音頻文件方面相當挑剔。 有些.wav文件要求我將其重新編碼為16位.wav,這是我在Audacity中所做的。

希望這會有所幫助。 祝好運!


編輯:只需復制並粘貼您的代碼即可對其進行測試。 看起來工作正常,因此看起來您將文件指向錯誤的目錄。 只需使用我對路徑String所做的實現,就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM