Merge pull request #1269 from owncloud/fix_video_not_previewed

Fix video not previewed #1180
This commit is contained in:
Maria Asensio 2015-11-11 17:13:42 +01:00
commit 80aacce3f3
3 changed files with 53 additions and 43 deletions

View file

@ -1,6 +1,8 @@
/** /**
* ownCloud Android client application * ownCloud Android client application
* *
* @author Bartek Przybylski
* @author David A. Velasco
* Copyright (C) 2012 Bartek Przybylski * Copyright (C) 2012 Bartek Przybylski
* Copyright (C) 2015 ownCloud Inc. * Copyright (C) 2015 ownCloud Inc.
* *
@ -20,6 +22,8 @@
package com.owncloud.android.datamodel; package com.owncloud.android.datamodel;
import android.content.ContentResolver;
import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
@ -80,6 +84,12 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
private boolean mShareWithSharee; private boolean mShareWithSharee;
/**
* URI to the local path of the file contents, if stored in the device; cached after first call
* to {@link #getStorageUri()}
*/
private Uri mLocalUri;
/** /**
* Create new {@link OCFile} with given path. * Create new {@link OCFile} with given path.
@ -213,6 +223,24 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
return mLocalPath; return mLocalPath;
} }
/**
* The URI to the file contents, if stored locally
*
* @return A URI to the local copy of the file, or NULL if not stored in the device
*/
public Uri getStorageUri() {
if (mLocalPath == null || mLocalPath.length() == 0) {
return null;
}
if (mLocalUri == null) {
Uri.Builder builder = new Uri.Builder();
builder.scheme(ContentResolver.SCHEME_FILE);
builder.path(mLocalPath);
mLocalUri = builder.build();
}
return mLocalUri;
}
/** /**
* Can be used to set the path where the file is stored * Can be used to set the path where the file is stored
* *
@ -220,6 +248,7 @@ public class OCFile implements Parcelable, Comparable<OCFile> {
*/ */
public void setStoragePath(String storage_path) { public void setStoragePath(String storage_path) {
mLocalPath = storage_path; mLocalPath = storage_path;
mLocalUri = null;
} }
/** /**

View file

@ -36,8 +36,6 @@ import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener; import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener; import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.IBinder; import android.os.IBinder;
import android.view.LayoutInflater; import android.view.LayoutInflater;
@ -153,7 +151,7 @@ public class PreviewMediaFragment extends FileFragment implements
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
Log_OC.e(TAG, "onCreateView"); Log_OC.v(TAG, "onCreateView");
mView = inflater.inflate(R.layout.file_preview, container, false); mView = inflater.inflate(R.layout.file_preview, container, false);
@ -174,7 +172,7 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
Log_OC.e(TAG, "onActivityCreated"); Log_OC.v(TAG, "onActivityCreated");
OCFile file = getFile(); OCFile file = getFile();
if (savedInstanceState == null) { if (savedInstanceState == null) {
@ -244,7 +242,7 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
Log_OC.e(TAG, "onSaveInstanceState"); Log_OC.v(TAG, "onSaveInstanceState");
outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile()); outState.putParcelable(PreviewMediaFragment.EXTRA_FILE, getFile());
outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount); outState.putParcelable(PreviewMediaFragment.EXTRA_ACCOUNT, mAccount);
@ -268,7 +266,7 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
Log_OC.e(TAG, "onStart"); Log_OC.v(TAG, "onStart");
OCFile file = getFile(); OCFile file = getFile();
if (file != null && file.isDown()) { if (file != null && file.isDown()) {
@ -402,7 +400,7 @@ public class PreviewMediaFragment extends FileFragment implements
/** /**
* Update the file of the fragment with file value * Update the file of the fragment with file value
* *
* @param file * @param file Replaces the held file with a new one
*/ */
public void updateFile(OCFile file) { public void updateFile(OCFile file) {
setFile(file); setFile(file);
@ -439,8 +437,7 @@ public class PreviewMediaFragment extends FileFragment implements
// load the video file in the video player ; // load the video file in the video player ;
// when done, VideoHelper#onPrepared() will be called // when done, VideoHelper#onPrepared() will be called
Uri uri = Uri.parse(getFile().getStoragePath()); mVideoPreview.setVideoURI(getFile().getStorageUri());
mVideoPreview.setVideoPath(uri.encode(getFile().getStoragePath()));
} }
@ -455,7 +452,7 @@ public class PreviewMediaFragment extends FileFragment implements
*/ */
@Override @Override
public void onPrepared(MediaPlayer vp) { public void onPrepared(MediaPlayer vp) {
Log_OC.e(TAG, "onPrepared"); Log_OC.v(TAG, "onPrepared");
mVideoPreview.seekTo(mSavedPlaybackPosition); mVideoPreview.seekTo(mSavedPlaybackPosition);
if (mAutoplay) { if (mAutoplay) {
mVideoPreview.start(); mVideoPreview.start();
@ -475,25 +472,9 @@ public class PreviewMediaFragment extends FileFragment implements
*/ */
@Override @Override
public void onCompletion(MediaPlayer mp) { public void onCompletion(MediaPlayer mp) {
Log_OC.e(TAG, "completed"); Log_OC.v(TAG, "completed");
if (mp != null) { if (mp != null) {
mVideoPreview.seekTo(0); mVideoPreview.seekTo(0);
// next lines are necessary to work around undesired video loops
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD) {
mVideoPreview.pause();
}
else {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.GINGERBREAD_MR1) {
// mVideePreview.pause() is not enough
mMediaController.setEnabled(false);
mVideoPreview.stopPlayback();
mAutoplay = false;
mSavedPlaybackPosition = 0;
mVideoPreview.setVideoPath(getFile().getStoragePath());
}
}
} // else : called from onError() } // else : called from onError()
mMediaController.updatePausePlay(); mMediaController.updatePausePlay();
} }
@ -508,6 +489,7 @@ public class PreviewMediaFragment extends FileFragment implements
*/ */
@Override @Override
public boolean onError(MediaPlayer mp, int what, int extra) { public boolean onError(MediaPlayer mp, int what, int extra) {
Log_OC.e(TAG, "Error in video playback, what = " + what + ", extra = " + extra);
if (mVideoPreview.getWindowToken() != null) { if (mVideoPreview.getWindowToken() != null) {
String message = MediaService.getMessageForMediaError( String message = MediaService.getMessageForMediaError(
getActivity(), what, extra); getActivity(), what, extra);
@ -531,25 +513,25 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onPause() { public void onPause() {
Log_OC.e(TAG, "onPause"); Log_OC.v(TAG, "onPause");
super.onPause(); super.onPause();
} }
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
Log_OC.e(TAG, "onResume"); Log_OC.v(TAG, "onResume");
} }
@Override @Override
public void onDestroy() { public void onDestroy() {
Log_OC.e(TAG, "onDestroy"); Log_OC.v(TAG, "onDestroy");
super.onDestroy(); super.onDestroy();
} }
@Override @Override
public void onStop() { public void onStop() {
Log_OC.e(TAG, "onStop"); Log_OC.v(TAG, "onStop");
mPrepared = false; mPrepared = false;
if (mMediaServiceConnection != null) { if (mMediaServiceConnection != null) {
@ -590,12 +572,12 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onConfigurationChanged(Configuration newConfig) { public void onConfigurationChanged(Configuration newConfig) {
Log_OC.e(TAG, "onConfigurationChanged " + this); Log_OC.v(TAG, "onConfigurationChanged " + this);
} }
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log_OC.e(TAG, "onActivityResult " + this); Log_OC.v(TAG, "onActivityResult " + this);
super.onActivityResult(requestCode, resultCode, data); super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
mSavedPlaybackPosition = data.getExtras().getInt( mSavedPlaybackPosition = data.getExtras().getInt(
@ -669,7 +651,7 @@ public class PreviewMediaFragment extends FileFragment implements
@Override @Override
public void onServiceDisconnected(ComponentName component) { public void onServiceDisconnected(ComponentName component) {
if (component.equals(new ComponentName(getActivity(), MediaService.class))) { if (component.equals(new ComponentName(getActivity(), MediaService.class))) {
Log_OC.e(TAG, "Media service suddenly disconnected"); Log_OC.w(TAG, "Media service suddenly disconnected");
if (mMediaController != null) { if (mMediaController != null) {
mMediaController.setMediaPlayer(null); mMediaController.setMediaPlayer(null);
} }
@ -733,7 +715,7 @@ public class PreviewMediaFragment extends FileFragment implements
if (mPrepared) { if (mPrepared) {
mSavedPlaybackPosition = mVideoPreview.getCurrentPosition(); mSavedPlaybackPosition = mVideoPreview.getCurrentPosition();
} }
Log_OC.e(TAG, "getting position: " + mSavedPlaybackPosition); Log_OC.v(TAG, "getting position: " + mSavedPlaybackPosition);
return mSavedPlaybackPosition; return mSavedPlaybackPosition;
} }

View file

@ -78,7 +78,7 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Log_OC.e(TAG, "ACTIVITY\t\tonCreate"); Log_OC.v(TAG, "onCreate");
setContentView(R.layout.video_layout); setContentView(R.layout.video_layout);
@ -110,7 +110,6 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
Log_OC.e(TAG, "ACTIVITY\t\tonSaveInstanceState");
outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition()); outState.putInt(PreviewVideoActivity.EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying()); outState.putBoolean(PreviewVideoActivity.EXTRA_AUTOPLAY , mVideoPlayer.isPlaying());
} }
@ -118,7 +117,7 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
@Override @Override
public void onBackPressed() { public void onBackPressed() {
Log_OC.e(TAG, "ACTIVTIY\t\tonBackPressed"); Log_OC.v(TAG, "onBackPressed");
Intent i = new Intent(); Intent i = new Intent();
i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying()); i.putExtra(EXTRA_AUTOPLAY, mVideoPlayer.isPlaying());
i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition()); i.putExtra(EXTRA_START_POSITION, mVideoPlayer.getCurrentPosition());
@ -136,7 +135,7 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
*/ */
@Override @Override
public void onPrepared(MediaPlayer mp) { public void onPrepared(MediaPlayer mp) {
Log_OC.e(TAG, "ACTIVITY\t\tonPrepare"); Log_OC.v(TAG, "onPrepare");
mVideoPlayer.seekTo(mSavedPlaybackPosition); mVideoPlayer.seekTo(mSavedPlaybackPosition);
if (mAutoplay) { if (mAutoplay) {
mVideoPlayer.start(); mVideoPlayer.start();
@ -204,8 +203,8 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
file = getStorageManager().getFileById(file.getFileId()); file = getStorageManager().getFileById(file.getFileId());
if (file != null) { if (file != null) {
if (file.isDown()) { if (file.isDown()) {
mVideoPlayer.setVideoPath(file.getStoragePath()); mVideoPlayer.setVideoURI(file.getStorageUri());
} else { } else {
// not working yet // not working yet
String url; String url;
@ -216,13 +215,13 @@ public class PreviewVideoActivity extends FileActivity implements OnCompletionLi
onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account); onError(null, MediaService.OC_MEDIA_ERROR, R.string.media_err_no_account);
} }
} }
// create and prepare control panel for the user // create and prepare control panel for the user
mMediaController = new MediaController(this); mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(mVideoPlayer); mMediaController.setMediaPlayer(mVideoPlayer);
mMediaController.setAnchorView(mVideoPlayer); mMediaController.setAnchorView(mVideoPlayer);
mVideoPlayer.setMediaController(mMediaController); mVideoPlayer.setMediaController(mMediaController);
} else { } else {
finish(); finish();
} }