8

我希望这可以让 AndroidMediaPlayer使用身份验证从 URL 流式传输,但现在我不太确定。我没有问题让它从开放服务器流式传输(无身份验证),但我看不到任何方法可以告诉MediaPlayer使用基本身份验证,除非使用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());

说 aFileDescriptor只能用于本地file://URL 而不能用于普通http://URL 是否正确?如果是这样,是否有人对如何从需要使用 Android 进行身份验证的服务器进行流式传输有任何其他想法MediaPlayer

4

1 回答 1

0

说 FileDescriptor 只能与本地 file:// URL 一起使用是否正确

不,这是不正确的,Java 采用“一切都是文件”的 Unix 哲学,javadoc 读取

表示打开的文件、打开的套接字或另一个字节源或接收器的底层机器特定结构的句柄。

但是,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);
于 2014-08-22T18:08:15.773 回答