开发者

How do I use FileDescriptor with HTTP URLs

开发者 https://www.devze.com 2023-01-13 20:45 出处:网络
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 (

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());
FileInputStrea开发者_开发百科m 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?


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

精彩评论

暂无评论...
验证码 换一张
取 消