2012-09-25 53 views
0

我們如何在Java桌面應用程序中播放聲音(如.wma,.mp3等任何格式的音樂文件)? (不是一個小程序)在Java桌面應用程序中播放聲音

我已經使用了下面的代碼(取自堆棧溢出的另一個問題),但它會拋出一個異常。

public class playsound { 
    public static void main(String[] args) { 
s s=new s(); 
s.start(); 
    } 
} 
class s extends Thread{ 
    public void run(){ 
     try{ 
      InputStream in = new FileInputStream("C:\\Users\\srgf\\Desktop\\s.wma"); 
     AudioStream as = new AudioStream(in); //line 26 
      AudioPlayer.player.start(as); 
     } 
     catch(Exception e){ 
      e.printStackTrace(); 
      System.exit(1); 
     } 
    } 
} 

運行時拋出以下例外的程序:

java.io.IOException: could not create audio stream from input stream 
    at sun.audio.AudioStream.<init>(AudioStream.java:82) 
    at s.run(delplaysound.java:26) 
+0

檢查http://stackoverflow.com/questions/5667454/playing-audio-file-in-java-application?rq=1。我知道它提到了MP3不支持JAVA,但可能與.wma的考慮Windows相同不是很好。 – Austin

回答

0

使用這個庫: http://www.javazoom.net/javalayer/javalayer.html

public void play() { 
     String song = "http://www.ntonyx.com/mp3files/Morning_Flower.mp3"; 
     Player mp3player = null; 
     BufferedInputStream in = null; 
     try { 
      in = new BufferedInputStream(new URL(song).openStream()); 
      mp3player = new Player(in); 
      mp3player.play(); 
     } catch (MalformedURLException ex) { 
     } catch (IOException e) { 
     } catch (JavaLayerException e) { 
     } catch (NullPointerException ex) { 
     } 

} 

希望幫助大家用:-)

+0

上述代碼是否也可以從硬盤播放文件?這不適合我。我將上面的代碼包含在Thread的run()方法中,並在語句mp3player.play()之後包含了sleep(10000);以便音頻完成播放。 – Ranjith

+0

是的,看到這個例子http://thiscouldbebetter.wordpress.com/2011/06/14/playing-an-mp3-from-java-using-jlayer/ –

0
類似的問題

嗯。這可能看起來像廣告了我的東西,但你可以在這裏用我的API:

https://github.com/s4ke/HotSound

播放與此一個很容易的。

替代方案:(由我小的變化)

public static AudioInputStream getSupportedAudioInputStreamFromInputStream(InputStream pInputStream) throws UnsupportedAudioFileException, 
     IOException { 
    AudioInputStream sourceAudioInputStream = AudioSystem 
      .getAudioInputStream(pInputStream); 
    AudioInputStream ret = sourceAudioInputStream; 
    AudioFormat sourceAudioFormat = sourceAudioInputStream.getFormat(); 
    DataLine.Info supportInfo = new DataLine.Info(SourceDataLine.class, 
      sourceAudioFormat, 
      AudioSystem.NOT_SPECIFIED); 
    boolean directSupport = AudioSystem.isLineSupported(supportInfo); 
    if(!directSupport) { 
     float sampleRate = sourceAudioFormat.getSampleRate(); 
     int channels = sourceAudioFormat.getChannels(); 
     AudioFormat newFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, 
       sampleRate, 
       16, 
       channels, 
       channels * 2, 
       sampleRate, 
       false); 
     AudioInputStream convertedAudioInputStream = AudioSystem 
       .getAudioInputStream(newFormat, sourceAudioInputStream); 
     sourceAudioFormat = newFormat; 
     ret = convertedAudioInputStream; 
    } 
    return ret; 
} 

來源爲剪輯例如:使用Java剪輯(預緩衝)

... code ... 
// specify the sound to play 
File soundFile = new File("pathToYouFile"); 
//this does the conversion stuff for you if you have the correct SPIs installed 
AudioInputStream inputStream = 
getSupportedAudioInputStreamFromInputStream(new FileInputStream(soundFile)); 

// load the sound into memory (a Clip) 
DataLine.Info info = new DataLine.Info(Clip.class, inputStream.getFormat()); 
Clip clip = (Clip) AudioSystem.getLine(info); 
clip.open(sound); 

// due to bug in Java Sound, explicitly exit the VM when 
// the sound has stopped. 
clip.addLineListener(new LineListener() { 
    public void update(LineEvent event) { 
    if (event.getType() == LineEvent.Type.STOP) { 
     event.getLine().close(); 
     System.exit(0); 
    } 
    } 
}); 

// play the sound clip 
clip.start(); 
... code ... 

然後,你需要這個方法http://www.java2s.com/Code/Java/Development-Class/AnexampleofloadingandplayingasoundusingaClip.htm

通過將SPI添加到類路徑來添加SPI

爲MP3它們是:

0

使用JavaFX(這是捆綁在一起的JDK)是非常簡單的。 您將需要以下進口:

import javafx.scene.media.Media; 
import javafx.scene.media.MediaPlayer; 
import javafx.util.Duration; 

import java.nio.file.Paths; 

步驟:

初始化的JavaFX:

new JFXPanel(); 

創建Media(聲音):

Media media = new Media(Paths.get(filename).toUri().toString()); 

創建一個MediaPlayer播放聲音:

MediaPlayer player = new MediaPlayer(media); 

並播放Media

player.play(); 

您可以設置開始/與MediaPlayer.setStartTime()MediaPlayer.setStopTime()停止時間以及:

player.setStartTime(new Duration(Duration.ZERO)); // Start at the beginning of the sound file 
player.setStopTime(1000); // Stop one second (1000 milliseconds) into the playback 

或者,你可以停止玩MediaPlayer.stop()

樣本函數來播放音頻:

public static void playAudio(String name, double startMillis, double stopMillis) { 
    Media media = new Media(Paths.get(name).toUri().toString()); 
    MediaPlayer player = new MediaPlayer(media); 

    player.setStartTime(new Duration(startMillis)); 
    player.setStopTime(new Duration(stopMillis)); 
    player.play(); 
} 

更多信息可以在JavaFX的javadoc被發現。

相關問題