2016-09-29 103 views
2

我發現堆棧溢出的一些可能的答案,但它們很老,看起來他們使用不贊成使用的技術。如何播放給定絕對文件名的mp3文件?

我需要播放給定絕對文件名的mp3文件。

這裏是我試過:

1 JavaFX的

MediaPlayer player = new MediaPlayer(new Media(uriString)); 

我得到java.lang.IllegalStateException: Toolkit not initialized

我可能會找到一種方法來初始化該工具包,但我想知道它是否是首選方式。

2的IntelliJ UIUtil

final InputStream is = new FileInputStream(fileName); 

UIUtil.playSoundFromStream(new Factory<InputStream>() { 
    @Override 
    public InputStream create() { 
     return is; 
    } 
}); 

我越來越Audio format is not yet supported: could not get audio input stream from input file

我做了一些更多的嘗試,但是這是我的一個紀錄。

那的工作對我來說,到目前爲止從殼播放文件的唯一的事情:在Mac上,

Runtime.getRuntime().exec("afplay " + filePath); 

但我寧願一個Java解決方案。有任何想法嗎?

+0

請參閱Java聲音API。工作得很好 – Tokazio

+0

也試過了。不支持MP3:http://stackoverflow.com/questions/5667454/playing-mp3-using-java-sound-api –

回答

2
  • 對於JavaFX的

你可以看看這裏Getting a mp3 file to play using javafx

  • 給你,我最喜歡的部分:

您可以使用JLayer支持.MP3

new Thread(()->{ 
    try { 
     FileInputStream file = new FileInputStream("path ..../audio.mp3"); //initialize the FileInputStream 
     Player player= new Player(file); //initialize the player 
     player.play(); //start the player 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

}).start(); 

注:

注意,我使用單獨Thread原因如果不應用程序將堆棧。

一般來說:

你必須使用外部庫在Java(雖然JavaFX支持播放文件,如MP3播放。MP3但不是所有的格式),只有.WAV

雖然這是enough.All你需要的是一個外部算法玩其他音樂formats.All其它格式的使用.wav最初來到

Java支持,他們進入的算法,然後轟他們成爲.mp3JLayer.jar 前面提到.ogg,.mp3,.whatever

1.As可以導入這個jar到您的項目作爲外部庫。

2. JavaZoom也已經和其他庫來支持.OGG,.speex,.FLAC,.MP3,按照上面的鏈接並下載jlGui項目那裏你可以找到很多的格式庫。

鏈接到 How to play .wav files with java

而且http://alvinalexander.com/java/java-audio-example-java-au-play-sound 不知道計算器,如果仍然用java 8

代碼工作:

import java.io.File; 
    import java.io.IOException; 

    import javax.sound.sampled.AudioFormat; 
    import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.Clip; 
    import javax.sound.sampled.DataLine; 
    import javax.sound.sampled.LineEvent; 
    import javax.sound.sampled.LineListener; 
    import javax.sound.sampled.LineUnavailableException; 
    import javax.sound.sampled.UnsupportedAudioFileException; 


public class AudioPlayerExample1 implements LineListener { 

/** 
* this flag indicates whether the playback completes or not. 
*/ 
boolean playCompleted; 

/** 
* Play a given audio file. 
* @param audioFilePath Path of the audio file. 
*/ 
void play() { 
    File audioFile = new File("C:/Users/Alex.hp/Desktop/Musc/audio.wav"); 

    try { 
     AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); 

     AudioFormat format = audioStream.getFormat(); 

     DataLine.Info info = new DataLine.Info(Clip.class, format); 

     Clip audioClip = (Clip) AudioSystem.getLine(info); 

     audioClip.addLineListener(this); 

     audioClip.open(audioStream); 

     audioClip.start(); 

     while (!playCompleted) { 
      // wait for the playback completes 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException ex) { 
       ex.printStackTrace(); 
      } 
     } 

     audioClip.close(); 

    } catch (UnsupportedAudioFileException ex) { 
     System.out.println("The specified audio file is not supported."); 
     ex.printStackTrace(); 
    } catch (LineUnavailableException ex) { 
     System.out.println("Audio line for playing back is unavailable."); 
     ex.printStackTrace(); 
    } catch (IOException ex) { 
     System.out.println("Error playing the audio file."); 
     ex.printStackTrace(); 
    } 
} 

/** 
* Listens to the START and STOP events of the audio line. 
*/ 
@Override 
public void update(LineEvent event) { 
    LineEvent.Type type = event.getType(); 

    if (type == LineEvent.Type.START) { 
     System.out.println("Playback started."); 

    } else if (type == LineEvent.Type.STOP) { 
     playCompleted = true; 
     System.out.println("Playback completed."); 
    } 
} 

public static void main(String[] args) { 
    AudioPlayerExample1 player = new AudioPlayerExample1(); 
    player.play(); 
} 

}

+0

謝謝你一個很好的答案。看起來像所有的解決方案,除了最後一個依賴於第三方庫。關於最後一個 - 你認爲它可能會播放MP3? –

+0

@Irina Rapoport我編輯了答案,因此在開始時它包含一個關於'JavaFX'的解決方案的鏈接。最後的代碼僅用於'.wav'文件。如果您想使用第三方庫,如「JLayer」 ,它很容易導入.jar到項目中,你在乞討中運行代碼,瞧! – GOXR3PLUS

+1

感謝您的編輯。真正的RE第三方.jar,但是我不會在沒有先閱讀的情況下運行不明顯的第三方代碼,以及誰有這個時間? –