Эх сурвалжийг харах

Basic video preview in a separate actitivy

David A. Velasco 12 жил өмнө
parent
commit
586793a261

+ 6 - 0
AndroidManifest.xml

@@ -85,6 +85,12 @@
         </activity>
         <activity android:name=".ui.activity.PreferencesNewSessionewSession" >
         </activity>
+        
+		<activity	android:name=".ui.activity.VideoActivity"
+					android:label="@string/app_name"
+					android:screenOrientation="landscape"
+					android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
+		</activity>        
 
         <service
             android:name=".authenticator.AccountAuthenticatorService"

+ 11 - 0
res/layout/video_layout.xml

@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="utf-8"?>
+<FrameLayout	xmlns:android="http://schemas.android.com/apk/res/android"
+				android:layout_width="fill_parent"
+				android:layout_height="fill_parent" >
+
+	<VideoView  android:id="@+id/videoPlayer"
+				android:layout_width="wrap_content"
+				android:layout_height="wrap_content"  
+				android:layout_gravity="center" />
+	
+</FrameLayout>

+ 5 - 1
src/com/owncloud/android/datamodel/OCFile.java

@@ -455,7 +455,11 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
     }
 
     public boolean isAudio() {
-        return (mMimeType.startsWith("audio/"));
+        return (mMimeType != null && mMimeType.startsWith("audio/"));
+    }
+
+    public boolean isVideo() {
+        return (mMimeType != null && mMimeType.startsWith("video/"));
     }
 
 }

+ 61 - 0
src/com/owncloud/android/ui/activity/VideoActivity.java

@@ -0,0 +1,61 @@
+package com.owncloud.android.ui.activity;
+
+import android.app.Activity;
+import android.media.MediaPlayer;
+import android.media.MediaPlayer.OnCompletionListener;
+import android.media.MediaPlayer.OnPreparedListener;
+import android.os.Bundle;
+import android.view.MotionEvent;
+import android.widget.VideoView;
+
+import com.owncloud.android.R;
+
+public class VideoActivity extends Activity implements OnCompletionListener, OnPreparedListener {
+      
+   public static final String EXTRA_PATH = "PATH";
+   
+   private VideoView mVideoPlayer;
+   private String mPathToFile;
+      
+   /** Called when the activity is first created. */
+   @Override
+   public void onCreate(Bundle savedInstanceState) {
+      super.onCreate(savedInstanceState);
+      setContentView(R.layout.video_layout);
+
+      mPathToFile = getIntent().getExtras().getString(EXTRA_PATH);
+      
+      mVideoPlayer = (VideoView) findViewById(R.id.videoPlayer);   
+      mVideoPlayer.setOnPreparedListener(this);
+      mVideoPlayer.setOnCompletionListener(this);
+      mVideoPlayer.setKeepScreenOn(true);    
+      mVideoPlayer.setVideoPath(mPathToFile);
+   }
+
+   /** This callback will be invoked when the file is ready to play */
+   @Override
+   public void onPrepared(MediaPlayer vp) {
+      mVideoPlayer.start();
+   }
+   
+   /** This callback will be invoked when the file is finished playing */
+   @Override
+   public void onCompletion(MediaPlayer  mp) {
+      this.finish(); 
+   }
+   
+   /**  Use screen touches to toggle the video between playing and paused. */
+   @Override
+   public boolean onTouchEvent (MotionEvent ev){ 
+      if(ev.getAction() == MotionEvent.ACTION_DOWN){
+         if(mVideoPlayer.isPlaying()){
+                  mVideoPlayer.pause();
+         } else {
+                  mVideoPlayer.start();
+         }
+         return true;        
+      } else {
+         return false;
+      }
+   }
+}

+ 47 - 13
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -71,6 +71,7 @@ import android.widget.ImageView;
 import android.widget.MediaController;
 import android.widget.TextView;
 import android.widget.Toast;
+import android.widget.VideoView;
 
 import com.actionbarsherlock.app.SherlockFragment;
 import com.owncloud.android.AccountUtils;
@@ -97,6 +98,7 @@ import com.owncloud.android.ui.activity.ConflictsResolveActivity;
 import com.owncloud.android.ui.activity.FileDetailActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.TransferServiceGetter;
+import com.owncloud.android.ui.activity.VideoActivity;
 import com.owncloud.android.ui.dialog.EditNameDialog;
 import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
 import com.owncloud.android.utils.OwnCloudVersion;
@@ -133,6 +135,7 @@ public class FileDetailFragment extends SherlockFragment implements
     private Handler mHandler;
     private RemoteOperation mLastRemoteOperation;
     private DialogFragment mCurrentDialog;
+    
     private MediaServiceBinder mMediaServiceBinder = null;
     private MediaController mMediaController = null;
     private MediaServiceConnection mMediaServiceConnection = null;
@@ -418,25 +421,56 @@ public class FileDetailFragment extends SherlockFragment implements
     
     @Override
     public boolean onTouch(View v, MotionEvent event) {
-        if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown() && mFile.isAudio()) {
-            if (!mMediaServiceBinder.isPlaying(mFile)) {
-                Log.d(TAG, "starting playback of " + mFile.getStoragePath());
-                mMediaServiceBinder.start(mAccount, mFile);
-                // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService
-                mMediaController.postDelayed(new Runnable() {
-                    @Override
-                    public void run() {
-                        mMediaController.show(0);
-                    }
-                } , 300);
-            } else {
-                mMediaController.show(0);
+        if (v == mPreview && event.getAction() == MotionEvent.ACTION_DOWN && mFile != null && mFile.isDown()) {
+            if (mFile.isAudio()) {
+                if (!mMediaServiceBinder.isPlaying(mFile)) {
+                    Log.d(TAG, "starting playback of " + mFile.getStoragePath());
+                    mMediaServiceBinder.start(mAccount, mFile);
+                    // this is a patch; need to synchronize this with the onPrepared() coming from MediaPlayer in the MediaService
+                    mMediaController.postDelayed(new Runnable() {
+                        @Override
+                        public void run() {
+                            mMediaController.show(0);
+                        }
+                    } , 300);
+                } else {
+                    mMediaController.show(0);
+                }
+                
+            } else if (mFile.isVideo()) {
+                startVideoActivity();
             }
         }
         return false;
     }
 
     
+    private void startVideoActivity() {
+        Intent i = new Intent(getActivity(), VideoActivity.class);
+        i.putExtra(VideoActivity.EXTRA_PATH, mFile.getStoragePath());
+        startActivity(i);
+        
+        // TODO THROW AN ACTIVTIY JUST FOR PREVIEW VIDEO
+        /*
+        if (mMediaController == null) {
+            mMediaController = new MediaController(getActivity());
+            mMediaController.setAnchorView(mVideoPreview);
+            //mMediaController.setEnabled(true);
+        }
+        //mMediaController.setMediaPlayer(mMediaServiceBinder);
+        if (!mVideoPreviewIsLoaded) {
+            mVideoPreviewIsLoaded = true;
+            mMediaController.setMediaPlayer(mVideoPreview);
+            mVideoPreview.setMediaController(mMediaController);
+            mVideoPreview.setVideoPath(mFile.getStoragePath());
+            mVideoPreview.start();
+            //mMediaController.show(0);
+        } else {
+            mMediaController.show(0);
+        }*/
+    }
+
+
     private void bindMediaService() {
         Log.d(TAG, "Binding to MediaService...");
         if (mMediaServiceConnection == null) {