4

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 ?

Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
Adem
  • 9,402
  • 9
  • 43
  • 58
  • I'd also like to do this because my audio files are encrypted. My workaround is to decrypt to a file in Resources.getCacheDir() and then pass the FileDescriptor of that to MediaPlayer. Not a great solution because it is a little slow. Also, the Motorola Atrix seems to have problems with this. – Mark Jun 08 '11 at 09:42

6 Answers6

5

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.

  • On this note you need to make sure that your local server supports the http `"range:` header correctly. I've just hit this bug via other means. – Philip Couling May 03 '15 at 07:20
1

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

Samuh
  • 36,316
  • 26
  • 109
  • 116
  • 1
    Is this seriously the best solution? Write the stream to a file and read it? You can't be serious that there's no pure-memory solution... – Cruncher Sep 18 '13 at 18:56
  • @Cruncher You could use a temporary file, or you could use a different class that's more low-level, "AudioTrack" : http://stackoverflow.com/q/31354338/878126 – android developer Apr 11 '16 at 14:22
  • I've noticed that MediaPlayer has the option to set the dataSource to be MediaDataSource , but not only I'm not sure how to use it, it also requires API 23 and above. It might help though – android developer Apr 12 '16 at 07:29
1

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.

Capndan
  • 31
  • 3
0

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?

devunwired
  • 62,780
  • 12
  • 127
  • 139
0

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)

Adem
  • 9,402
  • 9
  • 43
  • 58
0

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

steven smith
  • 1,519
  • 15
  • 31