-
Notifications
You must be signed in to change notification settings - Fork 3
/
MainActivity.java
144 lines (112 loc) · 4.29 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.example.mp4parser.test.mp4parsertest;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.ffmpeg.android;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private Button shortenButton;
private EditText startET;
private EditText endET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shortenButton = (Button) findViewById(R.id.shorten_file_button);
shortenButton.setOnClickListener(this);
startET = (EditText) findViewById(R.id.start_edit_text);
endET = (EditText) findViewById(R.id.end_edit_text);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
if ((startET.getText().toString().equals("")) || (endET.getText().toString().equals(""))) {
Log.d(TAG, "onClick: Empty");
return;
}
int startMS = Integer.parseInt(startET.getText().toString());
int endMS = Integer.parseInt(endET.getText().toString());
Log.d(TAG, "onClick: startMS: " + startMS + ", endMS: " + endMS);
shortenButton.setEnabled(false);
new ShortenAsync(startMS, endMS).execute();
}
private class ShortenAsync extends AsyncTask<Void, Void, Void> {
private final int startMs;
private final int endMs;
public ShortenAsync(int startMs, int endMs) {
this.startMs = startMs;
this.endMs = endMs;
}
@Override
protected Void doInBackground(Void... nothing) {
File moviesDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MOVIES
);
String filePrefix = "GOPR1008";
String fileExtn = ".MP4";
String fileName = filePrefix + fileExtn;
try {
InputStream inputStream = getAssets().open(fileName);
File src = new File(moviesDir, fileName);
storeFile(inputStream, src);
File dest = new File(moviesDir, filePrefix + "_1" + fileExtn);
Log.d(TAG, "startTrim: src: " + src.getAbsolutePath());
Log.d(TAG, "startTrim: dest: " + dest.getAbsolutePath());
Log.d(TAG, "startTrim: startMs: " + startMs);
Log.d(TAG, "startTrim: endMs: " + endMs);
SimpleShortenExample.startTrim(src, dest, startMs, endMs);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void nothing) {
Log.d(TAG, "onPostExecute");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, R.string.complete, Toast.LENGTH_SHORT).show();
if (shortenButton != null) shortenButton.setEnabled(true);
}
});
}
}
private void storeFile(InputStream input, File file) {
try {
final OutputStream output = new FileOutputStream(file);
try {
try {
final byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer)) != -1)
output.write(buffer, 0, read);
output.flush();
} finally {
output.close();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}