1 /*
2  * Copyright (C) 2009 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 
17 package com.example.android.apis.media;
18 
19 import android.content.ClipData;
20 import android.media.MediaPlayer;
21 import android.view.DragEvent;
22 import android.view.View;
23 import com.example.android.apis.R;
24 import android.app.Activity;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.widget.MediaController;
28 import android.widget.VideoView;
29 
30 public class VideoViewDemo extends Activity {
31 
32     private VideoView mVideoView;
33 
34     @Override
onCreate(Bundle icicle)35     public void onCreate(Bundle icicle) {
36         super.onCreate(icicle);
37         setContentView(R.layout.videoview);
38         mVideoView = (VideoView) findViewById(R.id.surface_view);
39 
40         initPlayer(Uri.parse("android.resource://" + getPackageName() +
41                 "/" + R.raw.videoviewdemo));
42 
43         mVideoView.setOnDragListener(mDragListener);
44     }
45 
initPlayer(Uri uri)46     private void initPlayer(Uri uri) {
47         mVideoView.setVideoURI(uri);
48         mVideoView.setMediaController(new MediaController(this));
49         mVideoView.requestFocus();
50     }
51 
52     private View.OnDragListener mDragListener = new View.OnDragListener() {
53         @Override
54         public boolean onDrag(View v, DragEvent event) {
55             if (event.getAction() != DragEvent.ACTION_DROP) {
56                 return true;
57             }
58             ClipData clipData = event.getClipData();
59             if (clipData.getItemCount() != 1) {
60                 return false;
61             }
62             ClipData.Item item = clipData.getItemAt(0);
63             Uri uri = item.getUri();
64             if (uri == null) {
65                 return false;
66             }
67             if (requestDragAndDropPermissions(event) == null) {
68                 return false;
69             }
70             initPlayer(uri);
71             mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
72                 public void onPrepared(MediaPlayer mediaPlayer) {
73                     mVideoView.start();
74                 }
75             });
76             return true;
77         }
78     };
79 }
80