Create mediaplayer with inputstream in android
Asked Answered
T

6

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 ?

Tetrafluoroethylene answered 11/3, 2011 at 12:3 Comment(1)
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.Zoospore
I
1

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

Immersion answered 11/3, 2011 at 12:21 Comment(3)
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...Trusting
@Trusting You could use a temporary file, or you could use a different class that's more low-level, "AudioTrack" : https://mcmap.net/q/1175444/-audiotrack-in-mode_stream-has-no-sound/878126Moravian
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 thoughMoravian
K
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.

Kai answered 16/10, 2012 at 22:15 Comment(1)
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.Nusku
I
1

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

Immersion answered 11/3, 2011 at 12:21 Comment(3)
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...Trusting
@Trusting You could use a temporary file, or you could use a different class that's more low-level, "AudioTrack" : https://mcmap.net/q/1175444/-audiotrack-in-mode_stream-has-no-sound/878126Moravian
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 thoughMoravian
S
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.

Sweepings answered 29/6, 2011 at 15:15 Comment(0)
E
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?

Emlynne answered 11/3, 2011 at 12:22 Comment(0)
T
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)

Tetrafluoroethylene answered 13/3, 2011 at 9:40 Comment(0)
T
0

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

Tomblin answered 13/4, 2022 at 22:44 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.