如何将 FileDescriptor 与 HTTP URL 结合使用

发布于 2024-09-16 14:58:45 字数 727 浏览 5 评论 0原文

我希望这能够让 Android 的 MediaPlayer 使用身份验证从 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());

Is it right to say that a FileDescriptor can only be used with local file:// URLs而不是普通的 http:// URL?如果是这样,是否有人对如何从需要使用 Android 的 MediaPlayer 进行身份验证的服务器进行流式传输有任何其他想法?

I was hoping this was going to work for getting Android's MediaPlayer to stream from a URL using authentication, but now I'm not so sure. I have no problem getting it to stream from an open server (no authentication) but I don't see any way to tell MediaPlayer to use basic authentication unless perhaps using the FileDescriptor argument works? So I tried this but got the following error:

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

My code looks something like this:

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

Is it correct to say that a FileDescriptor can only be used with local file:// URLs and not normal http:// URLs? If so, does anyone have any other ideas on how to stream from a server that requires authentication using Android's MediaPlayer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-09-23 14:58:45

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);

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

No, that's incorrect, Java takes the Unix philosophy that "everything is a file" and the javadoc reads:

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

However, the MediaPlayer can only open seekable file descriptors with setDataSource(FileDescriptor)

Maybe you can try something like this (untested)

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文