2013-01-24 61 views
0

這是我第一次嘗試在我的代碼中使用歌曲。 我一直在關注如何播放歌曲的網頁(http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html),但我遇到了錯誤ava.lang.IllegalArgumentException : 無效的格式。 我不明白爲什麼會發生這種情況,我能做些什麼來演奏一首歌。試圖在Java中播放音樂:java.lang.IllegalArgumentException:格式無效

這是不起作用的代碼:

private void startMusic() throws UnsupportedAudioFileException, IOException, LineUnavailableException { 
     // from a wave File 
     File soundFile = new File("/home/simone/OhHa/Pakman02/src/main/java/Pakman/ArsenioLupin.wav"); 
     AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile); 
     Clip clip = AudioSystem.getClip(); 
     clip.open(audioIn); 
// For small-size file only. Do not use this to open a large file over slow network, as it blocks. 
     // start() 
     clip.start(); // play once 
// Loop() 
//  clip.loop(0); // repeat none (play once), can be used in place of start(). 
//  clip.loop(5); // repeat 5 times (play 6 times) 
     clip.loop(Clip.LOOP_CONTINUOUSLY); // repeat forever 
    } 

有什麼建議?

回答

1

試試這個。注意javax.sound.sampled中的進口。*

import java.io.*; 
import java.net.URL; 
import javax.sound.sampled.*; 
import javax.swing.*; 

public class SoundClipTest extends JFrame { 

// Constructor 
public SoundClipTest() { 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setTitle("Test Sound Clip"); 
    this.setSize(300, 200); 
    this.setVisible(true); 

    try { 
    // Open an audio input stream. 
    URL url = this.getClass().getClassLoader().getResource("/home/simone/OhHa/Pakman02/src/main/java/Pakman/ArsenioLupin.wav"); 
    AudioInputStream audioIn = AudioSystem.getAudioInputStream(url); 
    // Get a sound clip resource. 
    Clip clip = AudioSystem.getClip(); 
    // Open audio clip and load samples from the audio input stream. 
    clip.open(audioIn); 
    clip.start(); 
    } catch (UnsupportedAudioFileException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (LineUnavailableException e) { 
    e.printStackTrace(); 
    } 
} 

public static void main(String[] args) { 
    new SoundClipTest(); 
} 
} 

另一種是

import javax.swing.*; 
import sun.audio.*; 
import java.awt.event.*; 
import java.io.*; 
public class Sound { 
public static void main(String[] args) 
{ 
JFrame frame = new JFrame(); 
frame.setSize(200,200); 
JButton button = new JButton("Click me"); 
frame.add(button); 
button.addActionListener(new AL()); 
frame.show(true); 
} 
public static class AL implements ActionListener{ 
public final void actionPerformed(ActionEvent e){ 
music(); 
} 
} 
public static void music(){ 
AudioPlayer MGP = AudioPlayer.player; 
AudioStream BGM; 
AudioData MD; 
ContinuousAudioDataStream loop = null; 
try{ 
BGM = new AudioStream(new FileInputStream("C:\home\simone\OhHa\Pakman02\src\main\java\Pakman\ArsenioLupin.wav")); 
MD = BGM.getData(); 
loop = new ContinuousAudioDataStream(MD); 
}catch(IOException error){ 
System.out.print("file not found"); 
} 
MGP.start(loop); 
} 
} 
+0

或者看看這個http://www.daniweb.com/software-development/java/threads/ 368663 /播放backround-music-in-a-java程序 –

+0

不能...現在錯誤信息如下: 線程「main」中的異常java.lang.NullPointerException \t at com.sun.media。 sound.StandardMidiFileReader.getSequence(StandardMidiFileReader.java:226) \t at javax.sound.midi.Mid iSystem.getSequence(MidiSystem.java:819) \t在com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(SoftMidiAudioFileReader.java:174) \t在javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1145) \t at Pakman.gui.SoundClipTest。 (SoundClipTest.java:28) \t at Pakman.Pakman.createWorld(Pakman.java:56) ecc。 –

+0

你也可以查看這個地址的代碼:https://github.com/SMRomeo/OhHa/tree/master/Pakman02/src/main/java –