Adem
Adem

Reputation: 9429

Create mediaplayer with inputstream in android

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 ?

Upvotes: 4

Views: 11547

Answers (6)

steven smith
steven smith

Reputation: 1577

I think you can get the file descriptor from your input stream and feed that to your setDataSource.

Upvotes: 0

Pick your poison
Pick your poison

Reputation: 51

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.

Upvotes: 5

Capndan
Capndan

Reputation: 31

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.

Upvotes: 1

Adem
Adem

Reputation: 9429

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)

Upvotes: 0

devunwired
devunwired

Reputation: 63293

You are correct, MediaPlayer will not take a stream parameter directly as its data source. The four overloaded versions of setDataSource() allow the data to come from a file (using the file path OR a FileDescriptor) 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?

Upvotes: 0

Samuh
Samuh

Reputation: 36484

One approach can be to write your stream to a File and then give it to the MediaPlayer for playback.

Upvotes: 1

Related Questions