├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── app ├── src │ └── main │ │ ├── res │ │ ├── drawable-hdpi │ │ │ └── icon.png │ │ ├── drawable-ldpi │ │ │ └── icon.png │ │ ├── drawable-mdpi │ │ │ └── icon.png │ │ ├── values │ │ │ └── strings.xml │ │ └── layout │ │ │ └── main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── meowme │ │ └── camerarecorder │ │ ├── CameraRecorder.java │ │ └── RecorderService.java ├── build.gradle └── app.iml ├── .gitignore ├── README.md ├── AndroidCameraRecorder.iml ├── import-summary.txt ├── gradlew.bat └── gradlew /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pickerweng/CameraRecorder/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pickerweng/CameraRecorder/HEAD/app/src/main/res/drawable-hdpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pickerweng/CameraRecorder/HEAD/app/src/main/res/drawable-ldpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pickerweng/CameraRecorder/HEAD/app/src/main/res/drawable-mdpi/icon.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .gradle 2 | /local.properties 3 | /.idea/workspace.xml 4 | /.idea/libraries 5 | .DS_Store 6 | /build 7 | /captures 8 | /coverage_report 9 | /app/build 10 | /*/build 11 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | Hello World, CameraRecorder! 4 | CameraRecorder 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CameraRecorder 2 | This is a sample project to show how to record video by camera in the background service. 3 | The sample need be built in API Level 16 (or later). 4 | 5 | Notice! 6 | This sample is only tested on HTC One (M8) device (Android 5.0.2). 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Oct 21 11:34:03 PDT 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.8-all.zip 7 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | android { 3 | compileSdkVersion 16 4 | buildToolsVersion "23.0.2" 5 | defaultConfig { 6 | applicationId "com.meowme.camerarecorder" 7 | minSdkVersion 16 8 | targetSdkVersion 16 9 | } 10 | buildTypes { 11 | release { 12 | minifyEnabled false 13 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 14 | } 15 | } 16 | productFlavors { 17 | } 18 | } 19 | 20 | dependencies { 21 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 13 | 19 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /AndroidCameraRecorder.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /import-summary.txt: -------------------------------------------------------------------------------- 1 | ECLIPSE ANDROID PROJECT IMPORT SUMMARY 2 | ====================================== 3 | 4 | Ignored Files: 5 | -------------- 6 | The following files were *not* copied into the new Gradle project; you 7 | should evaluate whether these are still needed in your project and if 8 | so manually move them: 9 | 10 | * default.properties 11 | 12 | Moved Files: 13 | ------------ 14 | Android Gradle projects use a different directory structure than ADT 15 | Eclipse projects. Here's how the projects were restructured: 16 | 17 | * AndroidManifest.xml => app/src/main/AndroidManifest.xml 18 | * res/ => app/src/main/res/ 19 | * src/ => app/src/main/java/ 20 | 21 | Next Steps: 22 | ----------- 23 | You can now build the project. The Gradle project needs network 24 | connectivity to download dependencies. 25 | 26 | Bugs: 27 | ----- 28 | If for some reason your project does not build, and you determine that 29 | it is due to a bug or limitation of the Eclipse to Gradle importer, 30 | please file a bug at http://b.android.com with category 31 | Component-Tools. 32 | 33 | (This import summary is for your information only, and can be deleted 34 | after import once you are satisfied with the results.) 35 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/com/meowme/camerarecorder/CameraRecorder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, Picker Weng 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * Neither the name of CameraRecorder nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | * Project: 31 | * CameraRecorder 32 | * 33 | * File: 34 | * CameraRecorder.java 35 | * 36 | * Author: 37 | * Picker Weng (pickerweng@gmail.com) 38 | */ 39 | 40 | package com.meowme.camerarecorder; 41 | 42 | import android.app.Activity; 43 | import android.content.Intent; 44 | import android.hardware.Camera; 45 | import android.os.Bundle; 46 | import android.view.SurfaceHolder; 47 | import android.view.SurfaceView; 48 | import android.view.View; 49 | import android.widget.Button; 50 | 51 | public class CameraRecorder extends Activity implements SurfaceHolder.Callback { 52 | 53 | private static final String TAG = CameraRecorder.class.getSimpleName(); 54 | 55 | public static SurfaceView mSurfaceView; 56 | public static SurfaceHolder mSurfaceHolder; 57 | public static Camera mCamera; 58 | public static boolean mPreviewRunning; 59 | 60 | /** Called when the activity is first created. */ 61 | @Override 62 | public void onCreate(Bundle savedInstanceState) { 63 | super.onCreate(savedInstanceState); 64 | setContentView(R.layout.main); 65 | 66 | mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1); 67 | mSurfaceHolder = mSurfaceView.getHolder(); 68 | mSurfaceHolder.addCallback(this); 69 | mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 70 | 71 | Button btnStart = (Button) findViewById(R.id.StartService); 72 | btnStart.setOnClickListener(new View.OnClickListener() 73 | { 74 | public void onClick(View v) 75 | { 76 | Intent intent = new Intent(CameraRecorder.this, RecorderService.class); 77 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 78 | startService(intent); 79 | finish(); 80 | } 81 | }); 82 | 83 | Button btnStop = (Button) findViewById(R.id.StopService); 84 | btnStop.setOnClickListener(new View.OnClickListener() 85 | { 86 | public void onClick(View v) 87 | { 88 | stopService(new Intent(CameraRecorder.this, RecorderService.class)); 89 | } 90 | }); 91 | } 92 | 93 | @Override 94 | public void surfaceCreated(SurfaceHolder holder) { 95 | 96 | } 97 | 98 | @Override 99 | public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 100 | } 101 | 102 | @Override 103 | public void surfaceDestroyed(SurfaceHolder holder) { 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/com/meowme/camerarecorder/RecorderService.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2015, Picker Weng 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * Redistributions of source code must retain the above copyright notice, this 9 | * list of conditions and the following disclaimer. 10 | * 11 | * Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * Neither the name of CameraRecorder nor the names of its 16 | * contributors may be used to endorse or promote products derived from 17 | * this software without specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | * 30 | * Project: 31 | * CameraRecorder 32 | * 33 | * File: 34 | * CameraRecorder.java 35 | * 36 | * Author: 37 | * Picker Weng (pickerweng@gmail.com) 38 | */ 39 | 40 | package com.meowme.camerarecorder; 41 | 42 | import android.app.Service; 43 | import android.content.Intent; 44 | import android.hardware.Camera; 45 | import android.hardware.Camera.Size; 46 | import android.media.MediaRecorder; 47 | import android.os.Environment; 48 | import android.os.IBinder; 49 | import android.util.Log; 50 | import android.view.SurfaceHolder; 51 | import android.view.SurfaceView; 52 | import android.widget.Toast; 53 | 54 | import java.io.IOException; 55 | import java.util.List; 56 | 57 | public class RecorderService extends Service { 58 | private static final String TAG = "RecorderService"; 59 | private SurfaceView mSurfaceView; 60 | private SurfaceHolder mSurfaceHolder; 61 | private static Camera mServiceCamera; 62 | private boolean mRecordingStatus; 63 | private MediaRecorder mMediaRecorder; 64 | 65 | @Override 66 | public void onCreate() { 67 | mRecordingStatus = false; 68 | mServiceCamera = CameraRecorder.mCamera; 69 | mSurfaceView = CameraRecorder.mSurfaceView; 70 | mSurfaceHolder = CameraRecorder.mSurfaceHolder; 71 | 72 | super.onCreate(); 73 | } 74 | 75 | @Override 76 | public IBinder onBind(Intent intent) { 77 | return null; 78 | } 79 | 80 | @Override 81 | public int onStartCommand(Intent intent, int flags, int startId) { 82 | super.onStartCommand(intent, flags, startId); 83 | 84 | if (mRecordingStatus == false) 85 | startRecording(); 86 | 87 | return START_STICKY; 88 | } 89 | 90 | @Override 91 | public void onDestroy() { 92 | stopRecording(); 93 | mRecordingStatus = false; 94 | 95 | super.onDestroy(); 96 | } 97 | 98 | public boolean startRecording(){ 99 | try { 100 | Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show(); 101 | 102 | mServiceCamera = Camera.open(); 103 | Camera.Parameters params = mServiceCamera.getParameters(); 104 | mServiceCamera.setParameters(params); 105 | Camera.Parameters p = mServiceCamera.getParameters(); 106 | 107 | final List listPreviewSize = p.getSupportedPreviewSizes(); 108 | for (Size size : listPreviewSize) { 109 | Log.i(TAG, String.format("Supported Preview Size (%d, %d)", size.width, size.height)); 110 | } 111 | 112 | Size previewSize = listPreviewSize.get(0); 113 | p.setPreviewSize(previewSize.width, previewSize.height); 114 | mServiceCamera.setParameters(p); 115 | 116 | try { 117 | mServiceCamera.setPreviewDisplay(mSurfaceHolder); 118 | mServiceCamera.startPreview(); 119 | } 120 | catch (IOException e) { 121 | Log.e(TAG, e.getMessage()); 122 | e.printStackTrace(); 123 | } 124 | 125 | mServiceCamera.unlock(); 126 | 127 | mMediaRecorder = new MediaRecorder(); 128 | mMediaRecorder.setCamera(mServiceCamera); 129 | mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 130 | mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); 131 | mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); 132 | mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); 133 | mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); 134 | mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath() + "/video.mp4"); 135 | mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); 136 | 137 | mMediaRecorder.prepare(); 138 | mMediaRecorder.start(); 139 | 140 | mRecordingStatus = true; 141 | 142 | return true; 143 | 144 | } catch (IllegalStateException e) { 145 | Log.d(TAG, e.getMessage()); 146 | e.printStackTrace(); 147 | return false; 148 | 149 | } catch (IOException e) { 150 | Log.d(TAG, e.getMessage()); 151 | e.printStackTrace(); 152 | return false; 153 | } 154 | } 155 | 156 | public void stopRecording() { 157 | Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show(); 158 | try { 159 | mServiceCamera.reconnect(); 160 | 161 | } catch (IOException e) { 162 | e.printStackTrace(); 163 | } 164 | 165 | mMediaRecorder.stop(); 166 | mMediaRecorder.reset(); 167 | 168 | mServiceCamera.stopPreview(); 169 | mMediaRecorder.release(); 170 | 171 | mServiceCamera.release(); 172 | mServiceCamera = null; 173 | } 174 | } 175 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | --------------------------------------------------------------------------------