2012-08-27 35 views
5

我知道這是一個重複的問題。 檢查原始的一個herehere在java中使用記事本播放.mp3文件

所以我的代碼只是複製粘貼:

import javafx.scene.media.*; 

class Gui { 
    public static void main(String[] args) { 
    try{ 
     Media hit = new Media("skin.mp3"); 
     MediaPlayer mediaPlayer = new MediaPlayer(hit); 
     mediaPlayer.play(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
    } 
} 

這我得到的例外是:

java.lang.IllegalArgumentException: uri.getScheme() == null! 
     at com.sun.media.jfxmedia.locator.Locator.<init>(Locator.java:217) 
     at javafx.scene.media.Media.<init>(Media.java:364) 
     at Gui.main(gui.java:6) 

我編譯&運行它正確地包括jfxrt.jar文件,即在類路徑中

注意:我只是使用記事本而不是任何IDE。

因此,誰能告訴我的IllegalArgumentException

Thankx

的原因

UPDATE:通過使用file://e:/skin.mp3它工作得很好,但與另一個異常留給我:

MediaException: MEDIA_INACCESSIBLE : e 
     at javafx.scene.media.Media.<init>(Unknown Source) 
     at Gui.main(gui.java:6) 

所以如果你能對這個例外加以說明。

順便說一下,我檢查了歌曲,它沒有腐敗,因爲它在vlc中播放得很好。

+1

*「沒有損壞,因爲它在vlc中播放的很好。」* LOL!媒體播放器的功能非常強大,以確保他們幾乎可以播放任何垃圾文件。如果您需要確認文件的有效性,請使用旨在檢查的程序執行此操作。 –

+0

LOL!........... – user1574009

回答

5

從JavaFX的API文檔

  • 所要求的java.net.URI中所提供的URI必須符合RFC-2396。
  • 僅支持HTTP,FILE和JAR URI。

所以,我懷疑從閱讀文檔,你需要提供一個URI路徑。

file://path/to/file/skin.mp3可能會工作。

+2

爲了得到這個URL,你可以執行'new File(「skin.mp3」)。toURI()。toString()' – Thilo

+0

嘿謝謝。 IllegalArgumentException已經通過使用'file:// e:/ skin.mp3'去了,但現在我遇到了另一個問題 'MediaException:MEDIA_INACCESSIBLE:e at javafx.scene.media.Media。 (Unknown Source) at Gui.main(gui.java:6)' – user1574009

+0

JavaFX docs says「發生錯誤:雖然媒體可能存在,但不可訪問」 - 這不是特別有用。從我讀過的要麼你沒有讀取文件的訪問權限或文件被其他程序鎖定...還有很多其他潛在的問題 – MadProgrammer

4

這個問題的代碼有幾個問題。

  1. 該課程需要公開。
  2. JavaFX 2應用程序需要擴展Application類。
  3. JavaFX 2應用程序應該定義一個啓動方法。
  4. 正在創建媒體的定位器應該是完整的URI,如MadProgrammer所述。

即使該問題有一個javafx-2標籤,我不知道它是否爲JavaFX 1.x JavaFX Script(現在是不受支持的編程語言並與JavaFX 2不兼容)編寫。如果是這樣,我建議使用Java編碼,並使用JavaFX 2.x而不是JavaFX Script。

在Windows上,URI的絕對定位符的文件表示形式在文件協議後有三個斜線。例如,以下是有效的:

 
file:///C:/Users/Public/Music/skin.mp3 

出於某種原因,單斜線也將工作(我猜的內部Java將插值對文件或協議說明額外//也許有一些我不在URL規範中理解這意味着你不需要在協議之後//)。檢查文件URI的東西

 
file:/C:/Users/Public/Music/skin.mp3 

一種方法是有效的,詢問的文件URI存在

 
System.out.println("File " + filename + " exists? " + new File(filename).exists()); 

後你知道你的文件URI是有效的,你可以將其轉換爲使用字符串。

file.toURI().toURL().toExternalForm() 

這裏是玩利用MediaPlayer的與錯誤處理一點點JavaFX的一些音頻,使之更容易理解,如果出現錯誤很短的示例程序。

import java.io.File; 
import java.net.MalformedURLException; 
import javafx.application.Application; 
import javafx.scene.Scene; 
import javafx.scene.control.Label; 
import javafx.scene.layout.StackPane; 
import javafx.scene.media.*; 
import javafx.scene.paint.Color; 
import javafx.stage.Stage; 

/** plays an audio in JavaFX 2.x */ 
public class SimpleAudioPlayer extends Application { 
    public static void main(String[] args) { launch(args); } 
    @Override public void start(Stage stage) throws MalformedURLException { 
    final Label status = new Label("Init"); 
    MediaPlayer mediaPlayer = createMediaPlayer(
     "C:/Users/Public/Music/Sample Music/Future Islands - Before the Bridge.mp3", 
     status 
    ); 
    StackPane layout = new StackPane(); 
    layout.getChildren().addAll(status); 
    stage.setScene(new Scene(layout, 600, 100, Color.CORNSILK)); 
    stage.show(); 
    if (mediaPlayer != null) { 
     mediaPlayer.play(); 
    } 
    } 

    /** 
    * creates a media player using a file from the given filename path 
    * and tracks the status of playing the file via the status label 
    */ 
    private MediaPlayer createMediaPlayer(final String filename, final Label status) throws MalformedURLException { 
    File file = new File(filename); 
    if (!file.exists()) { 
     status.setText("File does not exist: " + filename); 
    } 
    final String mediaLocation = file.toURI().toURL().toExternalForm(); 
    Media media = new Media(mediaLocation); 
    MediaPlayer mediaPlayer = new MediaPlayer(media); 
    mediaPlayer.setOnError(new Runnable() { 
     @Override public void run() { 
     status.setText("Error"); 
     } 
    }); 
    mediaPlayer.setOnPlaying(new Runnable() { 
     @Override public void run() { 
     status.setText("Playing: " + mediaLocation); 
     } 
    }); 
    mediaPlayer.setOnEndOfMedia(new Runnable() { 
     @Override public void run() { 
     status.setText("Done"); 
     } 
    }); 
    return mediaPlayer; 
    } 
} 

下面是一個JavaFX 2.x的媒體播放器,其plays all of the mp3 files in a given directory sequentially的另外例子的鏈接。

+0

好,它的工作......謝謝! –