blob: 58e80df5e8803a02a7f4c75e6f7019880dce2b5d [file] [log] [blame]
The Android Open Source Project0eaec582008-12-17 18:05:54 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.camera;
18
19
20import android.app.Activity;
21import android.content.Intent;
22import android.content.pm.ActivityInfo;
23import android.media.MediaPlayer;
24import android.net.Uri;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.Message;
28import android.provider.MediaStore;
29import android.view.View;
30import android.widget.MediaController;
31import android.widget.VideoView;
32
33public class MovieView extends Activity implements MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener
34{
35 private static final String TAG = "MovieView";
36 // Copied from MediaPlaybackService in the Music Player app. Should be public, but isn't.
37 private static final String SERVICECMD = "com.android.music.musicservicecommand";
38 private static final String CMDNAME = "command";
39 private static final String CMDPAUSE = "pause";
40
41 private VideoView mVideoView;
42 private View mProgressView;
43 private boolean mFinishOnCompletion;
44 public MovieView()
45 {
46 }
47
48 @Override
49 public void onCreate(Bundle icicle)
50 {
51 super.onCreate(icicle);
52
53 setContentView(R.layout.movie_view);
54
55 mVideoView = (VideoView) findViewById(R.id.surface_view);
56 mProgressView = findViewById(R.id.progress_indicator);
57 Intent intent = getIntent();
58 if (intent.hasExtra(MediaStore.EXTRA_SCREEN_ORIENTATION)) {
59 int orientation = intent.getIntExtra(MediaStore.EXTRA_SCREEN_ORIENTATION,
60 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
61 if (orientation != getRequestedOrientation()) {
62 setRequestedOrientation(orientation);
63 }
64 }
65 mFinishOnCompletion = intent.getBooleanExtra(MediaStore.EXTRA_FINISH_ON_COMPLETION, true);
66 Uri uri = intent.getData();
67
68 // For streams that we expect to be slow to start up, show a
69 // progress spinner until playback starts.
70 String scheme = uri.getScheme();
71 if ("http".equalsIgnoreCase(scheme) ||
72 "rtsp".equalsIgnoreCase(scheme)) {
73 mHandler.postDelayed(mPlayingChecker, 250);
74 } else {
75 mProgressView.setVisibility(View.GONE);
76 }
77
78 mVideoView.setOnErrorListener(this);
79 mVideoView.setOnCompletionListener(this);
80 mVideoView.setVideoURI(uri);
81 mVideoView.setMediaController(new MediaController(this));
82 mVideoView.requestFocus(); // make the video view handle keys for seeking and pausing
83
84 Intent i = new Intent(SERVICECMD);
85 i.putExtra(CMDNAME, CMDPAUSE);
86 sendBroadcast(i);
87
88 mVideoView.start();
89 }
90
91 @Override
92 public void onPause() {
93 mHandler.removeCallbacksAndMessages(null);
94 super.onPause();
95 }
96
97 Handler mHandler = new Handler() {
98 @Override
99 public void handleMessage(Message msg) {
100 }
101 };
102
103 Runnable mPlayingChecker = new Runnable() {
104 public void run() {
105 if (mVideoView.isPlaying()) {
106 mProgressView.setVisibility(View.GONE);
107 } else {
108 mHandler.postDelayed(mPlayingChecker, 250);
109 }
110 }
111 };
112
113 public boolean onError(MediaPlayer player, int arg1, int arg2) {
114 mHandler.removeCallbacksAndMessages(null);
115 mProgressView.setVisibility(View.GONE);
116 return false;
117 }
118
119 public void onCompletion(MediaPlayer mp) {
120 if (mFinishOnCompletion) {
121 finish();
122 }
123 }
124}