gpt4 book ai didi

android - 如何将 FileDescriptor 与 HTTP URL 一起使用

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:13:57 26 4
gpt4 key购买 nike

我希望这能够让 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());

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

最佳答案

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)seekable 文件描述符。

也许你可以尝试这样的事情(未经测试)

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

关于android - 如何将 FileDescriptor 与 HTTP URL 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3597450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com