├── .gitignore
├── .idea
├── compiler.xml
├── copyright
│ └── profiles_settings.xml
├── gradle.xml
├── markdown-navigator.xml
├── markdown-navigator
│ └── profiles_settings.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── 1.gif
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── example
│ │ └── jerei
│ │ └── videodemo
│ │ └── ExampleInstrumentedTest.java
│ ├── main
│ ├── AndroidManifest.xml
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── jerei
│ │ │ └── videodemo
│ │ │ ├── MainActivity.java
│ │ │ └── SystemAppUtils.java
│ └── res
│ │ ├── drawable
│ │ ├── btn_video.png
│ │ ├── btn_video_hover.png
│ │ ├── btn_video_record.xml
│ │ └── style_recorder_progress.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-hdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-mdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xhdpi
│ │ └── ic_launcher.png
│ │ ├── mipmap-xxhdpi
│ │ └── ic_launcher.png
│ │ ├── values-w820dp
│ │ └── dimens.xml
│ │ └── values
│ │ ├── colors.xml
│ │ ├── dimens.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── jerei
│ └── videodemo
│ └── ExampleUnitTest.java
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── shvideolibrary
├── .gitignore
├── assets
├── armeabi-v7a-neon
│ └── ffmpeg
├── armeabi-v7a
│ └── ffmpeg
└── x86
│ └── ffmpeg
├── build.gradle
├── libs
├── armeabi-v7a
│ └── libARM_ARCH.so
├── armeabi
│ └── libARM_ARCH.so
├── sardar.jar
└── x86
│ └── libARM_ARCH.so
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── com
│ └── sh
│ └── shvideolibrary
│ └── ExampleInstrumentedTest.java
├── main
├── AndroidManifest.xml
├── java
│ └── com
│ │ └── sh
│ │ └── shvideolibrary
│ │ ├── CameraPreview.java
│ │ ├── VideoInputActivity.java
│ │ ├── VideoInputDialog.java
│ │ └── compression
│ │ ├── CompressListener.java
│ │ ├── Compressor.java
│ │ ├── CompressorUtils.java
│ │ └── InitListener.java
└── res
│ ├── drawable
│ ├── btn_video.png
│ ├── btn_video_hover.png
│ ├── btn_video_record.xml
│ └── style_recorder_progress.xml
│ ├── layout
│ ├── activity_video_input.xml
│ └── dialog_video_input.xml
│ ├── mipmap-xxxhdpi
│ ├── ic_camera_menu_switch.png
│ ├── ic_flash_off_white.png
│ ├── ic_flash_on_white.png
│ ├── ic_launcher.png
│ ├── player_record.png
│ └── player_stop.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
└── test
└── java
└── com
└── sh
└── shvideolibrary
└── ExampleUnitTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/.idea/copyright/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/markdown-navigator.xml:
--------------------------------------------------------------------------------
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 |
--------------------------------------------------------------------------------
/.idea/markdown-navigator/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
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 | Android API 7 Platform
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/1.gif
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # ShVideoDemo
2 | Android 视频录制Demo 防微信小视频 视频压缩(FFmpeg)
3 |
4 | #Download
5 |
6 | Download the latest JAR or grab via Maven:
7 | ~~~xml
8 |
9 | com.sh.shvideo
10 | shvideolibrary
11 | 1.5.0
12 | pom
13 |
14 | ~~~
15 | or Gradle:
16 | ~~~gradle
17 | compile 'com.sh.shvideo:shvideolibrary:1.5.0'
18 | ~~~
19 | 注意:targetSdkVersion 23 及以上 要注意 6.0运行时权限 或干脆用23以下
20 |
21 | 
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "25.0.0"
6 | defaultConfig {
7 | applicationId "com.sh.zsh.code"
8 | minSdkVersion 15
9 | targetSdkVersion 22
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 |
21 | }
22 |
23 | dependencies {
24 | compile fileTree(include: ['*.jar'], dir: 'libs')
25 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
26 | exclude group: 'com.android.support', module: 'support-annotations'
27 | })
28 | compile 'com.android.support:appcompat-v7:24.2.1'
29 | testCompile 'junit:junit:4.12'
30 | compile project(path: ':shvideolibrary')
31 | }
32 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Java\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/jerei/videodemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.example.jerei.videodemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.example.jerei.videodemo", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/jerei/videodemo/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.jerei.videodemo;
2 |
3 | import android.Manifest;
4 | import android.app.Activity;
5 | import android.content.Intent;
6 | import android.content.pm.ActivityInfo;
7 | import android.content.pm.PackageManager;
8 | import android.graphics.Bitmap;
9 | import android.graphics.PixelFormat;
10 | import android.media.MediaMetadataRetriever;
11 | import android.media.MediaRecorder;
12 | import android.media.ThumbnailUtils;
13 | import android.os.Build;
14 | import android.provider.MediaStore;
15 | import android.support.v4.app.FragmentActivity;
16 | import android.support.v7.app.AppCompatActivity;
17 | import android.os.Bundle;
18 | import android.text.TextUtils;
19 | import android.util.Log;
20 | import android.view.SurfaceHolder;
21 | import android.view.SurfaceView;
22 | import android.view.View;
23 | import android.view.Window;
24 | import android.view.WindowManager;
25 | import android.widget.Button;
26 | import android.widget.ImageView;
27 | import android.widget.ProgressBar;
28 | import android.widget.ScrollView;
29 | import android.widget.TextView;
30 | import android.widget.Toast;
31 |
32 |
33 | import com.sh.shvideolibrary.VideoInputActivity;
34 | import com.sh.shvideolibrary.VideoInputDialog;
35 | import com.sh.shvideolibrary.compression.CompressListener;
36 | import com.sh.shvideolibrary.compression.CompressorUtils;
37 |
38 | import java.io.File;
39 | import java.io.IOException;
40 | import java.util.ArrayList;
41 | import java.util.List;
42 | import java.util.regex.Matcher;
43 | import java.util.regex.Pattern;
44 |
45 | /**
46 | * Created by zhush on 2016/11/11
47 | * E-mail zhush@jerei.com
48 | * PS 視頻DEMO
49 | */
50 | public class MainActivity extends AppCompatActivity implements VideoInputDialog.VideoCall{
51 |
52 | ImageView image;
53 | ImageView imag2;
54 | Button button;
55 | Button button2;
56 | Button button3;
57 |
58 | TextView first;
59 | TextView back;
60 | ProgressBar progressBar;
61 |
62 |
63 | static String TAG="MainActivity";
64 |
65 | String path;//视频录制输出地址
66 | //视频压缩数据地址
67 | private String currentOutputVideoPath = "/mnt/sdcard/out.mp4";
68 | private static final int REQUEST_CODE_FOR_RECORD_VIDEO = 5230;//录制视频请求码
69 | Double videoLength=0.0;//视频时长
70 | @Override
71 | protected void onCreate(Bundle savedInstanceState) {
72 | super.onCreate(savedInstanceState);
73 | setContentView(R.layout.activity_main);
74 | image = (ImageView) findViewById(R.id.image);
75 | button = (Button) findViewById(R.id.button);
76 | button2 = (Button) findViewById(R.id.button2);
77 | button3 = (Button) findViewById(R.id.button3);
78 | first = (TextView) findViewById(R.id.first);
79 | back = (TextView) findViewById(R.id.back);
80 | progressBar= (ProgressBar) findViewById(R.id.progressBar);
81 | imag2 = (ImageView) findViewById(R.id.imag2);
82 | button.setOnClickListener(new View.OnClickListener() {
83 | @Override
84 | public void onClick(View view) {
85 | //显示视频录制控件
86 | VideoInputDialog.show(getSupportFragmentManager(),MainActivity.this,VideoInputDialog.Q720,MainActivity.this);
87 | }
88 | });
89 | button2.setOnClickListener(new View.OnClickListener() {
90 | @Override
91 | public void onClick(View view) {
92 | VideoInputActivity.startActivityForResult(MainActivity.this, REQUEST_CODE_FOR_RECORD_VIDEO,VideoInputActivity.Q720);
93 | }
94 | });
95 | /**
96 | * 压缩视频
97 | */
98 | button3.setOnClickListener(new View.OnClickListener() {
99 | @Override
100 | public void onClick(View view) {
101 | //获取视频时长 计算压缩进度用
102 | MediaMetadataRetriever retr = new MediaMetadataRetriever();
103 | retr.setDataSource(path);
104 | String time = retr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);//获取视频时长
105 | //7680
106 | try {
107 | videoLength = Double.parseDouble(time)/1000.00;
108 | } catch (Exception e) {
109 | e.printStackTrace();
110 | videoLength = 0.00;
111 | }
112 | Log.v(TAG, "videoLength = "+videoLength + "s");
113 |
114 |
115 | /**
116 | * 压缩视频
117 | */
118 | CompressorUtils compressorUtils = new CompressorUtils(path,currentOutputVideoPath,MainActivity.this);
119 | compressorUtils.execCommand(new CompressListener() {
120 | @Override
121 | public void onExecSuccess(String message) {
122 | Log.i(TAG, "success " + message);
123 | progressBar.setVisibility(View.INVISIBLE);
124 | textAppend(getString(R.string.compress_succeed));
125 | back.setText(getFileSize(currentOutputVideoPath));
126 | //获取缩略图
127 | Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(currentOutputVideoPath, MediaStore.Video.Thumbnails.MINI_KIND);
128 | imag2.setImageBitmap(bitmap);
129 | }
130 |
131 | @Override
132 | public void onExecFail(String reason) {
133 | Log.i(TAG, "fail " + reason);
134 | }
135 |
136 | @Override
137 | public void onExecProgress(String message) {
138 | progressBar.setVisibility(View.VISIBLE);
139 | textAppend(getString(R.string.compress_progress, message));
140 |
141 | int i = getProgress(message);
142 | Log.e("进度",i+"");
143 | progressBar.setProgress(i);
144 | }
145 | });
146 | }
147 | });
148 |
149 | image.setOnClickListener(new View.OnClickListener() {
150 | @Override
151 | public void onClick(View view) {
152 | openView(path);
153 | }
154 | });
155 | imag2.setOnClickListener(new View.OnClickListener() {
156 | @Override
157 | public void onClick(View view) {
158 | openView(currentOutputVideoPath);
159 | }
160 | });
161 |
162 |
163 |
164 |
165 | }
166 |
167 | /**
168 | * 小视屏录制回调
169 | * @param path
170 | */
171 | @Override
172 | public void videoPathCall(String path) {
173 |
174 | Log.e("地址:",path);
175 | //根据视频地址获取缩略图
176 | this.path =path;
177 | Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
178 | image.setImageBitmap(bitmap);
179 | first.setText(getFileSize(path));
180 |
181 |
182 | }
183 |
184 | /**
185 | * 录制视频回调
186 | * @param requestCode
187 | * @param resultCode
188 | * @param data
189 | */
190 | @Override
191 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
192 | if(requestCode==REQUEST_CODE_FOR_RECORD_VIDEO&&resultCode==RESULT_CANCELED){
193 |
194 | }
195 | if(requestCode==REQUEST_CODE_FOR_RECORD_VIDEO&&resultCode==RESULT_OK){
196 | String path = data.getStringExtra(VideoInputActivity.INTENT_EXTRA_VIDEO_PATH);
197 | Log.e("地址:",path);
198 | //根据视频地址获取缩略图
199 | this.path =path;
200 | Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MINI_KIND);
201 | image.setImageBitmap(bitmap);
202 | first.setText(getFileSize(path));
203 | }
204 | super.onActivityResult(requestCode, resultCode, data);
205 | }
206 |
207 | public void openView(String path){
208 | if(TextUtils.isEmpty(path)){
209 |
210 | return;
211 | }
212 | File file = new File(path);
213 | SystemAppUtils.openFile(file,this);
214 | }
215 |
216 |
217 |
218 | private String getFileSize(String path) {
219 | File f = new File(path);
220 | if (!f.exists()) {
221 | return "0 MB";
222 | } else {
223 | long size = f.length();
224 | return (size / 1024f) / 1024f + "MB";
225 | }
226 | }
227 | int progress=0;
228 | private int getProgress(String source){
229 | // Duration: 00:00:22.50, start: 0.000000, bitrate: 13995 kb/s
230 |
231 | //progress frame= 28 fps=0.0 q=24.0 size= 107kB time=00:00:00.91 bitrate= 956.4kbits/s
232 | if(source.contains("start: 0.000000")){
233 | return progress;
234 | }
235 | Pattern p = Pattern.compile("00:\\d{2}:\\d{2}");
236 | Matcher m = p.matcher(source);
237 | if (m.find()) {
238 | //00:00:00
239 | String result = m.group(0);
240 | String temp[] = result.split(":");
241 | Double seconds = Double.parseDouble(temp[1]) * 60 + Double.parseDouble(temp[2]);
242 |
243 | if (0 != videoLength) {
244 | Log.v("进度长度", "current second = " + seconds+"/videoLength="+videoLength);
245 | progress = (int)(seconds *100/ videoLength);
246 |
247 | return progress;
248 | }
249 | return progress;
250 | }
251 | return progress;
252 | }
253 |
254 |
255 | private void textAppend(String text) {
256 | if (!TextUtils.isEmpty(text)) {
257 | Log.e("日志",text);
258 | }
259 | }
260 | }
261 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/jerei/videodemo/SystemAppUtils.java:
--------------------------------------------------------------------------------
1 | package com.example.jerei.videodemo;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.net.Uri;
7 |
8 | import java.io.File;
9 |
10 | /**
11 | * Created by zhush on 2016/10/24.
12 | * E-mail zhush@jerei.com
13 | */
14 |
15 | public class SystemAppUtils {
16 | //文件类型后缀
17 | private static final String[][] MIME_MapTable = { { ".3gp", "video/3gpp" }, { ".apk", "application/vnd.android.package-archive" }, { ".asf", "video/x-ms-asf" }, { ".avi", "video/x-msvideo" }, { ".bin", "application/octet-stream" }, { ".bmp", "image/bmp" }, { ".c", "text/plain" }, { ".class", "application/octet-stream" }, { ".conf", "text/plain" }, { ".cpp", "text/plain" }, { ".doc", "application/msword" }, { ".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }, { ".xls", "application/vnd.ms-excel" }, { ".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }, { ".exe", "application/octet-stream" }, { ".gif", "image/gif" }, { ".gtar", "application/x-gtar" }, { ".gz", "application/x-gzip" }, { ".h", "text/plain" }, { ".htm", "text/html" }, { ".html", "text/html" }, { ".jar", "application/java-archive" }, { ".java", "text/plain" }, { ".jpeg", "image/jpeg" }, { ".jpg", "image/jpeg" }, { ".js", "application/x-javascript" }, { ".log", "text/plain" }, { ".m3u", "audio/x-mpegurl" }, { ".m4a", "audio/mp4a-latm" }, { ".m4b", "audio/mp4a-latm" }, { ".m4p", "audio/mp4a-latm" }, { ".m4u", "video/vnd.mpegurl" }, { ".m4v", "video/x-m4v" }, { ".mov", "video/quicktime" }, { ".mp2", "audio/x-mpeg" }, { ".mp3", "audio/x-mpeg" }, { ".mp4", "video/mp4" }, { ".mpc", "application/vnd.mpohun.certificate" }, { ".mpe", "video/mpeg" }, { ".mpeg", "video/mpeg" }, { ".mpg", "video/mpeg" }, { ".mpg4", "video/mp4" }, { ".mpga", "audio/mpeg" }, { ".msg", "application/vnd.ms-outlook" }, { ".ogg", "audio/ogg" }, { ".pdf", "application/pdf" }, { ".png", "image/png" }, { ".pps", "application/vnd.ms-powerpoint" }, { ".ppt", "application/vnd.ms-powerpoint" }, { ".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation" }, { ".prop", "text/plain" }, { ".rc", "text/plain" }, { ".rmvb", "audio/x-pn-realaudio" }, { ".rtf", "application/rtf" }, { ".sh", "text/plain" }, { ".tar", "application/x-tar" }, { ".tgz", "application/x-compressed" }, { ".txt", "text/plain" }, { ".wav", "audio/x-wav" }, { ".wma", "audio/x-ms-wma" }, { ".wmv", "audio/x-ms-wmv" }, { ".wps", "application/vnd.ms-works" }, { ".xml", "text/plain" }, { ".z", "application/x-compress" }, { ".zip", "application/x-zip-compressed" }, { "", "*/*" } };
18 |
19 |
20 | /**
21 | * 打开文件
22 | * @param file
23 | */
24 | public static void openFile(File file, Activity activity){
25 | Intent intent = new Intent(Intent.ACTION_VIEW);
26 | String type = getMIMEType(file);
27 | Uri uri = Uri.fromFile(file);
28 | intent.setDataAndType(uri, type);
29 | activity.startActivity(intent);
30 | activity.overridePendingTransition(0, 0);
31 | }
32 |
33 | /**
34 | * 获取文件类型后缀
35 | * @param paramFile
36 | * @return
37 | */
38 | private static String getMIMEType(File paramFile)
39 | {
40 | String str1 = "*/*";
41 | String str2 = paramFile.getName();
42 | int i = str2.lastIndexOf(".");
43 | if (i < 0)
44 | return str1;
45 | String str3 = str2.substring(i, str2.length()).toLowerCase();
46 | if (str3 == "")
47 | return str1;
48 | for (int j = 0; ; j++)
49 | {
50 | if (j >= MIME_MapTable.length)
51 | return str1;
52 | if (str3.equals(MIME_MapTable[j][0]))
53 | str1 = MIME_MapTable[j][1];
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_video.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/drawable/btn_video.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_video_hover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/drawable/btn_video_hover.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/btn_video_record.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/style_recorder_progress.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
7 |
8 |
9 | -
11 |
12 |
13 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
5 |
10 |
11 |
16 |
21 |
26 |
27 |
28 |
34 |
39 |
45 |
50 |
55 |
60 |
61 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #25272A
8 | #1173CB
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | VideoDemo
3 | 录制过短,请重试
4 |
5 |
6 |
7 | 权限申请
8 | 权限申请啊权限申请啊,权限申请啊权限申请啊,到底给不给,没权限搞不了sd卡啊!!!
9 | 到设置页面重新授予权限
10 | 知道了,任性退出
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
13 |
14 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/jerei/videodemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.example.jerei.videodemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.3'
9 |
10 | // NOTE: Do not place your application dependencies here; they belong
11 | // in the individual module build.gradle files
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Dec 28 10:00:20 PST 2015
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':shvideolibrary'
2 |
--------------------------------------------------------------------------------
/shvideolibrary/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/shvideolibrary/assets/armeabi-v7a-neon/ffmpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/assets/armeabi-v7a-neon/ffmpeg
--------------------------------------------------------------------------------
/shvideolibrary/assets/armeabi-v7a/ffmpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/assets/armeabi-v7a/ffmpeg
--------------------------------------------------------------------------------
/shvideolibrary/assets/x86/ffmpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/assets/x86/ffmpeg
--------------------------------------------------------------------------------
/shvideolibrary/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion 24
5 | buildToolsVersion "25.0.0"
6 |
7 | defaultConfig {
8 | minSdkVersion 15
9 | targetSdkVersion 22
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | minifyEnabled false
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 | }
22 | sourceSets.main {
23 | assets.srcDirs = ['assets']
24 | jniLibs.srcDirs = ['libs']//指定jniLibs文件夹路径
25 | jni.srcDirs = []//不编译jni
26 |
27 | }
28 | }
29 |
30 | dependencies {
31 | compile fileTree(include: ['*.jar'], dir: 'libs')
32 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
33 | exclude group: 'com.android.support', module: 'support-annotations'
34 | })
35 | compile 'com.android.support:appcompat-v7:24.2.1'
36 | testCompile 'junit:junit:4.12'
37 | compile files('libs/sardar.jar')
38 | }
39 |
--------------------------------------------------------------------------------
/shvideolibrary/libs/armeabi-v7a/libARM_ARCH.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/libs/armeabi-v7a/libARM_ARCH.so
--------------------------------------------------------------------------------
/shvideolibrary/libs/armeabi/libARM_ARCH.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/libs/armeabi/libARM_ARCH.so
--------------------------------------------------------------------------------
/shvideolibrary/libs/sardar.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/libs/sardar.jar
--------------------------------------------------------------------------------
/shvideolibrary/libs/x86/libARM_ARCH.so:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/libs/x86/libARM_ARCH.so
--------------------------------------------------------------------------------
/shvideolibrary/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in D:\Java\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
--------------------------------------------------------------------------------
/shvideolibrary/src/androidTest/java/com/sh/shvideolibrary/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.sh.shvideolibrary.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/CameraPreview.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary;
2 |
3 | import android.content.Context;
4 | import android.content.res.Configuration;
5 | import android.hardware.Camera;
6 | import android.util.Log;
7 | import android.view.SurfaceHolder;
8 | import android.view.SurfaceView;
9 |
10 | import java.io.IOException;
11 |
12 |
13 | /** 摄像头预览界面控件 */
14 | public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
15 |
16 | private SurfaceHolder mHolder;
17 | private Camera mCamera;
18 | private static final String TAG = "CameraPreview";
19 | private String flashMode = Camera.Parameters.FLASH_MODE_OFF;
20 | public CameraPreview(Context context, Camera camera) {
21 | super(context);
22 | mCamera = camera;
23 | mHolder = getHolder();
24 | mHolder.addCallback(this);
25 | mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
26 | }
27 |
28 | @Override
29 | public void surfaceCreated(SurfaceHolder holder) {
30 | try {
31 | if (mCamera == null) return;
32 | mCamera.setPreviewDisplay(holder);
33 |
34 | Camera.Parameters parameters = mCamera.getParameters();
35 | parameters.setFlashMode(flashMode);
36 | mCamera.setParameters(parameters);
37 |
38 | mCamera.startPreview();
39 | } catch (IOException e) {
40 | Log.d(TAG, "Error setting camera preview: " + e.getMessage());
41 | }
42 | }
43 |
44 | @Override
45 | public void surfaceDestroyed(SurfaceHolder holder) {
46 | // empty. Take care of releasing the Camera preview in your activity.
47 | }
48 |
49 | @Override
50 | public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
51 | if (mCamera == null||mHolder.getSurface() == null){
52 | // preview surface does not exist
53 | return;
54 | }
55 |
56 | // stop preview before making changes
57 | try {
58 | mCamera.stopPreview();
59 | } catch (Exception e){
60 | // ignore: tried to stop a non-existent preview
61 | }
62 |
63 | // set preview size and make any resize, rotate or
64 | // reformatting changes here
65 |
66 | // start preview with new settings
67 | try {
68 | mCamera.setPreviewDisplay(mHolder);
69 | mCamera.setDisplayOrientation(90);
70 | Camera.Parameters parameters=mCamera.getParameters();
71 | parameters.set("orientation", "portrait");
72 | parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
73 | mCamera.setParameters(parameters);
74 | mCamera.startPreview();
75 |
76 | } catch (Exception e){
77 | Log.d(TAG, "Error starting camera preview: " + e.getMessage());
78 | }
79 | }
80 |
81 | public void setFlashMode(String mode) {
82 | flashMode = mode;
83 | }
84 |
85 | public void refreshCamera(Camera camera) {
86 | if (mHolder.getSurface() == null) {
87 | // preview surface does not exist
88 | return;
89 | }
90 | // stop preview before making changes
91 | try {
92 | mCamera.stopPreview();
93 | } catch (Exception e) {
94 | // ignore: tried to stop a non-existent preview
95 | }
96 | // set preview size and make any resize, rotate or
97 | // reformatting changes here
98 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
99 | if (camera != null) {
100 | camera.setDisplayOrientation(90);
101 | }
102 | }
103 |
104 | // start preview with new settings
105 | setCamera(camera);
106 | try {
107 | mCamera.setPreviewDisplay(mHolder);
108 |
109 |
110 | Camera.Parameters parameters = mCamera.getParameters();
111 | parameters.setFlashMode(flashMode);
112 | mCamera.setParameters(parameters);
113 |
114 | mCamera.startPreview();
115 | } catch (Exception e) {
116 | Log.d(VIEW_LOG_TAG, "Error starting camera preview: " + e.getMessage());
117 | }
118 | }
119 |
120 | public void setCamera(Camera camera) {
121 | //method to set a camera instance
122 | mCamera = camera;
123 |
124 | }
125 | }
126 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/VideoInputActivity.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary;
2 |
3 | import android.app.Activity;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.content.pm.ActivityInfo;
7 | import android.content.pm.PackageManager;
8 | import android.content.res.Configuration;
9 | import android.graphics.Rect;
10 | import android.hardware.Camera;
11 | import android.media.CamcorderProfile;
12 | import android.media.MediaRecorder;
13 | import android.os.Bundle;
14 | import android.os.Environment;
15 | import android.os.SystemClock;
16 | import android.support.v4.app.ActivityCompat;
17 | import android.support.v7.app.AppCompatActivity;
18 | import android.util.Log;
19 | import android.view.MotionEvent;
20 | import android.view.View;
21 | import android.widget.Chronometer;
22 | import android.widget.ImageView;
23 | import android.widget.LinearLayout;
24 | import android.widget.Toast;
25 |
26 | import java.io.File;
27 | import java.io.IOException;
28 | import java.text.SimpleDateFormat;
29 | import java.util.ArrayList;
30 | import java.util.Date;
31 | import java.util.List;
32 |
33 | public class VideoInputActivity extends AppCompatActivity {
34 | private CameraPreview mPreview;
35 | private Camera mCamera;
36 | private MediaRecorder mediaRecorder;
37 | private String url_file;
38 | private static boolean flash = false;
39 | private static boolean cameraFront = false;
40 | private long countUp;
41 | private int quality = CamcorderProfile.QUALITY_480P;
42 |
43 | private static final int FOCUS_AREA_SIZE = 500;
44 |
45 | String TAG="VideoInputActivity";
46 |
47 |
48 |
49 | public static final String INTENT_EXTRA_VIDEO_PATH = "intent_extra_video_path";//录制的视频路径
50 | public static final int RESULT_CODE_FOR_RECORD_VIDEO_FAILED = 3;//视频录制出错
51 |
52 | public static int Q480 = CamcorderProfile.QUALITY_480P;
53 | public static int Q720 = CamcorderProfile.QUALITY_720P;
54 | public static int Q1080 = CamcorderProfile.QUALITY_1080P;
55 | public static int Q21600 = CamcorderProfile.QUALITY_2160P;
56 |
57 | public static void startActivityForResult(Activity activity, int requestCode,int quality) {
58 | Intent intent = new Intent(activity, VideoInputActivity.class);
59 | intent.putExtra("quality",quality);
60 | ActivityCompat.startActivityForResult(activity, intent, requestCode, null);
61 | }
62 |
63 | @Override
64 | protected void onCreate(Bundle savedInstanceState) {
65 | super.onCreate(savedInstanceState);
66 | setContentView(R.layout.activity_video_input);
67 | quality = getIntent().getIntExtra("quality",Q480);
68 | initialize();
69 | }
70 | ImageView button_ChangeCamera;
71 | LinearLayout cameraPreview;
72 | ImageView buttonCapture ;
73 | ImageView buttonFlash ;
74 | Chronometer textChrono;
75 | ImageView chronoRecordingImage;
76 | //点击对焦
77 | public void initialize() {
78 | button_ChangeCamera = (ImageView) findViewById(R.id.button_ChangeCamera);
79 | cameraPreview = (LinearLayout) findViewById(R.id.camera_preview);
80 | buttonCapture = (ImageView) findViewById(R.id.button_capture);
81 | buttonFlash= (ImageView) findViewById(R.id.buttonFlash);
82 | chronoRecordingImage= (ImageView) findViewById(R.id.chronoRecordingImage);
83 | textChrono= (Chronometer) findViewById(R.id.textChrono);
84 | buttonFlash.setOnClickListener(flashListener);
85 | mPreview = new CameraPreview(VideoInputActivity.this, mCamera);
86 | cameraPreview.addView(mPreview);
87 | buttonCapture.setOnClickListener(captrureListener);
88 | button_ChangeCamera.setOnClickListener(switchCameraListener);
89 | cameraPreview.setOnTouchListener(new View.OnTouchListener() {
90 | @Override
91 | public boolean onTouch(View v, MotionEvent event) {
92 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
93 | try {
94 | focusOnTouch(event);
95 | } catch (Exception e) {
96 | Log.i(TAG, getString(R.string.fail_when_camera_try_autofocus, e.toString()));
97 | //do nothing
98 | }
99 | }
100 | return true;
101 | }
102 | });
103 |
104 | }
105 |
106 | private void focusOnTouch(MotionEvent event) {
107 | if (mCamera != null) {
108 | Camera.Parameters parameters = mCamera.getParameters();
109 | if (parameters.getMaxNumMeteringAreas() > 0) {
110 | Rect rect = calculateFocusArea(event.getX(), event.getY());
111 | parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
112 | List meteringAreas = new ArrayList();
113 | meteringAreas.add(new Camera.Area(rect, 800));
114 | parameters.setFocusAreas(meteringAreas);
115 | mCamera.setParameters(parameters);
116 | mCamera.autoFocus(mAutoFocusTakePictureCallback);
117 | } else {
118 | mCamera.autoFocus(mAutoFocusTakePictureCallback);
119 | }
120 | }
121 | }
122 |
123 | private Rect calculateFocusArea(float x, float y) {
124 | int left = clamp(Float.valueOf((x / mPreview.getWidth()) * 2000 - 1000).intValue(), FOCUS_AREA_SIZE);
125 | int top = clamp(Float.valueOf((y / mPreview.getHeight()) * 2000 - 1000).intValue(), FOCUS_AREA_SIZE);
126 | return new Rect(left, top, left + FOCUS_AREA_SIZE, top + FOCUS_AREA_SIZE);
127 | }
128 |
129 |
130 |
131 | private int clamp(int touchCoordinateInCameraReper, int focusAreaSize) {
132 | int result;
133 | if (Math.abs(touchCoordinateInCameraReper) + focusAreaSize / 2 > 1000) {
134 | if (touchCoordinateInCameraReper > 0) {
135 | result = 1000 - focusAreaSize / 2;
136 | } else {
137 | result = -1000 + focusAreaSize / 2;
138 | }
139 | } else {
140 | result = touchCoordinateInCameraReper - focusAreaSize / 2;
141 | }
142 | return result;
143 | }
144 |
145 | private Camera.AutoFocusCallback mAutoFocusTakePictureCallback = new Camera.AutoFocusCallback() {
146 | @Override
147 | public void onAutoFocus(boolean success, Camera camera) {
148 | if (success) {
149 | // do something...
150 | Log.i("tap_to_focus", "success!");
151 | } else {
152 | // do something...
153 | Log.i("tap_to_focus", "fail!");
154 | }
155 | }
156 | };
157 |
158 | public void onResume() {
159 | super.onResume();
160 | if (!hasCamera(getApplicationContext())) {
161 | //这台设备没有发现摄像头
162 | Toast.makeText(getApplicationContext(), R.string.dont_have_camera_error
163 | , Toast.LENGTH_SHORT).show();
164 | setResult(RESULT_CANCELED);
165 | releaseCamera();
166 | releaseMediaRecorder();
167 | finish();
168 | }
169 | if (mCamera == null) {
170 | releaseCamera();
171 | final boolean frontal = cameraFront;
172 |
173 | int cameraId = findFrontFacingCamera();
174 | if (cameraId < 0) {
175 | //前置摄像头不存在
176 | switchCameraListener = new View.OnClickListener() {
177 | @Override
178 | public void onClick(View v) {
179 | Toast.makeText(VideoInputActivity.this, R.string.dont_have_front_camera, Toast.LENGTH_SHORT).show();
180 | }
181 | };
182 |
183 | //尝试寻找后置摄像头
184 | cameraId = findBackFacingCamera();
185 | if (flash) {
186 | mPreview.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
187 | buttonFlash.setImageResource(R.mipmap.ic_flash_on_white);
188 | }
189 | } else if (!frontal) {
190 | cameraId = findBackFacingCamera();
191 | if (flash) {
192 | mPreview.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
193 | buttonFlash.setImageResource(R.mipmap.ic_flash_on_white);
194 | }
195 | }
196 |
197 | mCamera = Camera.open(cameraId);
198 | mPreview.refreshCamera(mCamera);
199 |
200 |
201 | }
202 | }
203 | //计时器
204 | private void startChronometer() {
205 | textChrono.setVisibility(View.VISIBLE);
206 | final long startTime = SystemClock.elapsedRealtime();
207 | textChrono.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
208 | @Override
209 | public void onChronometerTick(Chronometer arg0) {
210 | countUp = (SystemClock.elapsedRealtime() - startTime) / 1000;
211 | if (countUp % 2 == 0) {
212 | chronoRecordingImage.setVisibility(View.VISIBLE);
213 | } else {
214 | chronoRecordingImage.setVisibility(View.INVISIBLE);
215 | }
216 |
217 | String asText = String.format("%02d", countUp / 60) + ":" + String.format("%02d", countUp % 60);
218 | textChrono.setText(asText);
219 | }
220 | });
221 | textChrono.start();
222 | }
223 |
224 | /**
225 | * 找前置摄像头,没有则返回-1
226 | *
227 | * @return cameraId
228 | */
229 | private int findFrontFacingCamera() {
230 | int cameraId = -1;
231 | //获取摄像头个数
232 | int numberOfCameras = Camera.getNumberOfCameras();
233 | for (int i = 0; i < numberOfCameras; i++) {
234 | Camera.CameraInfo info = new Camera.CameraInfo();
235 | Camera.getCameraInfo(i, info);
236 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
237 | cameraId = i;
238 | cameraFront = true;
239 | break;
240 | }
241 | }
242 | return cameraId;
243 | }
244 |
245 | /**
246 | * 找后置摄像头,没有则返回-1
247 | *
248 | * @return cameraId
249 | */
250 | private int findBackFacingCamera() {
251 | int cameraId = -1;
252 | //获取摄像头个数
253 | int numberOfCameras = Camera.getNumberOfCameras();
254 | for (int i = 0; i < numberOfCameras; i++) {
255 | Camera.CameraInfo info = new Camera.CameraInfo();
256 | Camera.getCameraInfo(i, info);
257 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
258 | cameraId = i;
259 | cameraFront = false;
260 | break;
261 | }
262 | }
263 | return cameraId;
264 | }
265 |
266 |
267 |
268 | //检查设备是否有摄像头
269 | private boolean hasCamera(Context context) {
270 | if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
271 | return true;
272 | } else {
273 | return false;
274 | }
275 | }
276 |
277 | private void releaseCamera() {
278 | if (mCamera != null) {
279 | mCamera.release();
280 | mCamera = null;
281 | }
282 | }
283 |
284 | private void releaseMediaRecorder() {
285 | if (mediaRecorder != null) {
286 | mediaRecorder.reset();
287 | mediaRecorder.release();
288 | mediaRecorder = null;
289 | mCamera.lock();
290 | }
291 | }
292 |
293 |
294 | private boolean prepareMediaRecorder() {
295 | mediaRecorder = new MediaRecorder();
296 | mCamera.unlock();
297 | mediaRecorder.setCamera(mCamera);
298 | mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
299 | mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
300 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
301 | if (cameraFront) {
302 | mediaRecorder.setOrientationHint(270);
303 | } else {
304 | mediaRecorder.setOrientationHint(90);
305 | }
306 | }
307 |
308 | mediaRecorder.setProfile(CamcorderProfile.get(quality));
309 |
310 | File file1 = getOutputMediaFile();
311 | if (file1.exists()) {
312 | file1.delete();
313 | }
314 | // File file = new File("/mnt/sdcard/videokit");
315 | // if (!file.exists()) {
316 | // file.mkdirs();
317 | // }
318 | // Date d = new Date();
319 | // String timestamp = String.valueOf(d.getTime());
320 |
321 | // url_file = "/mnt/sdcard/videokit/in.mp4";
322 |
323 | //
324 | // File file1 = new File(url_file);
325 | // if (file1.exists()) {
326 | // file1.delete();
327 | // }
328 |
329 | mediaRecorder.setOutputFile(file1.toString());
330 | try {
331 | mediaRecorder.prepare();
332 | } catch (IllegalStateException e) {
333 | e.printStackTrace();
334 | releaseMediaRecorder();
335 | return false;
336 | } catch (IOException e) {
337 | e.printStackTrace();
338 | releaseMediaRecorder();
339 | return false;
340 | }
341 | return true;
342 |
343 | }
344 |
345 | private void stopChronometer() {
346 | textChrono.stop();
347 | chronoRecordingImage.setVisibility(View.INVISIBLE);
348 | textChrono.setVisibility(View.INVISIBLE);
349 | }
350 |
351 | boolean recording = false;
352 | //切换前置后置摄像头
353 | View.OnClickListener switchCameraListener = new View.OnClickListener() {
354 | @Override
355 | public void onClick(View v) {
356 | if (!recording) {
357 | int camerasNumber = Camera.getNumberOfCameras();
358 | if (camerasNumber > 1) {
359 | releaseCamera();
360 | chooseCamera();
361 | } else {
362 | //只有一个摄像头不允许切换
363 | Toast.makeText(getApplicationContext(), R.string.only_have_one_camera
364 | , Toast.LENGTH_SHORT).show();
365 | }
366 | }
367 | }
368 | };
369 |
370 | View.OnClickListener captrureListener = new View.OnClickListener() {
371 | @Override
372 | public void onClick(View v) {
373 | if (recording) {
374 | //如果正在录制点击这个按钮表示录制完成
375 | mediaRecorder.stop(); //停止
376 | stopChronometer();
377 | buttonCapture.setImageResource(R.mipmap.player_record);
378 | changeRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
379 | releaseMediaRecorder();
380 | Toast.makeText(VideoInputActivity.this, R.string.video_captured, Toast.LENGTH_SHORT).show();
381 | recording = false;
382 | Intent intent = new Intent();
383 | intent.putExtra(INTENT_EXTRA_VIDEO_PATH, url_file);
384 | setResult(RESULT_OK, intent);
385 | releaseCamera();
386 | releaseMediaRecorder();
387 | finish();
388 | } else {
389 | //准备开始录制视频
390 | if (!prepareMediaRecorder()) {
391 | Toast.makeText(VideoInputActivity.this, getString(R.string.camera_init_fail), Toast.LENGTH_SHORT).show();
392 | setResult(RESULT_CODE_FOR_RECORD_VIDEO_FAILED);
393 | releaseCamera();
394 | releaseMediaRecorder();
395 | finish();
396 | }
397 | //开始录制视频
398 | runOnUiThread(new Runnable() {
399 | public void run() {
400 | // If there are stories, add them to the table
401 | try {
402 | mediaRecorder.start();
403 | startChronometer();
404 | if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
405 | changeRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
406 | } else {
407 | changeRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
408 | }
409 | buttonCapture.setImageResource(R.mipmap.player_stop);
410 | } catch (final Exception ex) {
411 | Log.i("---", "Exception in thread");
412 | setResult(RESULT_CODE_FOR_RECORD_VIDEO_FAILED);
413 | releaseCamera();
414 | releaseMediaRecorder();
415 | finish();
416 | }
417 | }
418 | });
419 | recording = true;
420 | }
421 | }
422 | };
423 | private void changeRequestedOrientation(int orientation) {
424 | setRequestedOrientation(orientation);
425 | }
426 | //闪光灯
427 | View.OnClickListener flashListener = new View.OnClickListener() {
428 | @Override
429 | public void onClick(View v) {
430 | if (!recording && !cameraFront) {
431 | if (flash) {
432 | flash = false;
433 | buttonFlash.setImageResource(R.mipmap.ic_flash_off_white);
434 | setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
435 | } else {
436 | flash = true;
437 | buttonFlash.setImageResource(R.mipmap.ic_flash_on_white);
438 | setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
439 | }
440 | }
441 | }
442 | };
443 |
444 | //选择摄像头
445 | public void chooseCamera() {
446 | if (cameraFront) {
447 | //当前是前置摄像头
448 | int cameraId = findBackFacingCamera();
449 | if (cameraId >= 0) {
450 | // open the backFacingCamera
451 | // set a picture callback
452 | // refresh the preview
453 | mCamera = Camera.open(cameraId);
454 | // mPicture = getPictureCallback();
455 | mPreview.refreshCamera(mCamera);
456 |
457 | }
458 | } else {
459 | //当前为后置摄像头
460 | int cameraId = findFrontFacingCamera();
461 | if (cameraId >= 0) {
462 | // open the backFacingCamera
463 | // set a picture callback
464 | // refresh the preview
465 | mCamera = Camera.open(cameraId);
466 | if (flash) {
467 | flash = false;
468 | buttonFlash.setImageResource(R.mipmap.ic_flash_off_white);
469 | mPreview.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
470 | }
471 | // mPicture = getPictureCallback();
472 | mPreview.refreshCamera(mCamera);
473 |
474 | }
475 | }
476 | }
477 |
478 | //闪光灯
479 | public void setFlashMode(String mode) {
480 | try {
481 | if (getPackageManager().hasSystemFeature(
482 | PackageManager.FEATURE_CAMERA_FLASH)
483 | && mCamera != null
484 | && !cameraFront) {
485 |
486 | mPreview.setFlashMode(mode);
487 | mPreview.refreshCamera(mCamera);
488 |
489 | }
490 | } catch (Exception e) {
491 | e.printStackTrace();
492 | Toast.makeText(getApplicationContext(), R.string.changing_flashLight_mode,
493 | Toast.LENGTH_SHORT).show();
494 | }
495 | }
496 |
497 |
498 | /** Create a File for saving an image or video */
499 | private File getOutputMediaFile(){
500 |
501 | // return new File(getContext().getExternalCacheDir().getAbsolutePath() + "/" + fileName);
502 | String appName = getPackageName();
503 | File dir = new File(Environment.getExternalStorageDirectory() + "/" +appName);
504 | if (!dir.exists()){
505 | dir.mkdir();
506 | }
507 | url_file = dir+ "/video_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4";
508 | Log.i("filePath",url_file);
509 | return new File(url_file);
510 | }
511 | }
512 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/VideoInputDialog.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary;
2 |
3 | import android.content.Context;
4 | import android.content.pm.PackageManager;
5 | import android.hardware.Camera;
6 | import android.media.CamcorderProfile;
7 | import android.media.MediaRecorder;
8 | import android.os.Bundle;
9 | import android.os.Environment;
10 | import android.os.Handler;
11 | import android.os.Looper;
12 | import android.support.v4.app.DialogFragment;
13 | import android.support.v4.app.FragmentManager;
14 | import android.util.Log;
15 | import android.view.LayoutInflater;
16 | import android.view.MotionEvent;
17 | import android.view.View;
18 | import android.view.ViewGroup;
19 | import android.widget.FrameLayout;
20 | import android.widget.ImageButton;
21 | import android.widget.ProgressBar;
22 | import android.widget.Toast;
23 |
24 | import java.io.File;
25 | import java.io.IOException;
26 | import java.text.SimpleDateFormat;
27 | import java.util.Calendar;
28 | import java.util.Date;
29 | import java.util.Timer;
30 | import java.util.TimerTask;
31 |
32 |
33 | /**
34 | * Created by zhush on 2016/11/11
35 | * E-mail zhush@jerei.com
36 | * PS 小视频输入控件
37 | */
38 |
39 | public class VideoInputDialog extends DialogFragment {
40 |
41 | private static final String TAG = "VideoInputDialog";
42 | private Camera mCamera;
43 | private CameraPreview mPreview;
44 | private ProgressBar mProgressRight,mProgressLeft;
45 | private MediaRecorder mMediaRecorder;
46 | private Timer mTimer;
47 | private final int MAX_TIME = 1500;
48 | private int mTimeCount;
49 | private long time;
50 | private boolean isRecording = false;
51 | private String fileName;
52 | private VideoCall videoCall;
53 |
54 | public static int Q480 = CamcorderProfile.QUALITY_480P;
55 | public static int Q720 = CamcorderProfile.QUALITY_720P;
56 | public static int Q1080 = CamcorderProfile.QUALITY_1080P;
57 | public static int Q21600 = CamcorderProfile.QUALITY_2160P;
58 | private int quality =CamcorderProfile.QUALITY_480P;
59 |
60 | Context mContext;
61 |
62 | private Handler mainHandler = new Handler(Looper.getMainLooper());
63 | private Runnable updateProgress = new Runnable() {
64 | @Override
65 | public void run() {
66 | mProgressRight.setProgress(mTimeCount);
67 | mProgressLeft.setProgress(mTimeCount);
68 | }
69 | };
70 | private Runnable sendVideo = new Runnable() {
71 | @Override
72 | public void run() {
73 | recordStop();
74 | }
75 | };
76 |
77 | public void setVideoCall(VideoCall videoCall) {
78 | this.videoCall = videoCall;
79 | }
80 |
81 | public static VideoInputDialog newInstance(VideoCall videoCall,int quality,Context context) {
82 | VideoInputDialog dialog = new VideoInputDialog();
83 | dialog.setVideoCall(videoCall);
84 | dialog.setQuality(quality);
85 | dialog.setmContext(context);
86 | dialog.setStyle(DialogFragment.STYLE_NORMAL, R.style.maskDialog);
87 | return dialog;
88 | }
89 |
90 | @Override
91 | public View onCreateView(LayoutInflater inflater, ViewGroup container,
92 | Bundle savedInstanceState) {
93 | View v = inflater.inflate(R.layout.dialog_video_input, container, false);
94 | //打开相机
95 | mCamera = getCameraInstance();
96 | mPreview = new CameraPreview(getActivity(), mCamera);
97 | FrameLayout preview = (FrameLayout) v.findViewById(R.id.camera_preview);
98 | mProgressRight = (ProgressBar) v.findViewById(R.id.progress_right);
99 | mProgressLeft = (ProgressBar) v.findViewById(R.id.progress_left);
100 | mProgressRight.setMax(MAX_TIME);
101 | mProgressLeft.setMax(MAX_TIME);
102 | mProgressLeft.setRotation(180);
103 | ImageButton record = (ImageButton) v.findViewById(R.id.btn_record);
104 | record.setOnTouchListener(new View.OnTouchListener() {
105 | @Override
106 | public boolean onTouch(View v, MotionEvent event) {
107 | switch (event.getAction()) {
108 | case MotionEvent.ACTION_DOWN:
109 | //按下 开始录像
110 | if (!isRecording) {
111 | if (prepareVideoRecorder()) {
112 | time = Calendar.getInstance().getTimeInMillis(); //倒计时
113 | mMediaRecorder.start();
114 | isRecording = true;
115 | mTimer = new Timer();
116 | mTimer.schedule(new TimerTask() {
117 | @Override
118 | public void run() {
119 | mTimeCount++;
120 | mainHandler.post(updateProgress);
121 | if (mTimeCount == MAX_TIME) {
122 | mainHandler.post(sendVideo);
123 | }
124 | }
125 | }, 0, 10);
126 | } else {
127 | releaseMediaRecorder();
128 | }
129 | }
130 | break;
131 | case MotionEvent.ACTION_UP:
132 | //抬起 停止录像
133 | recordStop();
134 | break;
135 | }
136 | return true;
137 | }
138 | });
139 | preview.addView(mPreview);
140 | return v;
141 | }
142 |
143 | @Override
144 | public void onPause() {
145 | super.onPause();
146 | recordStop();
147 | releaseMediaRecorder();
148 | releaseCamera();
149 | }
150 |
151 | /**
152 | * 停止录制
153 | */
154 | private void recordStop(){
155 | if (isRecording) {
156 | isRecording = false;
157 | if (isLongEnough()){
158 | mMediaRecorder.stop();
159 | }
160 | releaseMediaRecorder();
161 | mCamera.lock();
162 | if (mTimer != null) mTimer.cancel();
163 | mTimeCount = 0;
164 | mainHandler.post(updateProgress);
165 |
166 | }
167 | }
168 |
169 |
170 | /**
171 | *
172 | * @param ft
173 | * @param videoCall 录制视频回调
174 | * @param quality 分辨率
175 | * @param context
176 | */
177 | public static void show(FragmentManager ft,VideoCall videoCall,int quality,Context context){
178 |
179 | DialogFragment newFragment = VideoInputDialog.newInstance(videoCall,quality, context);
180 | newFragment.show(ft, "VideoInputDialog");
181 | }
182 |
183 |
184 |
185 | /** A safe way to get an instance of the Camera object. */
186 | private static Camera getCameraInstance(){
187 | Camera c = null;
188 | try {
189 | c = Camera.open();
190 | }
191 | catch (Exception e){
192 | // Camera is not available (in use or does not exist)
193 | }
194 | return c; // returns null if camera is unavailable
195 | }
196 |
197 |
198 |
199 | private void releaseMediaRecorder(){
200 | if (mMediaRecorder != null) {
201 | mMediaRecorder.reset(); // clear recorder configuration
202 | mMediaRecorder.release(); // release the recorder object
203 | mMediaRecorder = null;
204 | mCamera.lock(); // lock camera for later use
205 | if (isLongEnough()){
206 | videoCall.videoPathCall(fileName);
207 | }else{
208 | Toast.makeText(getContext(), getString(R.string.chat_video_too_short), Toast.LENGTH_SHORT).show();
209 | }
210 | dismiss();
211 | }
212 | }
213 |
214 | private void releaseCamera(){
215 | if (mCamera != null){
216 | mCamera.release(); // release the camera for other applications
217 | mCamera = null;
218 | }
219 | }
220 | //初始化 mMediaRecorder 用于录像
221 | private boolean prepareVideoRecorder(){
222 |
223 | if (mCamera==null) return false;
224 | mMediaRecorder = new MediaRecorder();
225 | mCamera.unlock();
226 | mMediaRecorder.setCamera(mCamera);
227 | //声音
228 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
229 | //视频
230 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
231 | //设置分辨率为480P
232 | mMediaRecorder.setProfile(CamcorderProfile.get(quality));
233 | //路径
234 | mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
235 | mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());
236 |
237 |
238 | try {
239 | //旋转90度 保持竖屏
240 | mMediaRecorder.setOrientationHint(90);
241 | mMediaRecorder.prepare();
242 | } catch (IllegalStateException e) {
243 | Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
244 | releaseMediaRecorder();
245 | return false;
246 | } catch (IOException e) {
247 | Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
248 | releaseMediaRecorder();
249 | return false;
250 | }
251 | return true;
252 | }
253 |
254 |
255 |
256 | /** Create a File for saving an image or video */
257 | private File getOutputMediaFile(){
258 |
259 | // return new File(getContext().getExternalCacheDir().getAbsolutePath() + "/" + fileName);
260 | // PackageManager pm = mContext.getPackageManager();
261 | String appName = mContext. getPackageName();
262 | File dir = new File(Environment.getExternalStorageDirectory() + "/" + appName);
263 | if (!dir.exists()){
264 | dir.mkdir();
265 | }
266 | fileName = dir+ "/video_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".mp4";
267 | Log.i("filePath",fileName);
268 | return new File(fileName);
269 | }
270 |
271 | /**
272 | * 判断录制时间
273 | * @return
274 | */
275 | private boolean isLongEnough(){
276 | return Calendar.getInstance().getTimeInMillis() - time > 3000;
277 | }
278 |
279 | /**
280 | * Created by zhush on 2016/11/11
281 | * E-mail zhush@jerei.com
282 | * PS 录制视频回调
283 | */
284 |
285 | public static interface VideoCall{
286 | public void videoPathCall(String path);
287 | }
288 |
289 | public void setQuality(int quality) {
290 | this.quality = quality;
291 | }
292 |
293 | public void setmContext(Context mContext) {
294 | this.mContext = mContext;
295 | }
296 | }
297 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/compression/CompressListener.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary.compression;
2 |
3 | /**
4 | * Created by karan on 13/2/15.
5 | */
6 | public interface CompressListener {
7 | public void onExecSuccess(String message);
8 | public void onExecFail(String reason);
9 | public void onExecProgress(String message);
10 | }
11 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/compression/Compressor.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary.compression;
2 |
3 | import android.app.Activity;
4 | import android.util.Log;
5 |
6 | import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
7 | import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
8 | import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler;
9 | import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
10 | import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;
11 |
12 |
13 | /**
14 | * Created by karan on 13/2/15.
15 | */
16 | public class Compressor {
17 |
18 | public Activity a;
19 | public FFmpeg ffmpeg;
20 | public Compressor(Activity activity){
21 | a = activity;
22 | ffmpeg = FFmpeg.getInstance(a);
23 | }
24 |
25 | public void loadBinary(final InitListener mListener) {
26 | try {
27 | ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
28 | @Override
29 | public void onStart() {
30 | Log.e("","");
31 | }
32 |
33 | @Override
34 | public void onFailure() {
35 | mListener.onLoadFail("incompatible with this device");
36 | }
37 |
38 | @Override
39 | public void onSuccess() {
40 | mListener.onLoadSuccess();
41 | }
42 | @Override
43 | public void onFinish() {
44 | Log.e("","");
45 | }
46 | });
47 | } catch (FFmpegNotSupportedException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | public void execCommand(String cmd,final CompressListener mListener){
53 | try {
54 | ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {
55 |
56 | @Override
57 | public void onStart() {}
58 |
59 | @Override
60 | public void onProgress(String message) { mListener.onExecProgress(message);}
61 |
62 | @Override
63 | public void onFailure(String message) { mListener.onExecFail(message); }
64 |
65 | @Override
66 | public void onSuccess(String message) {
67 | mListener.onExecSuccess(message);
68 | }
69 |
70 | @Override
71 | public void onFinish() {}
72 | });
73 | } catch (FFmpegCommandAlreadyRunningException e) {
74 | e.printStackTrace();
75 | }
76 | }
77 |
78 |
79 |
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/compression/CompressorUtils.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary.compression;
2 |
3 | import android.app.Activity;
4 | import android.os.Build;
5 | import android.util.Log;
6 |
7 | import java.io.File;
8 |
9 | /**
10 | * Created by zhush on 2017/1/14.
11 | * E-mail zhush@jerei.com
12 | * PS
13 | */
14 | public class CompressorUtils {
15 |
16 | //视频 输入地址
17 | private String currentInputVideoPath = "";
18 | //视频数据地址
19 | private String currentOutputVideoPath="";
20 |
21 | static String TAG="CompressorUtils";
22 |
23 | Compressor mCompressor;
24 | Activity activity;
25 | public CompressorUtils(String currentInputVideoPath, String currentOutputVideoPath,final Activity activity) {
26 | this.currentInputVideoPath = currentInputVideoPath;
27 | this.currentOutputVideoPath = currentOutputVideoPath;
28 | this.activity = activity;
29 | mCompressor = new Compressor(activity);
30 | mCompressor.loadBinary(new InitListener() {
31 | @Override
32 | public void onLoadSuccess() {
33 | Log.e(TAG, "load library succeed");
34 |
35 | }
36 | @Override
37 | public void onLoadFail(String reason) {
38 | Log.e(TAG, "load library fail:" + reason);
39 | }
40 | });
41 | }
42 |
43 |
44 | /**
45 | * 压缩
46 | */
47 | public void execCommand(CompressListener compressListener) {
48 |
49 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
50 | compressListener.onExecFail("暂时不支持android7.0以上版本");
51 | return;
52 | }
53 |
54 | String command ="-y -i " + currentInputVideoPath + " -strict -2 -vcodec libx264 -preset ultrafast " +
55 | "-crf 24 -acodec aac -ar 44100 -ac 2 -b:a 96k -s 640x480 -aspect 16:9 " + currentOutputVideoPath;;
56 |
57 | File mFile = new File(currentOutputVideoPath);
58 | if (mFile.exists()) {
59 | mFile.delete();
60 | }
61 | mCompressor.execCommand(command,compressListener);
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/java/com/sh/shvideolibrary/compression/InitListener.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary.compression;
2 |
3 | /**
4 | * Created by karan on 13/2/15.
5 | */
6 | public interface InitListener {
7 | public void onLoadSuccess();
8 | public void onLoadFail(String reason);
9 | }
10 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/drawable/btn_video.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/drawable/btn_video.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/drawable/btn_video_hover.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/drawable/btn_video_hover.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/drawable/btn_video_record.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/drawable/style_recorder_progress.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | -
4 |
5 |
7 |
8 |
9 | -
11 |
12 |
13 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/layout/activity_video_input.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
12 |
13 |
24 |
34 |
35 |
36 |
37 |
38 |
51 |
52 |
58 |
59 |
60 |
68 |
69 |
70 |
71 |
77 |
85 |
86 |
87 |
93 |
101 |
102 |
103 |
104 |
105 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/layout/dialog_video_input.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
8 |
9 |
13 |
14 |
15 |
21 |
22 |
23 |
27 |
34 |
41 |
42 |
46 |
52 |
53 |
54 |
55 |
56 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_camera_menu_switch.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_camera_menu_switch.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_flash_off_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_flash_off_white.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_flash_on_white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_flash_on_white.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/player_record.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/player_record.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/mipmap-xxxhdpi/player_stop.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/hui46226021/ShVideoDemo/65aa06fcf153371d2e36e2589110fdd7519baba4/shvideolibrary/src/main/res/mipmap-xxxhdpi/player_stop.png
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | #25272A
6 | #1173CB
7 |
8 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ShVideoLibrary
3 | 录制过短,请重试
4 |
5 | 加载库成功
6 | 加载库失败,原因:%1$s
7 | 压缩成功
8 | 压缩失败,原因: %1$s
9 | 进度: %1$s
10 | 请输入命令
11 |
12 |
13 | 录制视频
14 | 运行压缩命令
15 | 视频文件不存在,请先录制视频
16 | 对焦失败:%1$s
17 | 对不起,没有发现摄像头,请检查
18 | 对不起,您只有一个摄像头,无法切换
19 | 视频已成功录制
20 | 摄像头初始化失败
21 | 当前设备没有前置摄像头
22 | 切换闪光灯模式失败
23 | 打开视频
24 | 确定
25 | 取消
26 | 输入:%1$s,大小:%2$s,输出:%3$s,大小:%4$s
27 | 视频路径:%1$s,大小:%2$s
28 | 没有支持此格式的播放器
29 |
30 |
--------------------------------------------------------------------------------
/shvideolibrary/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
7 |
8 |
--------------------------------------------------------------------------------
/shvideolibrary/src/test/java/com/sh/shvideolibrary/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.sh.shvideolibrary;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------