2012-06-07 46 views
0

我正在製作一個Java小程序,它必須播放視頻文件。我已經在網上搜索一個代碼,但它給在getParameter如何處理java代碼中的getParameter()?

這裏是代碼中的錯誤...

public void init() { 

     //$ System.out.println("Applet.init() is called"); 
     setLayout(null); 
     setBackground(Color.white); 
     panel = new Panel(); 
     panel.setLayout(null); 
     add(panel); 
     panel.setBounds(0, 0, 320, 240); 

     // input file name from html param 
     String mediaFile = null; 
     // URL for our media file 
     MediaLocator mrl = null; 
     URL url = null; 

     // Get the media filename info. 
     // The applet tag should contain the path to the 
     // source media file, relative to the html page. 

     // Error here: Invalid media file parameter 

     if ((mediaFile = getParameter("C:\\Users\\asim\\Documents\\JCreator LE\\MyProjects\\SimplePlayerApplet\\src\\Movie.avi")) == null) 
      Fatal("Invalid media file parameter"); 

     try { 
      url = new URL(getDocumentBase(), mediaFile); 
      mediaFile = url.toExternalForm(); 
     } catch (MalformedURLException mue) { 
     } 

這裏是鏈接到整個代碼:

http://docs.oracle.com/javase/1.4.2/docs/guide/plugin/developer_guide/SimplePlayerApplet.java.html

+1

根據已經提供的「答案」,您是否可以更新您的代碼和HTML,然後將它們粘貼到上面,包括任何可能仍然存在的錯誤。 – radimpe

回答

0

這裏有幾個想法:

  1. 你是usin g駐留在本地文件系統中的文件。默認情況下,Applets無權訪問文件系統。本教程中的方法有一個註釋。它明確指出該文件應該與html頁面相關並駐留在服務器上。在那裏仔細閱讀評論。

  2. 該示例使用Applet(它們提供的類擴展了Applet) - 它是一種舊方法,現在已被棄用。基本上它使用的是AWT而不是較新的Swing。所以你應該尋找一個使用JApplet的例子。

希望這會有所幫助。

+0

我也嘗試了一個網址,但它不工作。 –

1

你是如何「調用」你的applet?

看起來你試圖將「參數」指定爲位於本地文件系統上的東西,但是你正在構建一個「小程序」,所以你應該真的通過HTML調用它,然後像這樣傳遞參數:

<applet code=SimplePlayerApplet.class width=320 height=300> 
<param name="file" value="sun.avi"> 
</applet> 

,因此您的通話getParameter仍然應該是"file"。就像你之前使用的代碼一樣。

0

您應該將參數放在HTMLpart中,這會使applet獨立於內容。您將參數名稱與其值相混淆。

<HTML> <BODY> 
<APPLET CODE="SimplePlayerApplet.class" WIDTH=320 HEIGHT=240> 
<PARAM NAME="filename" VALUE="C:\\Users\\asim\\Documents\\JCreator LE\\MyProjects\\SimplePlayerApplet\\src\\Movie.avi"> 

</APPLET> 
</BODY> </HTML> 


if ((mediaFile = getParameter("filename")) == null) 
    ... 

爲了訪問文件系統,您需要簽署小程序。

行:

URL(getDocumentBase(), mediaFile); 

試圖打開從文檔基的媒體文件相對。這意味着相對於嵌入此applet的文檔的URL。 查看更多Applet

因此最簡單的方法得到它運行將是你的影片.avi複製在你的HTML文件所在的文件夾,並使用

URL(getDocumentBase(), "Movie.avi"); 

之後,你應該讓配置通過提供文件名作爲參數。

+0

::它不是這樣工作的。同樣的錯誤:-( –

+0

@azeemAkram我更新了我的答案 – stacker