├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── markdown-navigator │ └── profiles_settings.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── app ├── .gitignore ├── build.gradle ├── libs │ └── fmod.jar ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── aserbao │ │ └── cameravideorecord │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── aserbao │ │ │ └── cameravideorecord │ │ │ ├── MainActivity.java │ │ │ ├── cameraDemo01 │ │ │ ├── CameraTakePictureActivity.java │ │ │ └── RecordingActivity.java │ │ │ └── utils │ │ │ ├── CameraUtils.java │ │ │ └── MediaUtils.java │ └── res │ │ ├── drawable │ │ ├── message_vioce01.png │ │ ├── message_vioce02.png │ │ ├── message_vioce03.png │ │ ├── message_vioce04.png │ │ ├── message_vioce05.png │ │ ├── message_vioce06.png │ │ ├── message_vioce07.png │ │ └── message_vioce08.png │ │ ├── layout │ │ ├── activity_camera_take_picture.xml │ │ ├── activity_main.xml │ │ └── activity_recording.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── aserbao │ └── cameravideorecord │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.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 | 17 | 18 | -------------------------------------------------------------------------------- /.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 | 39 | 40 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 88 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 115 | 116 | 117 | 118 | 119 | 1.8 120 | 121 | 126 | 127 | 128 | 129 | 130 | 131 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'com.neenbedankt.android-apt' 3 | 4 | android { 5 | compileSdkVersion 26 6 | buildToolsVersion "26.0.0" 7 | defaultConfig { 8 | applicationId "com.aserbao.cameravideorecord" 9 | minSdkVersion 15 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 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:26.+' 29 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 30 | compile 'com.jakewharton:butterknife:8.4.0' 31 | testCompile 'junit:junit:4.12' 32 | apt 'com.jakewharton:butterknife-compiler:8.4.0' 33 | compile files('libs/fmod.jar') 34 | } 35 | -------------------------------------------------------------------------------- /app/libs/fmod.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/libs/fmod.jar -------------------------------------------------------------------------------- /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:\Android\android-sdk-windows\android-sdk-windows/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 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/aserbao/cameravideorecord/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.aserbao.cameravideorecord; 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.aserbao.cameravideorecord", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/java/com/aserbao/cameravideorecord/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.aserbao.cameravideorecord; 2 | 3 | import android.content.Context; 4 | import android.graphics.SurfaceTexture; 5 | import android.graphics.SurfaceTexture.OnFrameAvailableListener; 6 | import android.hardware.Camera; 7 | import android.opengl.GLSurfaceView; 8 | import android.os.Bundle; 9 | import android.os.Handler; 10 | import android.os.Message; 11 | import android.support.v7.app.AppCompatActivity; 12 | import android.util.Log; 13 | import android.view.View; 14 | import android.widget.Button; 15 | 16 | import com.aserbao.cameravideorecord.utils.CameraUtils; 17 | 18 | import java.io.IOException; 19 | import java.lang.ref.WeakReference; 20 | import java.util.List; 21 | 22 | import javax.microedition.khronos.egl.EGLConfig; 23 | import javax.microedition.khronos.opengles.GL10; 24 | 25 | import butterknife.BindView; 26 | import butterknife.ButterKnife; 27 | import butterknife.OnClick; 28 | 29 | public class MainActivity extends AppCompatActivity implements Camera.PreviewCallback,OnFrameAvailableListener{ 30 | private static final String TAG = "MainActivity"; 31 | 32 | @BindView(R.id.gl_surface_view) 33 | GLSurfaceView mGlSurfaceView; 34 | @BindView(R.id.btn_start) 35 | Button mBtnStart; 36 | @BindView(R.id.btn_stop) 37 | Button mBtnStop; 38 | Context mContext; 39 | private MainHandler mMainHandler; 40 | 41 | int currentCameraType = Camera.CameraInfo.CAMERA_FACING_FRONT; 42 | int cameraWidth = 1280; 43 | int cameraHeight = 720; 44 | Camera mCamera; 45 | boolean isNeedSwitchCameraSurfaceTexture = true; 46 | 47 | static class MainHandler extends Handler { 48 | 49 | static final int HANDLE_CAMERA_START_PREVIEW = 1; 50 | 51 | private WeakReference mActivityWeakReference; 52 | 53 | MainHandler(MainActivity activity) { 54 | mActivityWeakReference = new WeakReference<>(activity); 55 | } 56 | 57 | @Override 58 | public void handleMessage(Message msg) { 59 | super.handleMessage(msg); 60 | MainActivity activity = mActivityWeakReference.get(); 61 | switch (msg.what) { 62 | case HANDLE_CAMERA_START_PREVIEW: 63 | activity.handleCameraStartPreview((SurfaceTexture) msg.obj); 64 | break; 65 | } 66 | } 67 | } 68 | @Override 69 | protected void onCreate(Bundle savedInstanceState) { 70 | super.onCreate(savedInstanceState); 71 | setContentView(R.layout.activity_main); 72 | ButterKnife.bind(this); 73 | mContext = this; 74 | mMainHandler = new MainHandler(this); 75 | mGlSurfaceView.setEGLContextClientVersion(2); 76 | mGlSurfaceView.setRenderer(new GLRenderer()); 77 | mGlSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); 78 | } 79 | 80 | @Override 81 | protected void onResume() { 82 | super.onResume(); 83 | openCamera(currentCameraType, cameraWidth, cameraHeight); 84 | mGlSurfaceView.onResume(); 85 | } 86 | 87 | @OnClick({R.id.btn_start, R.id.btn_stop}) 88 | public void onViewClicked(View view) { 89 | switch (view.getId()) { 90 | case R.id.btn_start: 91 | break; 92 | case R.id.btn_stop: 93 | break; 94 | } 95 | } 96 | 97 | @Override 98 | public void onFrameAvailable(SurfaceTexture surfaceTexture) { 99 | Log.e(TAG, "onFrameAvailable: " ); 100 | } 101 | private void handleCameraStartPreview(SurfaceTexture surfaceTexture) { 102 | mCamera.setPreviewCallbackWithBuffer(this); 103 | try { 104 | mCamera.setPreviewTexture(surfaceTexture); 105 | } catch (IOException e) { 106 | e.printStackTrace(); 107 | } 108 | surfaceTexture.setOnFrameAvailableListener(this); 109 | mCamera.startPreview(); 110 | } 111 | @Override 112 | public void onPointerCaptureChanged(boolean hasCapture) { 113 | Log.e(TAG, "onPointerCaptureChanged: " ); 114 | } 115 | 116 | @Override 117 | public void onPreviewFrame(byte[] data, Camera camera) { 118 | Log.e(TAG, "onPreviewFrame: "); 119 | mCamera.addCallbackBuffer(data); 120 | } 121 | 122 | class GLRenderer implements GLSurfaceView.Renderer{ 123 | SurfaceTexture mCameraSurfaceTexture; 124 | private int mCameraTextureId = 1; 125 | 126 | @Override 127 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { 128 | Log.e(TAG, "onSurfaceCreated: " ); 129 | switchCameraSurfaceTexture(); 130 | } 131 | public void switchCameraSurfaceTexture() { 132 | Log.e(TAG, "switchCameraSurfaceTexture"); 133 | isNeedSwitchCameraSurfaceTexture = false; 134 | if (mCameraSurfaceTexture != null) { 135 | destroySurfaceTexture(); 136 | } 137 | mCameraSurfaceTexture = new SurfaceTexture(mCameraTextureId); 138 | Log.e(TAG, "send start camera message"); 139 | mMainHandler.sendMessage(mMainHandler.obtainMessage( 140 | MainHandler.HANDLE_CAMERA_START_PREVIEW, 141 | mCameraSurfaceTexture)); 142 | } 143 | public void destroySurfaceTexture() { 144 | if (mCameraSurfaceTexture != null) { 145 | mCameraSurfaceTexture.release(); 146 | mCameraSurfaceTexture = null; 147 | } 148 | } 149 | @Override 150 | public void onSurfaceChanged(GL10 gl, int width, int height) { 151 | Log.e(TAG, "onSurfaceChanged: "); 152 | } 153 | 154 | @Override 155 | public void onDrawFrame(GL10 gl) { 156 | Log.e(TAG, "onDrawFrame: "); 157 | } 158 | } 159 | 160 | private void openCamera(int cameraType, int desiredWidth, int desiredHeight) { 161 | Camera.CameraInfo info = new Camera.CameraInfo(); 162 | int cameraId = 0; 163 | int numCameras = Camera.getNumberOfCameras(); 164 | for (int i = 0; i < numCameras; i++) { 165 | Camera.getCameraInfo(i, info); 166 | if (info.facing == cameraType) { 167 | cameraId = i; 168 | mCamera = Camera.open(i); 169 | currentCameraType = cameraType; 170 | break; 171 | } 172 | } 173 | CameraUtils.setCameraDisplayOrientation(this, cameraId, mCamera); 174 | Camera.Parameters parameters = mCamera.getParameters(); 175 | 176 | 177 | List focusModes = parameters.getSupportedFocusModes(); 178 | if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) 179 | parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); 180 | 181 | int[] closetFramerate = CameraUtils.closetFramerate(parameters, 30); 182 | parameters.setPreviewFpsRange(closetFramerate[0], closetFramerate[1]); 183 | 184 | CameraUtils.choosePreviewSize(parameters, desiredWidth, desiredHeight); 185 | mCamera.setParameters(parameters); 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /app/src/main/java/com/aserbao/cameravideorecord/cameraDemo01/CameraTakePictureActivity.java: -------------------------------------------------------------------------------- 1 | package com.aserbao.cameravideorecord.cameraDemo01; 2 | 3 | import android.graphics.Bitmap; 4 | import android.graphics.BitmapFactory; 5 | import android.hardware.Camera; 6 | import android.media.MediaRecorder; 7 | import android.os.Bundle; 8 | import android.os.Environment; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.util.Log; 11 | import android.view.SurfaceHolder; 12 | import android.view.SurfaceView; 13 | import android.view.View; 14 | import android.widget.Button; 15 | import android.widget.ImageView; 16 | import android.widget.Toast; 17 | 18 | import com.aserbao.cameravideorecord.R; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | 23 | import butterknife.BindView; 24 | import butterknife.ButterKnife; 25 | import butterknife.OnClick; 26 | 27 | public class CameraTakePictureActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PictureCallback { 28 | private static final String TAG = "CameraTakePictureActivi"; 29 | 30 | 31 | @BindView(R.id.picture_sv) 32 | SurfaceView mPictureSv; 33 | @BindView(R.id.image_view) 34 | ImageView mImageView; 35 | @BindView(R.id.take_picture) 36 | Button mTakePicture; 37 | @BindView(R.id.btn_recording) 38 | Button mBtnRecording; 39 | @BindView(R.id.btn_start_preview) 40 | Button mBtnStartPreview; 41 | private Camera mCamera; 42 | private SurfaceHolder mHolder; 43 | private Bitmap mBitmap; 44 | private MediaRecorder mMediaRecorder; 45 | private boolean isRecording = false; 46 | 47 | @Override 48 | protected void onCreate(Bundle savedInstanceState) { 49 | super.onCreate(savedInstanceState); 50 | setContentView(R.layout.activity_camera_take_picture); 51 | ButterKnife.bind(this); 52 | mHolder = mPictureSv.getHolder(); 53 | // mHolder.setFormat(PixelFormat.TRANSPARENT); 54 | // mHolder.addCallback(this); 55 | // mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 56 | } 57 | 58 | @Override 59 | protected void onResume() { 60 | super.onResume(); 61 | } 62 | 63 | private void openCamera() { 64 | mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); 65 | Camera.Parameters parameters = mCamera.getParameters(); 66 | mCamera.setParameters(parameters); 67 | mCamera.setDisplayOrientation(90); 68 | try { 69 | mCamera.setPreviewDisplay(mHolder); 70 | } catch (IOException e) { 71 | e.printStackTrace(); 72 | } 73 | mCamera.startPreview(); 74 | } 75 | 76 | @Override 77 | public void surfaceCreated(SurfaceHolder holder) { 78 | 79 | } 80 | 81 | @Override 82 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 83 | 84 | } 85 | 86 | @Override 87 | public void surfaceDestroyed(SurfaceHolder holder) { 88 | 89 | } 90 | 91 | @Override 92 | public void onPictureTaken(byte[] data, Camera camera) { 93 | if (data != null) { 94 | Log.e(TAG, "onPictureTaken: " + data.toString()); 95 | } 96 | } 97 | 98 | @Override 99 | public void onPointerCaptureChanged(boolean hasCapture) { 100 | Log.e(TAG, "onPointerCaptureChanged: " + hasCapture); 101 | } 102 | 103 | @OnClick({R.id.take_picture, R.id.btn_recording, R.id.btn_start_preview}) 104 | public void onViewClicked(View view) { 105 | switch (view.getId()) { 106 | case R.id.btn_start_preview: 107 | openCamera(); 108 | mImageView.setVisibility(View.GONE); 109 | break; 110 | case R.id.take_picture: 111 | mCamera.takePicture(new Camera.ShutterCallback() { 112 | @Override 113 | public void onShutter() { 114 | Log.e(TAG, "onShutter: " + "按下快门"); 115 | } 116 | }, new Camera.PictureCallback() { 117 | @Override 118 | public void onPictureTaken(byte[] data, Camera camera) { 119 | if (data != null) { 120 | showImageView(data); 121 | Log.e(TAG, "onPictureTaken: " + data.toString()); 122 | } 123 | } 124 | }, new Camera.PictureCallback() { 125 | @Override 126 | public void onPictureTaken(byte[] data, Camera camera) { 127 | if (data != null) { 128 | showImageView(data); 129 | Log.e(TAG, "onPictureTaken2: " + data.toString()); 130 | } 131 | } 132 | }); 133 | break; 134 | case R.id.btn_recording: 135 | if(!isRecording){ 136 | mBtnRecording.setText("停止录制"); 137 | startRecording(); 138 | }else{ 139 | mBtnRecording.setText("开始录制"); 140 | stopRecording(); 141 | } 142 | break; 143 | } 144 | } 145 | 146 | private void startRecording() { 147 | try { 148 | mCamera.unlock(); 149 | mMediaRecorder = new MediaRecorder(); 150 | /* String filePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/ych/123.mp4"; 151 | new File(filePath);*/ 152 | File file = new File(Environment.getExternalStorageDirectory().getCanonicalFile() + "/1234.mp4"); 153 | mMediaRecorder.reset(); 154 | mMediaRecorder.setCamera(mCamera); 155 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 156 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 157 | mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); 158 | mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 159 | // mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); 160 | // mMediaRecorder.setVideoSize(320, 240);//每个手机的屏幕视频都不一样,需要调整 161 | // mMediaRecorder.setVideoFrameRate(4); 162 | mMediaRecorder.setVideoEncoder(MediaRecorder 163 | .VideoEncoder.H264); 164 | mMediaRecorder.setVideoSize(1280, 720); 165 | // 每秒 4帧 166 | mMediaRecorder.setOrientationHint(90); 167 | mMediaRecorder.setVideoFrameRate(20); 168 | mMediaRecorder.setPreviewDisplay(mHolder.getSurface()); // ① 169 | mMediaRecorder.setOutputFile(file.getAbsolutePath()); 170 | mMediaRecorder.prepare(); 171 | mMediaRecorder.start(); 172 | isRecording = true; 173 | } catch (Exception e) { 174 | e.printStackTrace(); 175 | } 176 | Toast.makeText(this, "成功", Toast.LENGTH_SHORT).show(); 177 | } 178 | 179 | private void stopRecording() { 180 | if (mMediaRecorder != null) { 181 | isRecording = false; 182 | mMediaRecorder.stop(); 183 | mMediaRecorder.release(); 184 | mMediaRecorder = null; 185 | } 186 | } 187 | 188 | private void showImageView(byte[] data) { 189 | mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length); 190 | mImageView.setImageBitmap(mBitmap); 191 | mImageView.setVisibility(View.VISIBLE); 192 | } 193 | 194 | @Override 195 | protected void onPause() { 196 | super.onPause(); 197 | mCamera.stopPreview(); 198 | } 199 | 200 | @Override 201 | protected void onDestroy() { 202 | super.onDestroy(); 203 | mCamera.release(); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /app/src/main/java/com/aserbao/cameravideorecord/cameraDemo01/RecordingActivity.java: -------------------------------------------------------------------------------- 1 | package com.aserbao.cameravideorecord.cameraDemo01; 2 | 3 | import android.media.MediaPlayer; 4 | import android.media.MediaRecorder; 5 | import android.os.Bundle; 6 | import android.os.Environment; 7 | import android.os.Handler; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.util.Log; 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.ImageView; 13 | import android.widget.TextView; 14 | 15 | import com.aserbao.cameravideorecord.R; 16 | import com.aserbao.cameravideorecord.utils.MediaUtils; 17 | 18 | import java.io.File; 19 | import java.io.IOException; 20 | 21 | import butterknife.BindView; 22 | import butterknife.ButterKnife; 23 | import butterknife.OnClick; 24 | 25 | /** 26 | * 这个一个带录音功能的Activity 27 | */ 28 | public class RecordingActivity extends AppCompatActivity { 29 | @BindView(R.id.start_recording) 30 | Button mStartRecording; 31 | @BindView(R.id.play_recording) 32 | Button mPlayRecording; 33 | @BindView(R.id.textView) 34 | TextView mTextView; 35 | @BindView(R.id.image_view) 36 | ImageView mImageView; 37 | private MediaRecorder mRecorder; 38 | private boolean isRecording = false; 39 | private String mAbsolutePath; 40 | private boolean isPlaying = false; 41 | private MediaPlayer mMediaPlayer; 42 | 43 | @Override 44 | protected void onCreate(Bundle savedInstanceState) { 45 | super.onCreate(savedInstanceState); 46 | setContentView(R.layout.activity_recording); 47 | ButterKnife.bind(this); 48 | mPlayRecording.setClickable(false); 49 | } 50 | 51 | @OnClick({R.id.start_recording, R.id.play_recording}) 52 | public void onViewClicked(View view){ 53 | switch (view.getId()) { 54 | case R.id.start_recording: 55 | if (!isRecording) { 56 | mPlayRecording.setClickable(false); 57 | mStartRecording.setText("停止录音"); 58 | isRecording = true; 59 | mStartRecording(); 60 | } else { 61 | mPlayRecording.setClickable(true); 62 | mStartRecording.setText("开始录音"); 63 | isRecording = false; 64 | mStopRecording(); 65 | } 66 | break; 67 | case R.id.play_recording: 68 | if (!isPlaying) { 69 | try { 70 | mStartRecording.setClickable(false); 71 | isPlaying = true; 72 | mPlayRecording.setText("暂停播放"); 73 | mMediaPlayer = new MediaPlayer(); 74 | mMediaPlayer.setDataSource(mAbsolutePath); 75 | mMediaPlayer.prepare(); 76 | mMediaPlayer.start(); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | } else { 81 | mStartRecording.setClickable(true); 82 | if (mMediaPlayer != null) { 83 | isPlaying = false; 84 | mPlayRecording.setText("开始播放"); 85 | mMediaPlayer.pause(); 86 | } 87 | } 88 | break; 89 | } 90 | } 91 | 92 | private void mStopRecording() { 93 | mRecorder.stop(); 94 | mRecorder.reset(); 95 | mRecorder.release(); 96 | } 97 | 98 | private void mStartRecording() { 99 | try { 100 | String s = Environment.getExternalStorageDirectory().getCanonicalFile().getAbsolutePath() + "/aserbao"; 101 | boolean b = new File(s).mkdir(); 102 | mAbsolutePath = new File(s + "/" + String.valueOf(System.currentTimeMillis()) + ".3gp").getAbsolutePath(); 103 | mRecorder = MediaUtils.startRecording(mAbsolutePath); 104 | updateAudioSize(); 105 | mTextView.setText("录音保存的路径名为:" + mAbsolutePath.toString()); 106 | } catch (IOException e) { 107 | e.printStackTrace(); 108 | } 109 | } 110 | 111 | private final Handler mHandler = new Handler(); 112 | private Runnable mUpdateMicStatusTimer = new Runnable() { 113 | public void run() { 114 | updateAudioSize(); 115 | } 116 | }; 117 | 118 | private int BASE = 600; 119 | private int SPACE = 300;// 间隔取样时间 120 | private void updateAudioSize() { 121 | if (mRecorder != null && mImageView != null){ 122 | int audio = mRecorder.getMaxAmplitude()/BASE; 123 | int db = 0; 124 | if (audio > 1) 125 | db = (int) (20 * Math.log10(audio)); 126 | Log.e("audio_size", "updateAudioSize: " + String.valueOf(db/4) + "==================getMaxAmplitude: " + String.valueOf(mRecorder.getMaxAmplitude())); 127 | switch (db / 4) { 128 | case 0: 129 | mImageView.setImageResource(R.drawable.message_vioce01); 130 | break; 131 | case 1: 132 | mImageView.setImageResource(R.drawable.message_vioce01); 133 | break; 134 | case 2: 135 | mImageView.setImageResource(R.drawable.message_vioce02); 136 | break; 137 | case 3: 138 | mImageView.setImageResource(R.drawable.message_vioce03); 139 | break; 140 | case 4: 141 | mImageView.setImageResource(R.drawable.message_vioce04); 142 | break; 143 | case 5: 144 | mImageView.setImageResource(R.drawable.message_vioce05); 145 | break; 146 | case 6: 147 | mImageView.setImageResource(R.drawable.message_vioce06); 148 | break; 149 | case 7: 150 | mImageView.setImageResource(R.drawable.message_vioce07); 151 | break; 152 | case 8: 153 | mImageView.setImageResource(R.drawable.message_vioce08); 154 | break; 155 | default: 156 | mImageView.setImageResource(R.drawable.message_vioce08); 157 | break; 158 | } 159 | mHandler.postDelayed(mUpdateMicStatusTimer, SPACE); 160 | } 161 | } 162 | 163 | } 164 | -------------------------------------------------------------------------------- /app/src/main/java/com/aserbao/cameravideorecord/utils/CameraUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. All rights reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.aserbao.cameravideorecord.utils; 18 | 19 | import android.app.Activity; 20 | import android.hardware.Camera; 21 | import android.util.Log; 22 | import android.view.Surface; 23 | 24 | import java.util.List; 25 | 26 | /** 27 | * Camera-related utility functions. 28 | */ 29 | public class CameraUtils { 30 | private static final String TAG = "CameraUtils"; 31 | 32 | /** 33 | * Attempts to find a preview size that matches the provided width and height (which 34 | * specify the dimensions of the encoded video). If it fails to find a match it just 35 | * uses the default preview size for video. 36 | *

37 | * TODO: should do a best-fit match, e.g. 38 | * https://github.com/commonsguy/cwac-camera/blob/master/camera/src/com/commonsware/cwac/camera/CameraUtils.java 39 | */ 40 | public static void choosePreviewSize(Camera.Parameters parms, int width, int height) { 41 | // We should make sure that the requested MPEG size is less than the preferred 42 | // size, and has the same aspect ratio. 43 | Camera.Size ppsfv = parms.getPreferredPreviewSizeForVideo(); 44 | if (ppsfv != null) { 45 | Log.d(TAG, "Camera preferred preview size for video is " + 46 | ppsfv.width + "x" + ppsfv.height); 47 | } 48 | 49 | //for (Camera.Size size : parms.getSupportedPreviewSizes()) { 50 | // Log.d(TAG, "supported: " + size.width + "x" + size.height); 51 | //} 52 | 53 | for (Camera.Size size : parms.getSupportedPreviewSizes()) { 54 | if (size.width == width && size.height == height) { 55 | parms.setPreviewSize(width, height); 56 | return; 57 | } 58 | } 59 | 60 | Log.w(TAG, "Unable to set preview size to " + width + "x" + height); 61 | if (ppsfv != null) { 62 | parms.setPreviewSize(ppsfv.width, ppsfv.height); 63 | } 64 | // else use whatever the default size is 65 | } 66 | 67 | /** 68 | * Attempts to find a fixed preview frame rate that matches the desired frame rate. 69 | *

70 | * It doesn't seem like there's a great deal of flexibility here. 71 | *

72 | * TODO: follow the recipe from http://stackoverflow.com/questions/22639336/#22645327 73 | * 74 | * @return The expected frame rate, in thousands of frames per second. 75 | */ 76 | public static int chooseFixedPreviewFps(Camera.Parameters parms, int desiredThousandFps) { 77 | List supported = parms.getSupportedPreviewFpsRange(); 78 | 79 | for (int[] entry : supported) { 80 | //Log.d(TAG, "entry: " + entry[0] + " - " + entry[1]); 81 | if ((entry[0] == entry[1]) && (entry[0] == desiredThousandFps)) { 82 | parms.setPreviewFpsRange(entry[0], entry[1]); 83 | return entry[0]; 84 | } 85 | } 86 | 87 | int[] tmp = new int[2]; 88 | parms.getPreviewFpsRange(tmp); 89 | int guess; 90 | if (tmp[0] == tmp[1]) { 91 | guess = tmp[0]; 92 | } else { 93 | guess = tmp[1] / 2; // shrug 94 | } 95 | 96 | Log.d(TAG, "Couldn't find match for " + desiredThousandFps + ", using " + guess); 97 | return guess; 98 | } 99 | 100 | /** 101 | * reference http://www.jianshu.com/p/1513134733d0 102 | */ 103 | public static void setCameraDisplayOrientation(Activity activity, 104 | int cameraId, Camera camera) { 105 | Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); 106 | Camera.getCameraInfo(cameraId, info); 107 | int rotation = activity.getWindowManager().getDefaultDisplay().getRotation(); 108 | int degrees = 0; 109 | switch (rotation) { 110 | case Surface.ROTATION_0: 111 | degrees = 0; 112 | break; 113 | case Surface.ROTATION_90: 114 | degrees = 90; 115 | break; 116 | case Surface.ROTATION_180: 117 | degrees = 180; 118 | break; 119 | case Surface.ROTATION_270: 120 | degrees = 270; 121 | break; 122 | } 123 | 124 | int result; 125 | if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 126 | result = (info.orientation + degrees) % 360; 127 | result = (360 - result) % 360; // compensate the mirror 128 | } else { // back-facing 129 | result = (info.orientation - degrees + 360) % 360; 130 | } 131 | camera.setDisplayOrientation(result); 132 | } 133 | 134 | //TODO: follow the recipe from http://stackoverflow.com/questions/22639336/#22645327 135 | public static int[] closetFramerate(Camera.Parameters parameters, float frameRate) { 136 | int framerate = (int) (frameRate * 1000); 137 | List rates = parameters.getSupportedPreviewFpsRange(); 138 | int[] bestFramerate = rates.get(0); 139 | for (int i = 0; i < rates.size(); i++) { 140 | int[] rate = rates.get(i); 141 | Log.e(TAG, "supported preview pfs min " + rate[0] + " max " + rate[1]); 142 | int curDelta = Math.abs(rate[1] - framerate); 143 | int bestDelta = Math.abs(bestFramerate[1] - framerate); 144 | if (curDelta < bestDelta) { 145 | bestFramerate = rate; 146 | } else if (curDelta == bestDelta) { 147 | bestFramerate = bestFramerate[0] < rate[0] ? rate : bestFramerate; 148 | } 149 | } 150 | return bestFramerate; 151 | } 152 | } 153 | -------------------------------------------------------------------------------- /app/src/main/java/com/aserbao/cameravideorecord/utils/MediaUtils.java: -------------------------------------------------------------------------------- 1 | package com.aserbao.cameravideorecord.utils; 2 | 3 | import android.media.MediaRecorder; 4 | import android.os.Environment; 5 | import android.text.TextUtils; 6 | 7 | import java.io.File; 8 | import java.io.IOException; 9 | 10 | /** 11 | * description: 12 | * Created by aserbao on 2017/11/29. 13 | */ 14 | 15 | 16 | public class MediaUtils { 17 | 18 | 19 | public static MediaRecorder startRecording(String mAbsolutePath){ 20 | if (TextUtils.isEmpty(mAbsolutePath)){ 21 | return null; 22 | } 23 | try { 24 | MediaRecorder mRecorder = new MediaRecorder(); 25 | mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 26 | mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB); 27 | mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); 28 | mRecorder.setOutputFile(mAbsolutePath); 29 | mRecorder.prepare(); 30 | mRecorder.start(); 31 | return mRecorder; 32 | } catch (IOException e) { 33 | e.printStackTrace(); 34 | return null; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce01.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce01.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce02.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce02.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce03.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce03.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce04.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce04.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce05.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce05.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce06.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce06.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce07.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce07.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/message_vioce08.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aserbao/CameraVideoRecord/d7a1866f3e9407231d38018c620b420a19be0bcd/app/src/main/res/drawable/message_vioce08.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_camera_take_picture.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 |