allow playing videos

This commit is contained in:
tibbi 2016-06-08 20:58:45 +02:00
parent b45608a5ab
commit 309e52ef3a
12 changed files with 176 additions and 15 deletions

View file

@ -1,18 +1,33 @@
package com.simplemobiletools.gallery;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.davemorrissey.labs.subscaleview.ImageSource;
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView;
import com.simplemobiletools.gallery.activities.ViewPagerActivity;
public class ViewPagerFragment extends Fragment implements View.OnClickListener {
import java.io.IOException;
public class ViewPagerFragment extends Fragment
implements View.OnClickListener, SurfaceHolder.Callback, MediaPlayer.OnCompletionListener, MediaPlayer.OnVideoSizeChangedListener {
private static final String TAG = ViewPagerFragment.class.getSimpleName();
private static final String MEDIUM = "medium";
private Media medium;
private static SurfaceHolder surfaceHolder;
private static ImageView playOutline;
private static boolean isPlaying;
private static MediaPlayer mediaPlayer;
public void setMedium(Media medium) {
this.medium = medium;
@ -20,23 +35,49 @@ public class ViewPagerFragment extends Fragment implements View.OnClickListener
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.pager_item, container, false);
View view;
if (medium == null && savedInstanceState != null) {
medium = (Media) savedInstanceState.getSerializable(MEDIUM);
}
if (medium != null) {
final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view.findViewById(R.id.photo);
imageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
imageView.setImage(ImageSource.uri(medium.getPath()));
imageView.setMaxScale(5f);
imageView.setOnClickListener(this);
if (medium.getIsVideo()) {
view = inflater.inflate(R.layout.pager_video_item, container, false);
setupPlayer(view);
} else {
view = inflater.inflate(R.layout.pager_photo_item, container, false);
final SubsamplingScaleImageView imageView = (SubsamplingScaleImageView) view.findViewById(R.id.photo_view);
imageView.setOrientation(SubsamplingScaleImageView.ORIENTATION_USE_EXIF);
imageView.setImage(ImageSource.uri(medium.getPath()));
imageView.setMaxScale(5f);
}
} else {
view = inflater.inflate(R.layout.pager_photo_item, container, false);
}
view.setOnClickListener(this);
return view;
}
private void setupPlayer(View view) {
if (getActivity() == null)
return;
playOutline = (ImageView) view.findViewById(R.id.video_play_outline);
playOutline.setOnClickListener(this);
final SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.video_surface);
surfaceView.setOnClickListener(this);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
}
public void itemDragged() {
pauseVideo();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
@ -45,6 +86,95 @@ public class ViewPagerFragment extends Fragment implements View.OnClickListener
@Override
public void onClick(View v) {
((ViewPagerActivity) getActivity()).photoClicked();
switch (v.getId()) {
case R.id.video_play_outline:
togglePlayPause();
break;
default:
((ViewPagerActivity) getActivity()).photoClicked();
}
}
private void pauseVideo() {
if (isPlaying) {
togglePlayPause();
}
}
private void togglePlayPause() {
if (getActivity() == null)
return;
if (mediaPlayer == null)
initMediaPlayer();
isPlaying = !isPlaying;
if (isPlaying) {
mediaPlayer.start();
playOutline.setImageDrawable(null);
} else {
mediaPlayer.pause();
playOutline.setImageDrawable(getResources().getDrawable(R.mipmap.play_outline_big));
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
initMediaPlayer();
}
private void initMediaPlayer() {
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(getContext(), Uri.parse(medium.getPath()));
mediaPlayer.setDisplay(surfaceHolder);
mediaPlayer.setOnCompletionListener(this);
mediaPlayer.setOnVideoSizeChangedListener(this);
mediaPlayer.prepare();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
addPreviewImage();
} catch (IOException e) {
Log.e(TAG, "init media player " + e.getMessage());
}
}
private void addPreviewImage() {
mediaPlayer.start();
mediaPlayer.pause();
}
@Override
public void onPause() {
super.onPause();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
pauseVideo();
if (mediaPlayer != null) {
mediaPlayer.release();
mediaPlayer = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
@Override
public void onCompletion(MediaPlayer mp) {
pauseVideo();
}
@Override
public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
surfaceHolder.setFixedSize(width, height);
}
}

View file

@ -146,7 +146,7 @@ public class MediaActivity extends AppCompatActivity
if (curPath.matches(pattern) && !toBeDeleted.contains(curPath)) {
final File file = new File(curPath);
if (file.exists()) {
myMedia.add(new Media(cursor.getString(pathIndex), (i == 1)));
myMedia.add(new Media(curPath, (i == 1)));
} else {
invalidFiles.add(file.getAbsolutePath());
}

View file

@ -248,7 +248,7 @@ public class ViewPagerActivity extends AppCompatActivity
}
private List<Media> getMedia() {
final List<Media> media = new ArrayList<>();
final List<Media> myMedia = new ArrayList<>();
int j = 0;
for (int i = 0; i < 2; i++) {
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
@ -267,7 +267,7 @@ public class ViewPagerActivity extends AppCompatActivity
do {
final String curPath = cursor.getString(pathIndex);
if (curPath.matches(pattern) && !curPath.equals(toBeDeleted) && !curPath.equals(beingDeleted)) {
media.add(new Media(curPath, j == 1));
myMedia.add(new Media(curPath, i == 1));
if (curPath.equals(path)) {
pos = j;
@ -279,7 +279,7 @@ public class ViewPagerActivity extends AppCompatActivity
cursor.close();
}
}
return media;
return myMedia;
}
public void photoClicked() {
@ -355,7 +355,10 @@ public class ViewPagerActivity extends AppCompatActivity
@Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_DRAGGING) {
final MyPagerAdapter adapter = (MyPagerAdapter) pager.getAdapter();
adapter.itemDragged();
}
}
@Override

View file

@ -11,6 +11,7 @@ import java.util.List;
public class MyPagerAdapter extends FragmentStatePagerAdapter {
private List<Media> media;
private ViewPagerFragment fragment;
public MyPagerAdapter(FragmentManager fm, List<Media> media) {
super(fm);
@ -24,11 +25,15 @@ public class MyPagerAdapter extends FragmentStatePagerAdapter {
@Override
public Fragment getItem(int position) {
final ViewPagerFragment fragment = new ViewPagerFragment();
fragment = new ViewPagerFragment();
fragment.setMedium(media.get(position));
return fragment;
}
public void itemDragged() {
fragment.itemDragged();
}
public void updateItems(List<Media> newPaths) {
media.clear();
media.addAll(newPaths);

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="@+id/photo"
android:id="@+id/photo_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

View file

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/video_holder"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="100dp">
<SurfaceView
android:id="@+id/video_surface"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/transparent"/>
<ImageView
android:id="@+id/video_play_outline"
android:layout_width="@dimen/play_outline_size_big"
android:layout_height="@dimen/play_outline_size_big"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:src="@mipmap/play_outline_big"/>
</RelativeLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View file

@ -3,5 +3,6 @@
<dimen name="dir_tmb_size">150dp</dimen>
<dimen name="medium_tmb_size">100dp</dimen>
<dimen name="play_outline_size">40dp</dimen>
<dimen name="play_outline_size_big">160dp</dimen>
<dimen name="undo_padding">8dp</dimen>
</resources>