【问题标题】:How do I use FileDescriptor with HTTP URLs如何将 FileDescriptor 与 HTTP URL 一起使用
【发布时间】:2011-04-05 13:50:03
【问题描述】:

我希望这可以让 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:// URL 是否正确?如果是这样,是否有人对如何从需要使用 Android 的 MediaPlayer 进行身份验证的服务器进行流式传输有任何其他想法?

【问题讨论】:

    标签: android file-descriptor file-uri


    【解决方案1】:

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

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

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

    然而,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);
    

    【讨论】:

    • 在这段代码中,我没有看到应用程序如何知道 tmp 与 InputStream 中的任何关系。
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 2017-10-22
    • 2015-11-18
    • 1970-01-01
    • 2012-07-28
    • 2015-05-02
    • 2013-05-27
    相关资源
    最近更新 更多