gpt4 book ai didi

android - 我正在尝试使用服务录制视频

转载 作者:行者123 更新时间:2023-11-29 00:08:23 25 4
gpt4 key购买 nike

我正在尝试开发一个应用程序,它可以在后台录制一段视频,然后自动停止。我能够初始化所有必要的东西但不能记录它

我的主要 Activity :

 mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button btnStart = (Button) findViewById(R.id.button1);

btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(StartTestActivity.this,
RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
finish();

我的录音机服务:

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

import java.io.IOException;
import java.util.List;

public class RecorderService extends Service {
private static final String TAG = "RecorderService";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private static Camera mServiceCamera;
private boolean mRecordingStatus;
private MediaRecorder mMediaRecorder;

@Override
public void onCreate() {
mRecordingStatus = false;
mServiceCamera = Camera.open(1);
mSurfaceView = MainActivity.mSurfaceView;
mSurfaceHolder = MainActivity.mSurfaceHolder;
super.onCreate();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (mRecordingStatus == false)
startRecording();
return START_STICKY;
}

@Override
public void onDestroy() {
stopRecording();
mRecordingStatus = false;

super.onDestroy();
}

public boolean startRecording() {
try {
Camera.Parameters params = mServiceCamera.getParameters();
mServiceCamera.setParameters(params);
Camera.Parameters p = mServiceCamera.getParameters();

final List<Size> listSize = p.getSupportedPreviewSizes();
Size mPreviewSize = listSize.get(2);
Log.v(TAG, "use: width = " + mPreviewSize.width
+ " height = " + mPreviewSize.height);
p.setPreviewSize(mPreviewSize.width, mPreviewSize.height);
p.setPreviewFormat(PixelFormat.YCbCr_420_SP);
mServiceCamera.setParameters(p);

try {
mServiceCamera.setPreviewDisplay(mSurfaceHolder);
mServiceCamera.startPreview();
}
catch (IOException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
}

mServiceCamera.unlock();

mMediaRecorder = new MediaRecorder();
mMediaRecorder.setCamera(mServiceCamera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory()+"/vid
eo.mp4");
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mPreviewSize.width,
mPreviewSize.height);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
mMediaRecorder.setMaxDuration(5000);
mMediaRecorder.prepare();
mMediaRecorder.start();
mRecordingStatus = true;
return true;
} catch (IllegalStateException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
return false;
} catch (IOException e) {
Log.d(TAG, e.getMessage());
e.printStackTrace();
return false;
}
}

public void stopRecording() {
Toast.makeText(getBaseContext(), "Recording Stopped",
Toast.LENGTH_SHORT).show();
try {
mServiceCamera.reconnect();
} catch (IOException e) {
e.printStackTrace();
}
mMediaRecorder.stop();
mMediaRecorder.reset();
mServiceCamera.stopPreview();
mMediaRecorder.release();
mServiceCamera.release();
mServiceCamera = null;
}
}

我的错误:

08-20 11:10:54.184  12603-12603/com.example.svbhargav.mediafilesharing 
E/MediaRecorder﹕ start failed: -19
08-20 11:10:54.189 12603-12603/com.example.svbhargav.mediafilesharing
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.svbhargav.mediafilesharing, PID: 12603
java.lang.RuntimeException: Unable to start service
com.example.svbhargav.mediafilesharing.RecorderService@32ef1113 with Intent
{ flg=0x10000000 cmp=com.example.svbhargav.mediafilesharing/.RecorderService
}: java.lang.RuntimeException: start failed.
at
android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2942)
at android.app.ActivityThread.access$2200(ActivityThread.java:154)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5291)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at

.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
Caused by: java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at
com.example.svbhargav.mediafilesharing.RecorderService.startRecording
(RecorderService.java:114)
com.example.svbhargav.mediafilesharing.RecorderService.onStartCommand
(RecorderService.java:45)
at .ActivityThread.handleServiceArgs(ActivityThread.java:2925)     
at android.app.ActivityThread.access$2200(ActivityThread.java:154)

最佳答案

我做过类似的事情并取得了成功你也可以通过下面的链接 Background video recording in Android 4.0

关于android - 我正在尝试使用服务录制视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32110522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com