michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko; michael@0: michael@0: import android.app.Activity; michael@0: import android.content.Intent; michael@0: import android.net.Uri; michael@0: import android.os.Bundle; michael@0: import android.util.Log; michael@0: import android.widget.MediaController; michael@0: import android.widget.VideoView; michael@0: michael@0: import java.io.BufferedReader; michael@0: import java.io.InputStreamReader; michael@0: import java.net.URL; michael@0: import java.net.URLConnection; michael@0: michael@0: public final class VideoPlayer extends Activity { michael@0: public static final String VIDEO_ACTION = "org.mozilla.gecko.PLAY_VIDEO"; michael@0: michael@0: private VideoView mVideoView; michael@0: michael@0: /** Called when the activity is first created. */ michael@0: @Override michael@0: public void onCreate(Bundle savedInstanceState) { michael@0: super.onCreate(savedInstanceState); michael@0: setContentView(R.layout.videoplayer); michael@0: mVideoView = (VideoView) findViewById(R.id.VideoView); michael@0: MediaController mediaController = new MediaController(this); michael@0: mediaController.setAnchorView(mVideoView); michael@0: Intent intent = getIntent(); michael@0: final Uri data = intent.getData(); michael@0: if (data == null) { michael@0: return; michael@0: } michael@0: michael@0: String spec = null; michael@0: if ("vnd.youtube".equals(data.getScheme())) { michael@0: String ssp = data.getSchemeSpecificPart(); michael@0: int paramIndex = ssp.indexOf('?'); michael@0: String id; michael@0: if (paramIndex == -1) { michael@0: id = ssp; michael@0: } else { michael@0: id = ssp.substring(0, paramIndex); michael@0: } michael@0: spec = getSpecFromYouTubeVideoID(id); michael@0: } michael@0: michael@0: if (spec == null) { michael@0: return; michael@0: } michael@0: michael@0: final Uri video = Uri.parse(spec); michael@0: mVideoView.setMediaController(mediaController); michael@0: mVideoView.setVideoURI(video); michael@0: mVideoView.start(); michael@0: } michael@0: michael@0: private String getSpecFromYouTubeVideoID(String id) { michael@0: String spec = null; michael@0: try { michael@0: String infoUri = "http://www.youtube.com/get_video_info?&video_id=" + id; michael@0: URL infoUrl = new URL(infoUri); michael@0: URLConnection urlConnection = infoUrl.openConnection(); michael@0: BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream())); michael@0: try { michael@0: StringBuilder sb = new StringBuilder(); michael@0: String line; michael@0: while ((line = br.readLine()) != null) michael@0: sb.append(line); michael@0: android.net.Uri fakeUri = android.net.Uri.parse("fake:/fake?" + sb); michael@0: String streamMap = fakeUri.getQueryParameter("url_encoded_fmt_stream_map"); michael@0: if (streamMap == null) michael@0: return null; michael@0: String[] streams = streamMap.split(","); michael@0: for (int i = 0; i < streams.length; i++) { michael@0: fakeUri = android.net.Uri.parse("fake:/fake?" + streams[i]); michael@0: String url = fakeUri.getQueryParameter("url"); michael@0: String type = fakeUri.getQueryParameter("type"); michael@0: if (type != null && url != null && michael@0: (type.startsWith("video/mp4") || type.startsWith("video/webm"))) { michael@0: spec = url; michael@0: } michael@0: } michael@0: } finally { michael@0: br.close(); michael@0: } michael@0: } catch (Exception e) { michael@0: Log.e("VideoPlayer", "exception", e); michael@0: } michael@0: return spec; michael@0: } michael@0: }