Initial support for video playback.

parent de18be86
......@@ -20,6 +20,8 @@ android {
repositories {
mavenCentral()
jcenter()
google()
maven { url 'https://maven.google.com' }
maven { url "https://jitpack.io" }
}
......@@ -37,4 +39,5 @@ dependencies {
compile 'com.github.chrisbanes:PhotoView:2.1.3'
compile 'com.google.android.exoplayer:exoplayer:r2.5.4'
}
......@@ -3,7 +3,7 @@
package="com.systemallica.gallery">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
......@@ -30,6 +30,11 @@
android:name=".ImageActivity"
android:label="@string/title_activity_image"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".VideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/title_activity_video"
android:theme="@style/AppTheme.NoActionBar"/>
</application>
</manifest>
\ No newline at end of file
......@@ -170,13 +170,23 @@ public class FolderActivity extends AppCompatActivity {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
// Create intent
Intent intent = new Intent(FolderActivity.this, ImageActivity.class);
// Pass arrayList of image paths
intent.putExtra("position", position);
intent.putExtra("list_of_images", list_of_paths);
// Start activity
startActivityForResult(intent, 1);
if(Utils.isVideo(list_of_paths.get(position))){
// Create video intent
Intent intent = new Intent(FolderActivity.this, VideoActivity.class);
// Pass arrayList of image paths
intent.putExtra("position", position);
intent.putExtra("list_of_images", list_of_paths);
// Start activity
startActivityForResult(intent, 2);
}else {
// Create image intent
Intent intent = new Intent(FolderActivity.this, ImageActivity.class);
// Pass arrayList of image paths
intent.putExtra("position", position);
intent.putExtra("list_of_images", list_of_paths);
// Start activity
startActivityForResult(intent, 1);
}
}
});
}
......
package com.systemallica.gallery;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import com.google.android.exoplayer2.ExoPlayerFactory;
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.extractor.DefaultExtractorsFactory;
import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.source.ExtractorMediaSource;
import com.google.android.exoplayer2.source.MediaSource;
import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.trackselection.TrackSelector;
import com.google.android.exoplayer2.ui.SimpleExoPlayerView;
import com.google.android.exoplayer2.upstream.BandwidthMeter;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultBandwidthMeter;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.util.ArrayList;
public class VideoActivity extends AppCompatActivity {
private static final int UI_ANIMATION_DELAY = 300;
private final Handler mHideHandler = new Handler();
private final Runnable mHidePart2Runnable = new Runnable() {
@SuppressLint("InlinedApi")
@Override
public void run() {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
private final Runnable mShowPart2Runnable = new Runnable() {
@Override
public void run() {
// Delayed display of UI elements
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.show();
}
}
};
private boolean mVisible;
private final Runnable mHideRunnable = new Runnable() {
@Override
public void run() {
hide();
}
};
private SimpleExoPlayerView playerView;
private SimpleExoPlayer player;
ArrayList<String> list_of_images = new ArrayList<>();
int position_intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mVisible = true;
playerView = findViewById(R.id.video_view);
Intent intent = getIntent();
// Get all the images in the folder
list_of_images = intent.getStringArrayListExtra("list_of_images");
// Get position
position_intent = intent.getIntExtra("position", 0);
if (getSupportActionBar() != null) {
// Display arrow to return to previous activity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
exoPlayer();
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
if(action == 1) {
toggle();
}
return true;
}
@Override
public void onPause() {
super.onPause();
if (Util.SDK_INT <= 23) {
player.release();
}
}
@Override
public void onStop() {
super.onStop();
if (Util.SDK_INT > 23) {
player.release();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Trigger the initial hide() shortly after the activity has been
// created, to briefly hint to the user that UI controls
// are available.
delayedHide();
}
public void exoPlayer(){
// 1. Create a default TrackSelector
BandwidthMeter bandwidthMeter = new DefaultBandwidthMeter();
TrackSelection.Factory videoTrackSelectionFactory =
new AdaptiveTrackSelection.Factory(bandwidthMeter);
TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
// 2. Create the player
player = ExoPlayerFactory.newSimpleInstance(this, trackSelector);
// Bind the player to the view.
playerView.setPlayer(player);
// Measures bandwidth during playback. Can be null if not required.
DefaultBandwidthMeter bandwidthMeter2 = new DefaultBandwidthMeter();
// Produces DataSource instances through which media data is loaded.
DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this,
Util.getUserAgent(this, "yourApplicationName"), bandwidthMeter2);
// Produces Extractor instances for parsing the media data.
ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
// This is the MediaSource representing the media to be played.
MediaSource videoSource = new ExtractorMediaSource(Uri.fromFile(new File(list_of_images.get(position_intent))),
dataSourceFactory, extractorsFactory, null, null);
// Prepare the player with the source.
player.prepare(videoSource);
player.setPlayWhenReady(true);
}
private void toggle() {
if (mVisible) {
hide();
} else {
show();
}
}
private void hide() {
// Hide UI first
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.hide();
}
mVisible = false;
// Schedule a runnable to remove the status and navigation bar
mHideHandler.removeCallbacks(mShowPart2Runnable);
mHideHandler.post(mHidePart2Runnable);
}
@SuppressLint("InlinedApi")
private void show() {
// Show the system bar
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
mVisible = true;
// Schedule a runnable to display UI elements after a delay
mHideHandler.removeCallbacks(mHidePart2Runnable);
mHideHandler.postDelayed(mShowPart2Runnable, UI_ANIMATION_DELAY);
}
private void delayedHide() {
mHideHandler.removeCallbacks(mHideRunnable);
mHideHandler.postDelayed(mHideRunnable, UI_ANIMATION_DELAY);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.systemallica.gallery.VideoActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_marginTop="24dp"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<com.google.android.exoplayer2.ui.SimpleExoPlayerView
android:id="@+id/video_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
......@@ -14,6 +14,7 @@
<string name="title_activity_folder">Folder</string>
<string name="title_activity_image">Image</string>
<string name="title_activity_video">Video</string>
<string name="no_images">The folder is empty!</string>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment