Search code examples
androidmedia-player

Can one retrieve data from a MediaPlayer's stream?


If I were to stream some sort of media to a MediaPlayer, is there any way I could copy it before/as/after it is played? For instance, if I were to stream a YouTube clip, is it possible to save that clip as it is being played?

Edit:
(Ocelot's answer made me realise how localised this question is).

What I am looking to do is copy the stream of a MediaPlayer already in progress (be it youtube or music stream). I want to be able to be notified when a new stream starts and ends. So far the only thing I found (for the latter) that is even remotely close it the broadcast string ACTION_AUDIO_BECOMING_NOISY but that doesn't really do anything for what I need. I there any way to do this?


Solution

  • I haven't tested this, and it looks like quite a bit of work, but here is what I would try:

    1. Create a subclass of Socket. In this class, you can handle all byte reads, and save the stream locally or do whatever you want with it

    2. Create your own content provider, which you can use to pass URIs to your media player, in your own format. Example: mystream://youtube.com/watch?v=3Rhy37u

    3. In your content provider, override the openFile method and in it, open your own socket, and create a ParcelFileDescriptor with it.

    Now, simply passing the new format url to your mediaplayer should make all streams go through your Socket, where you can save your data.