Skip to content

Commit

Permalink
add fill mode custom sample
Browse files Browse the repository at this point in the history
  • Loading branch information
MasayukiSuda committed Jul 23, 2019
1 parent 97d57b1 commit 7f90a56
Show file tree
Hide file tree
Showing 20 changed files with 1,404 additions and 318 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {
mavenLocal()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath 'com.android.tools.build:gradle:3.4.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
47 changes: 25 additions & 22 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion COMPILE_SDK_VERSION as int
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId "com.daasuu.mp4composer"
minSdkVersion COMPILE_MIN_SDK_VERSION as int
targetSdkVersion COMPILE_SDK_VERSION as int
compileSdkVersion COMPILE_SDK_VERSION as int
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId "com.daasuu.mp4composer"
minSdkVersion COMPILE_MIN_SDK_VERSION as int
targetSdkVersion COMPILE_SDK_VERSION as int

versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'

implementation 'com.github.bumptech.glide:glide:3.8.0'

implementation 'com.github.bumptech.glide:glide:3.8.0'
implementation project(':mp4compose')

implementation project(':mp4compose')
implementation 'com.google.android.exoplayer:exoplayer-core:2.10.2'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
}
20 changes: 12 additions & 8 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.daasuu.sample">

<!-- Camera permissions -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<!-- Microphone permissions -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<!-- Require OpenGL ES >= 2.0. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- Microphone permissions -->
<uses-permission android:name="android.permission.RECORD_AUDIO" /> <!-- Require OpenGL ES >= 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
Expand All @@ -24,7 +19,16 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.daasuu.sample.MainActivity">
<activity
android:name=".FillModeCustomActivity"
android:label="@string/title_fill_mode_custom" />
<activity
android:name=".BasicUsageActivity"
android:label="@string/title_basic_usage" />
<activity
android:name=".MovieListActivity"
android:label="@string/title_fill_mode_custom_movie_list" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
225 changes: 225 additions & 0 deletions sample/src/main/java/com/daasuu/sample/BasicUsageActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
package com.daasuu.sample;

import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.daasuu.mp4compose.FillMode;
import com.daasuu.mp4compose.composer.Mp4Composer;
import com.daasuu.mp4compose.filter.GlFilter;
import com.daasuu.mp4compose.filter.GlFilterGroup;
import com.daasuu.mp4compose.filter.GlMonochromeFilter;
import com.daasuu.mp4compose.filter.GlVignetteFilter;
import com.daasuu.sample.video.VideoItem;
import com.daasuu.sample.video.VideoListAdapter;
import com.daasuu.sample.video.VideoLoadListener;
import com.daasuu.sample.video.VideoLoader;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class BasicUsageActivity extends AppCompatActivity {

private VideoItem videoItem = null;

private static final String TAG = "SAMPLE";

private Mp4Composer mp4Composer;

private CheckBox muteCheckBox;
private CheckBox flipVerticalCheckBox;
private CheckBox flipHorizontalCheckBox;

private String videoPath;
private AlertDialog filterDialog;
private GlFilter glFilter = new GlFilterGroup(new GlMonochromeFilter(), new GlVignetteFilter());

public static void startActivity(Context context) {
Intent intent = new Intent(context, BasicUsageActivity.class);
context.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_basic_usage);

muteCheckBox = findViewById(R.id.mute_check_box);
flipVerticalCheckBox = findViewById(R.id.flip_vertical_check_box);
flipHorizontalCheckBox = findViewById(R.id.flip_horizontal_check_box);

findViewById(R.id.start_codec_button).setOnClickListener(v -> {
v.setEnabled(false);
startCodec();
});

findViewById(R.id.cancel_button).setOnClickListener(v -> {
if (mp4Composer != null) {
mp4Composer.cancel();
}
});

findViewById(R.id.start_play_movie).setOnClickListener(v -> {
Uri uri = Uri.parse(videoPath);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.setDataAndType(uri, "video/mp4");
startActivity(intent);
});

findViewById(R.id.btn_filter).setOnClickListener(v -> {
if (filterDialog == null) {

AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
builder.setTitle("Choose a filter");
builder.setOnDismissListener(dialog -> {
filterDialog = null;
});

final FilterType[] filters = FilterType.values();
CharSequence[] charList = new CharSequence[filters.length];
for (int i = 0, n = filters.length; i < n; i++) {
charList[i] = filters[i].name();
}
builder.setItems(charList, (dialog, item) -> {
changeFilter(filters[item]);
});
filterDialog = builder.show();
} else {
filterDialog.dismiss();
}
});

}

private void changeFilter(FilterType filter) {
glFilter = null;
glFilter = FilterType.createGlFilter(filter, this);
Button button = findViewById(R.id.btn_filter);
button.setText("Filter : " + filter.name());
}

@Override
protected void onResume() {
super.onResume();
VideoLoader videoLoader = new VideoLoader(getApplicationContext());
videoLoader.loadDeviceVideos(new VideoLoadListener() {
@Override
public void onVideoLoaded(final List<VideoItem> items) {

ListView lv = findViewById(R.id.video_list);
VideoListAdapter adapter = new VideoListAdapter(getApplicationContext(), R.layout.row_video_list, items);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
videoItem = null;
videoItem = items.get(position);
findViewById(R.id.start_codec_button).setEnabled(true);
}
});
}

@Override
public void onFailed(Exception e) {
e.printStackTrace();
}
});
}

private void startCodec() {
videoPath = getVideoFilePath();

final ProgressBar progressBar = findViewById(R.id.progress_bar);
progressBar.setMax(100);


mp4Composer = null;
mp4Composer = new Mp4Composer(videoItem.getPath(), videoPath)
// .rotation(Rotation.ROTATION_270)
.size(720, 720)
.fillMode(FillMode.PRESERVE_ASPECT_FIT)
.filter(glFilter)
.mute(muteCheckBox.isChecked())
.flipHorizontal(flipHorizontalCheckBox.isChecked())
.flipVertical(flipVerticalCheckBox.isChecked())
.listener(new Mp4Composer.Listener() {
@Override
public void onProgress(double progress) {
Log.d(TAG, "onProgress = " + progress);
runOnUiThread(() -> progressBar.setProgress((int) (progress * 100)));
}

@Override
public void onCompleted() {
Log.d(TAG, "onCompleted()");
exportMp4ToGallery(getApplicationContext(), videoPath);
runOnUiThread(() -> {
progressBar.setProgress(100);
findViewById(R.id.start_codec_button).setEnabled(true);
findViewById(R.id.start_play_movie).setEnabled(true);
Toast.makeText(BasicUsageActivity.this, "codec complete path =" + videoPath, Toast.LENGTH_SHORT).show();
});
}

@Override
public void onCanceled() {

}

@Override
public void onFailed(Exception exception) {
Log.d(TAG, "onFailed()");
}
})
.start();


}


public File getAndroidMoviesFolder() {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
}

public String getVideoFilePath() {
return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "filter_apply.mp4";
}

/**
* ギャラリーにエクスポート
*
* @param filePath
* @return The video MediaStore URI
*/
public static void exportMp4ToGallery(Context context, String filePath) {
// ビデオのメタデータを作成する
final ContentValues values = new ContentValues(2);
values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
values.put(MediaStore.Video.Media.DATA, filePath);
// MediaStoreに登録
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
values);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + filePath)));
}


}
Loading

0 comments on commit 7f90a56

Please sign in to comment.