mobile/android/base/VideoPlayer.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/VideoPlayer.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
     1.5 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +package org.mozilla.gecko;
    1.10 +
    1.11 +import android.app.Activity;
    1.12 +import android.content.Intent;
    1.13 +import android.net.Uri;
    1.14 +import android.os.Bundle;
    1.15 +import android.util.Log;
    1.16 +import android.widget.MediaController;
    1.17 +import android.widget.VideoView;
    1.18 +
    1.19 +import java.io.BufferedReader;
    1.20 +import java.io.InputStreamReader;
    1.21 +import java.net.URL;
    1.22 +import java.net.URLConnection;
    1.23 +
    1.24 +public final class VideoPlayer extends Activity {
    1.25 +    public static final String VIDEO_ACTION = "org.mozilla.gecko.PLAY_VIDEO";
    1.26 +
    1.27 +    private VideoView mVideoView;
    1.28 +
    1.29 +    /** Called when the activity is first created. */
    1.30 +    @Override
    1.31 +    public void onCreate(Bundle savedInstanceState) {
    1.32 +        super.onCreate(savedInstanceState);
    1.33 +        setContentView(R.layout.videoplayer);
    1.34 +        mVideoView = (VideoView) findViewById(R.id.VideoView);
    1.35 +        MediaController mediaController = new MediaController(this);
    1.36 +        mediaController.setAnchorView(mVideoView);
    1.37 +        Intent intent = getIntent();
    1.38 +        final Uri data = intent.getData();
    1.39 +        if (data == null) {
    1.40 +            return;
    1.41 +        }
    1.42 +
    1.43 +        String spec = null;
    1.44 +        if ("vnd.youtube".equals(data.getScheme())) {
    1.45 +            String ssp = data.getSchemeSpecificPart();
    1.46 +            int paramIndex = ssp.indexOf('?');
    1.47 +            String id;
    1.48 +            if (paramIndex == -1) {
    1.49 +                id = ssp;
    1.50 +            } else {
    1.51 +                id = ssp.substring(0, paramIndex);
    1.52 +            }
    1.53 +            spec = getSpecFromYouTubeVideoID(id);
    1.54 +        }
    1.55 +
    1.56 +        if (spec == null) {
    1.57 +            return;
    1.58 +        }
    1.59 +
    1.60 +        final Uri video = Uri.parse(spec);
    1.61 +        mVideoView.setMediaController(mediaController);
    1.62 +        mVideoView.setVideoURI(video);
    1.63 +        mVideoView.start();
    1.64 +    }
    1.65 +
    1.66 +    private String getSpecFromYouTubeVideoID(String id) {
    1.67 +        String spec = null;
    1.68 +        try {
    1.69 +            String infoUri = "http://www.youtube.com/get_video_info?&video_id=" + id;
    1.70 +            URL infoUrl = new URL(infoUri);
    1.71 +            URLConnection urlConnection = infoUrl.openConnection();
    1.72 +            BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
    1.73 +            try {
    1.74 +                StringBuilder sb = new StringBuilder();
    1.75 +                String line;
    1.76 +                while ((line = br.readLine()) != null)
    1.77 +                    sb.append(line);
    1.78 +                android.net.Uri fakeUri = android.net.Uri.parse("fake:/fake?" + sb);
    1.79 +                String streamMap = fakeUri.getQueryParameter("url_encoded_fmt_stream_map");
    1.80 +                if (streamMap == null)
    1.81 +                    return null;
    1.82 +                String[] streams = streamMap.split(",");
    1.83 +                for (int i = 0; i < streams.length; i++) {
    1.84 +                    fakeUri = android.net.Uri.parse("fake:/fake?" + streams[i]);
    1.85 +                    String url = fakeUri.getQueryParameter("url");
    1.86 +                    String type = fakeUri.getQueryParameter("type");
    1.87 +                    if (type != null && url != null &&
    1.88 +                        (type.startsWith("video/mp4") || type.startsWith("video/webm"))) {
    1.89 +                        spec = url;
    1.90 +                    }
    1.91 +                }
    1.92 +            } finally {
    1.93 +                br.close();
    1.94 +            }
    1.95 +        } catch (Exception e) {
    1.96 +            Log.e("VideoPlayer", "exception", e);
    1.97 +        }
    1.98 +        return spec;
    1.99 +    }
   1.100 +}

mercurial