mobile/android/base/VideoPlayer.java

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

mercurial