2010-08-30 74 views
7

我希望這是爲了讓Android的MediaPlayer通過使用身份驗證的URL流式傳輸,但現在我不太確定。我沒有問題讓它從一個開放的服務器流(沒有身份驗證),但我沒有看到任何告訴MediaPlayer使用基本身份驗證的方式,除非可能使用FileDescriptor參數工作?所以,我想這一點,但得到了以下錯誤:如何在HTTP URL中使用FileDescriptor

IllegalArgumentException: Expected file scheme in URI http://www.myserver.com/music.mp3 

我的代碼看起來是這樣的:

File f = new File(new URL("http://www.myserver.com/music.mp3").toURI()); 
FileInputStream fis = new FileInputStream(f); 
mediaplayer.SetDataSource(fis.getFD()); 

它是正確的說,一個FileDescriptor只能與當地file:// URL和不正常使用http://網址?如果是這樣,有沒有人有任何其他想法如何從需要使用Android的MediaPlayer認證的服務器進行流式傳輸?

回答

1

Is it correct to say that a FileDescriptor can only be used with local file:// URL

不,那是不正確,Java的需要Unix的理念是 「一切皆文件」 和javadoc reads

handle to the underlying machine-specific structure representing an open file, an open socket, or another source or sink of bytes.

但是,MediaPlayer只能打開可搜索文件,setDataSource(FileDescriptor)

描述

也許你可以嘗試類似的東西(未經測試)

URLConnection connection = new URL(url).openConnection(); 
// Authenticate the way you can 
connection.setRequestProperty("HeaderName", "Header Value"); 

// Save into a file 
File tmp = new File(getCacheDir(), "media"); 
InputStream in = connection.getInputStream(); 
FileOutputStream out = new FileOutputStream(tmp); 
// TODO copy in into out 
mediaPlayer.setDataSource(tmp); 
+0

在這段代碼中,我沒有看到應用程序如何知道tmp與InputStream有什麼關係。 – froggythefrog 2016-11-24 23:56:40

相關問題