4

i'm using usb device connected to my android device

this device send me a buffer that contain video frame it dose that continuously .

when i receive the buffer i should put specific header into that and write it to sd card as m4v video file .

then i should play it in media player so far it works fine but i should show the video live . as soon as usb device send me the buffer i should process it and play it so i can show the video .

my question is how to play a video file from byte buffer without store it in sd card ?

Amin.Frg
  • 59
  • 1
  • 6

2 Answers2

3

I know this is awhile since the question was asked, but I am in the same situation where I have a video transport stream coming through a USB connection.

It looks like you need to create a data source that extends a MediaDataSource and provides the readAt() and getSize() methods.

My understanding is Android's MediaPlayer, when you supply a URL/URI as a data source (through setDataSource(), handles all the networking to download the data or retrieve it from storage, and has its own MediaDataSource the player users.

My plan is to create a service that gets the transport stream from the USB device, and constantly update a custom MediaDataSource that I supply to the MediaPlayer.

Here is the only thing I could find on the subject, which was a test written for testing using a MediaDataSource with the MediaPlayer. They read a video from a file into a byte array, and then use that array for the readAt() and getSize() methods.

Only other resources I could find are people supplying strings to the MediaPlayer, but nothing that involves doing their own networking. Also, I'm curious how a constant stream of video is different that just a fixed sized video.

Edit: I wrote a blog post about MediaDataSource and how to get started with it. Here is the link.

jacks205
  • 545
  • 8
  • 19
  • 2
    good job ! I need something like this but MediaDataSource was introduced in API level 23(M) which I don't know if 5% of Android users have this yet. Do you have a backwards compatible solution ? I am thinking to make a proxy class or some sort of a cache that streams from the server and then somehow I pass the result to the media player. Yet I don't know how I should make this because mediaplayer only takes URI/path and not bytes/streams – Tudor Dec 09 '15 at 12:27
  • @PopTudor I think pre-API 23 you may have to look into [Exoplayer](http://developer.android.com/guide/topics/media/exoplayer.html) and use that if you want to customize the data source. I've never messed with it, but you can [override the Media Extractor](https://www.youtube.com/watch?v=6VjF638VObA) to handle networking and buffering to try to obtain a backwards compatible solution. – jacks205 Dec 09 '15 at 18:01
  • 1
    Thank you. I think that I will go with Exoplayer since it offers so much in comparison with Mediaplayer and on API < 16 I'll just use media player since those are phones that are pretty much going to be extinct in the next 1-2 years so not much is lost there. – Tudor Dec 09 '15 at 21:14
0

I found jacks205 doesn't mention abstract method close() in Closeable. We also need to write the close(), or the compiling will report the following errors: YourMediaDataSource is not abstract and does not override abstract method close() in Closeable

Clock ZHONG
  • 875
  • 9
  • 23