在android中使用输入流创建媒体播放器
如何使用输入流创建媒体播放器实例?
我只看到 4 个 setDataSource 函数。并且没有获取输入流的函数? 是否必须使用 FileDescriptor
来媒体播放器?看起来很基础。但是,我找不到办法。在j2me中,有一个函数Manager.createPlayer(InputStream)
。您可以使用 inputstream 创建媒体播放器。有没有办法创建像 j2me 这样的媒体播放器?
How to I create mediaplayer instance with inputstream?
I see only 4 function for setDataSource. And there is no function getting inputstream ?
is it a must to use FileDescriptor
to mediaplayer ? It seems so basic. but, I couldnot find a way. In j2me, there is a function that Manager.createPlayer(InputStream)
. And you can use inputstream to create a media player. Is there a way to create a mediaplayer like j2me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如何在接收端(手机上的线程)创建一个 HTTP 服务器,将任何 HTTP 请求上的 InputStream 中的数据输出到 Socket 的 OutputStream 并为 MediPlayer 提供 URI http 127.0.0.1 :端口?这是丑陋的(但它应该可以工作)
,也可以从 android 中的 InputStream 播放 PCM 未压缩的音频。古尔它。如果您可以使用 JLayer 或其他软件在软件中进行解码并将其作为 PCM 输出到音频接口,也应该可以实现这一点,但无需硬件加速。
我想选择你的毒药吧。我选择了B选项。
How about making a HTTP server on the recieveing side (a thread on the phone) that outputs the data from the InputStream on any HTTP request to the OutputStream of the Socket and provide MediPlayer with URI http 127.0.0.1 : a port? THAT IS UGLY (but it should work)
it is also possible to play PCM uncompressed audio from an InputStream in android. goole it. if you can do decoding in software with JLayer or something and output it as PCMto the audio interface that should do the trick too but without hardware acceleration.
Pick your poison I guess. I chose option B.
一种方法是将流写入
File
,然后将其交给MediaPlayer
进行播放。One approach can be to write your stream to a
File
and then give it to theMediaPlayer
for playback.我现在正在研究这个(为了将加密的电影文件发送到 MediaPlayer)。我假设 FileInputStream.getFD() 是这个问题的解决方案是错误的吗?如果您可以从 FileInputStream 获取 FileDescriptor(除非您特别需要 InputStream 而不是 FileInputStream),那么它似乎可以直接传递到 MediaPlayer。
编辑:好的,所以 FileInputStream 是使用路径或 URL 创建的,使其毫无用处。
I'm looking into this right now (in order to send encrypted movie files to the MediaPlayer). Am I wrong in assuming the FileInputStream.getFD() is the solution to this? If you can get a FileDescriptor from a FileInputStream (unless you specifically need InputStream and not FileInputStream) then it seems like that can be passed right on to the MediaPlayer.
EDIT: Okay, so FileInputStream is created using a path or URL, making it useless.
你是对的,
MediaPlayer
不会直接将流参数作为其数据源。setDataSource()
的四个重载版本允许数据来自文件(使用文件路径或FileDescriptor
)或 Uri(Web 位置或本地内容:/ / uri)。此外,静态
create()
方法可以从原始资源 (R.raw.something) 或与上面相同样式的 Uri 创建媒体播放器。除了文件位置或网络之外,您从哪里获取音频/视频?
You are correct,
MediaPlayer
will not take a stream parameter directly as its data source. The four overloaded versions ofsetDataSource()
allow the data to come from a file (using the file path OR aFileDescriptor
) or a Uri (web location or local content:// uri).In addition, to static
create()
method can create a media player from a raw resource (R.raw.something) or the same style Uri as above.Where are you sourcing the audio/video from other than either a file location or the web?
现在,我将
InputStream
复制到临时文件并将其提供给媒体播放器。但是,你知道,这不是一个好的解决方案。因为,这是一个缓慢的解决方案。如果数据很大,玩家必须等待太多。我的来源正在改变。例如,它来自数据库。如果我找不到解决方案,我会将代码转换为获取文件。但是,现在我设计系统来获取输入流并播放它。比如,
MediaPlayer.createPlayer(InputStream,String)的j2me函数
for now, I copy the
InputStream
to a temp file and give it to mediaplayer. but, you know, it s not good solution. because, it s slow solution. if data is big, player must wait too much.my source is changing. for example, it s come from database. if I will not find solution, I convert my code to getting file. but, for now I desing the system to getting inputstream and play it. like, j2me function of
MediaPlayer.createPlayer(InputStream,String)
我认为您可以从输入流中获取文件描述符并将其提供给 setDataSource。
I think you can get the file descriptor from your input stream and feed that to your setDataSource.