2017-05-16 15 views
0

私はeclipseで.wavファイルを再生しようとしています。私は、デスクトップから.wavファイルをeclipseのsrcフォルダにドラッグしました。それから、javaで音楽を演奏するはずのコードが見つかりました。私がそれを始めるときに何も演じない。表示された唯一のものはEclipseでオーディオファイルを再生

Picked up _JAVA_OPTIONS: -Xmx256M 

が、私はそれは私のプログラムの終了時までに登場しているので、これは私の問題に関連しているとは思わないです。とにかく誰かが助けてくれればそれは素晴らしいことだろう。私は自分の問題が日食でファイルを見つけることと何かであると思う。

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) 
    { 

    } 
    } 
} 
+0

ファイルの検索に問題があると思われる場合は、file.exists()を試すことができます –

答えて

0

私はAudioSystemを取得するためにFileオブジェクトを使用して偉大な運を持っていませんでした。一般的に私は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()); 
0

を私は最近、Javaでのオーディオとのトラブルのトンを持っていました。次に、ClipsとAudioInputStreamの実装を示します。それはあなたのために問題ではありませんので、日食でコード化されています。また、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ファイルでは、私がAudacityで行った16ビットの.wavとして再エンコードする必要があります。

これが役に立った。がんばろう!


編集:テストコードをコピーして貼り付けただけです。それは正常に動作するように見えるので、間違ったディレクトリにファイルを指しているように見えます。ちょうど私がパス文字列のためにした実装を使用し、あなたは良いはずです。

関連する問題