У меня есть служба, которая записывает видео из фона без предварительного просмотра или чего-то еще. Я не уверен в ошибке. Я пробовал поискать в Google всюду, но все равно не мог понять причину Но что бы я ни делал, я получаю сообщение об ошибке при запуске MediaRecorder 22?
import android.app.IntentService;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.Context;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
/**
* An {@link IntentService} subclass for handling asynchronous task requests in
* a service on a separate handler thread.
* <p>
* TODO: Customize class - update intent actions, extra parameters and static
* helper methods.
*/
public class RecordVideoService extends IntentService {
Camera mCamera;
MediaRecorder mediaRecorder;
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private boolean Boolean_record;
private String TAG="record service";
private IBinder mBinder = new LocalBinder();
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG,"onbind");
return mBinder;
}
public class LocalBinder extends Binder {
public RecordVideoService getService() {
return RecordVideoService.this;
}
}
public RecordVideoService() {
super("RecordVideoService");
}
/**
* Starts this service to perform action Foo with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
/**
* Starts this service to perform action Baz with the given parameters. If
* the service is already performing a task this action will be queued.
*
* @see IntentService
*/
// TODO: Customize helper method
@Override
protected void onHandleIntent(Intent intent) {
if (intent != null) {
Boolean_record=false;
final String action = intent.getAction();
startRecord();
Log.i(TAG,"onHandleIntent");
}
}
/**
* Handle action Foo in the provided background thread with the provided
* parameters.
*/
public void startRecord(){
try{
Log.i(TAG,"startRecord");
mCamera=openFrontFacingCamera();
mCamera.unlock();
mediaRecorder=new MediaRecorder();
ContextWrapper cw = new ContextWrapper(getApplication());
File directory = cw.getDir("background_upload",MODE_PRIVATE);
String filename="1";
File localfile=new File(directory,filename);
mediaRecorder.setCamera(mCamera);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mediaRecorder.setOutputFile(localfile.toString());
mediaRecorder.setVideoFrameRate(30);
mediaRecorder.prepare();
mediaRecorder.start();
Boolean_record=true;
}catch (Exception e){
e.printStackTrace();
}
}
private Camera openFrontFacingCamera() {
int cameraCount = 0;
Camera cam = null;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
Log.i(TAG,"camera count"+cameraCount);
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
Log.i(TAG,"camera front open");
try {
if(cam==null){
Log.i(TAG,"cam is null"+ camIdx);
cam = Camera.open(camIdx);}
} catch (RuntimeException e) {
Log.i(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return cam;
}
public void stopRecording(){
if(Boolean_record){
Toast.makeText(getBaseContext(), "Recording Stopped", Toast.LENGTH_SHORT).show();
try {
mCamera.reconnect();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mCamera.release();
mCamera = null;
Log.i(TAG,"stopRecord");}
}
public void stopService(){
stopSelf();
}
/**
* Handle action Baz in the provided background thread with the provided
* parameters.
*/
@Override
public boolean onUnbind(Intent intent) {
Log.i(TAG,"unblind");
return super.onUnbind(intent);
}
@Override
public void onRebind(Intent intent) {
Log.i(TAG,"rebind");
super.onRebind(intent);
}
}
Но при запуске службы я получаю следующую ошибку:
2020-07-12 22:55:45.375 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setAudioSource(1)
2020-07-12 22:55:45.379 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setVideoSource(1)
2020-07-12 22:55:45.380 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setAudioEncoder(0)
2020-07-12 22:55:45.380 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setVideoEncoder(0)
2020-07-12 22:55:45.380 32312-32395/com.example.datatrasfer W/MediaRecorder: setOutputFile: path = /data/user/0/com.example.datatrasfer/app_background_upload/1
2020-07-12 22:55:45.380 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setVideoFrameRate(30)
2020-07-12 22:55:45.381 32312-32395/com.example.datatrasfer W/MediaRecorder: prepare: mPath = /data/user/0/com.example.datatrasfer/app_background_upload/1
2020-07-12 22:55:45.384 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: setOutputFile
2020-07-12 22:55:45.384 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: prepare
2020-07-12 22:55:45.388 32312-32395/com.example.datatrasfer I/MediaRecorderJNI: start
2020-07-12 22:55:45.874 32312-32395/com.example.datatrasfer E/MediaRecorder: start failed: -22
2020-07-12 22:55:45.876 32312-32395/com.example.datatrasfer W/System.err: java.lang.RuntimeException: start failed.
2020-07-12 22:55:45.877 32312-32395/com.example.datatrasfer W/System.err: at android.media.MediaRecorder._start(Native Method)
2020-07-12 22:55:45.877 32312-32395/com.example.datatrasfer W/System.err: at android.media.MediaRecorder.start(MediaRecorder.java:1340)
2020-07-12 22:55:45.877 32312-32395/com.example.datatrasfer W/System.err: at com.example.datatrasfer.RecordVideoService.startRecord(RecordVideoService.java:103)
2020-07-12 22:55:45.877 32312-32395/com.example.datatrasfer W/System.err: at com.example.datatrasfer.RecordVideoService.onHandleIntent(RecordVideoService.java:75)
2020-07-12 22:55:45.878 32312-32395/com.example.datatrasfer W/System.err: at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:76)
2020-07-12 22:55:45.878 32312-32395/com.example.datatrasfer W/System.err: at android.os.Handler.dispatchMessage(Handler.java:106)
2020-07-12 22:55:45.878 32312-32395/com.example.datatrasfer W/System.err: at android.os.Looper.loop(Looper.java:216)
2020-07-12 22:55:45.878 32312-32395/com.example.datatrasfer W/System.err: at android.os.HandlerThread.run(HandlerThread.java:65)
2020-07-12 22:55:45.878 32312-32395/com.example.datatrasfer I/record service: onHandleIntent
2020-07-12 22:55:49.205 32312-32358/com.example.datatrasfer V/FA: Inactivity, disconnecting from the service