ahodder
ahodder

Reputation: 11439

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?

Upvotes: 4

Views: 886

Answers (3)

Manfred Schmidbartl
Manfred Schmidbartl

Reputation: 1

@zrgiu

I tried to go with this solution, but the MediaPlayer retrieves a FileDescriptor from the URI, so sadly no http URL can be passed like this.

I also found another solution, it suggests to create a local ProxyServer on your device to serve files from the internet, it should be possible to also save the files streamed via the proxy.

Upvotes: 0

zrgiu
zrgiu

Reputation: 6322

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.

Upvotes: 1

Duke
Duke

Reputation: 1731

one way is to first find out where the video is in the server for exmaple in youtube with simple regex like this :

Regex("(?<=&t=)[^&]*").Match(file).Value;

you could retrieve url to the video and then download it like

    public static void Download(string videoID, string newFilePath)
    {
        WebClient wc = new WebClient();
        string file = wc.DownloadString(string.Format("http://www.youtube.com/watch?v={0}", videoID));
        string t = new Regex("(?<=&t=)[^&]*").Match(file).Value;
        wc.DownloadFile(string.Format("http://www.youtube.com/get_video?t={0}=&video_id={1}",t,videoID), newFilePath);

}

it's c# code but you could easily convert it to java.

Upvotes: 0

Related Questions