mobile/android/base/VideoPlayer.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko;
michael@0 7
michael@0 8 import android.app.Activity;
michael@0 9 import android.content.Intent;
michael@0 10 import android.net.Uri;
michael@0 11 import android.os.Bundle;
michael@0 12 import android.util.Log;
michael@0 13 import android.widget.MediaController;
michael@0 14 import android.widget.VideoView;
michael@0 15
michael@0 16 import java.io.BufferedReader;
michael@0 17 import java.io.InputStreamReader;
michael@0 18 import java.net.URL;
michael@0 19 import java.net.URLConnection;
michael@0 20
michael@0 21 public final class VideoPlayer extends Activity {
michael@0 22 public static final String VIDEO_ACTION = "org.mozilla.gecko.PLAY_VIDEO";
michael@0 23
michael@0 24 private VideoView mVideoView;
michael@0 25
michael@0 26 /** Called when the activity is first created. */
michael@0 27 @Override
michael@0 28 public void onCreate(Bundle savedInstanceState) {
michael@0 29 super.onCreate(savedInstanceState);
michael@0 30 setContentView(R.layout.videoplayer);
michael@0 31 mVideoView = (VideoView) findViewById(R.id.VideoView);
michael@0 32 MediaController mediaController = new MediaController(this);
michael@0 33 mediaController.setAnchorView(mVideoView);
michael@0 34 Intent intent = getIntent();
michael@0 35 final Uri data = intent.getData();
michael@0 36 if (data == null) {
michael@0 37 return;
michael@0 38 }
michael@0 39
michael@0 40 String spec = null;
michael@0 41 if ("vnd.youtube".equals(data.getScheme())) {
michael@0 42 String ssp = data.getSchemeSpecificPart();
michael@0 43 int paramIndex = ssp.indexOf('?');
michael@0 44 String id;
michael@0 45 if (paramIndex == -1) {
michael@0 46 id = ssp;
michael@0 47 } else {
michael@0 48 id = ssp.substring(0, paramIndex);
michael@0 49 }
michael@0 50 spec = getSpecFromYouTubeVideoID(id);
michael@0 51 }
michael@0 52
michael@0 53 if (spec == null) {
michael@0 54 return;
michael@0 55 }
michael@0 56
michael@0 57 final Uri video = Uri.parse(spec);
michael@0 58 mVideoView.setMediaController(mediaController);
michael@0 59 mVideoView.setVideoURI(video);
michael@0 60 mVideoView.start();
michael@0 61 }
michael@0 62
michael@0 63 private String getSpecFromYouTubeVideoID(String id) {
michael@0 64 String spec = null;
michael@0 65 try {
michael@0 66 String infoUri = "http://www.youtube.com/get_video_info?&video_id=" + id;
michael@0 67 URL infoUrl = new URL(infoUri);
michael@0 68 URLConnection urlConnection = infoUrl.openConnection();
michael@0 69 BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
michael@0 70 try {
michael@0 71 StringBuilder sb = new StringBuilder();
michael@0 72 String line;
michael@0 73 while ((line = br.readLine()) != null)
michael@0 74 sb.append(line);
michael@0 75 android.net.Uri fakeUri = android.net.Uri.parse("fake:/fake?" + sb);
michael@0 76 String streamMap = fakeUri.getQueryParameter("url_encoded_fmt_stream_map");
michael@0 77 if (streamMap == null)
michael@0 78 return null;
michael@0 79 String[] streams = streamMap.split(",");
michael@0 80 for (int i = 0; i < streams.length; i++) {
michael@0 81 fakeUri = android.net.Uri.parse("fake:/fake?" + streams[i]);
michael@0 82 String url = fakeUri.getQueryParameter("url");
michael@0 83 String type = fakeUri.getQueryParameter("type");
michael@0 84 if (type != null && url != null &&
michael@0 85 (type.startsWith("video/mp4") || type.startsWith("video/webm"))) {
michael@0 86 spec = url;
michael@0 87 }
michael@0 88 }
michael@0 89 } finally {
michael@0 90 br.close();
michael@0 91 }
michael@0 92 } catch (Exception e) {
michael@0 93 Log.e("VideoPlayer", "exception", e);
michael@0 94 }
michael@0 95 return spec;
michael@0 96 }
michael@0 97 }

mercurial