我正在尝试使用 Activity 和 Service 在后台录制视频,但我无法使其正常工作。应用程序只显示按钮然后关闭。我在Galaxy SII Android 4.0.3上运行它,以下是错误和源代码。
logcat的
04-24 22:27:10.158: E/MediaRecorder(28177): start failed: -19
04-24 22:27:10.158: D/AndroidRuntime(28177): Shutting down VM
04-24 22:27:10.158: W/dalvikvm(28177): threadid=1: thread exiting with uncaught exception (group=0x40c511f8)
04-24 22:27:10.158: E/AndroidRuntime(28177): FATAL EXCEPTION: main
04-24 22:27:10.158: E/AndroidRuntime(28177): java.lang.RuntimeException: Unable to start service com.example.transfertofile.RecorderService@41519328 with Intent { flg=0x10000000 cmp=com.example.transfertofile/.RecorderService }: java.lang.RuntimeException: start failed.
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2388)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.app.ActivityThread.access$1900(ActivityThread.java:127)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1222)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.os.Looper.loop(Looper.java:137)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.app.ActivityThread.main(ActivityThread.java:4507)
04-24 22:27:10.158: E/AndroidRuntime(28177): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 22:27:10.158: E/AndroidRuntime(28177): at java.lang.reflect.Method.invoke(Method.java:511)
04-24 22:27:10.158: E/AndroidRuntime(28177): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-24 22:27:10.158: E/AndroidRuntime(28177): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-24 22:27:10.158: E/AndroidRuntime(28177): at dalvik.system.NativeStart.main(Native Method)
04-24 22:27:10.158: E/AndroidRuntime(28177): Caused by: java.lang.RuntimeException: start failed.
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.media.MediaRecorder.start(Native Method)
04-24 22:27:10.158: E/AndroidRuntime(28177): at com.example.transfertofile.RecorderService.startRecording(RecorderService.java:107)
04-24 22:27:10.158: E/AndroidRuntime(28177): at com.example.transfertofile.RecorderService.onStartCommand(RecorderService.java:48)
04-24 22:27:10.158: E/AndroidRuntime(28177): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2371)
04-24 22:27:10.158: E/AndroidRuntime(28177): ... 10 more
CameraRecorder.java
public class CameraRecorder extends Activity implements SurfaceHolder.Callback {
protected void onResume(){
super.onResume() ;
setContentView(R.layout.main);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView1);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Log.v(TAG, "onresume");
Runnable task = new Runnable() {
public void run() {
// do something
if (mPreviewRunning) {
mPreviewRunning = !mPreviewRunning;
stopService(new Intent(CameraRecorder.this, RecorderService.class));
Log.v(TAG, "running");
finish();
}
else {
mPreviewRunning = !mPreviewRunning;
Intent intent = new Intent(CameraRecorder.this, RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startService(intent);
Log.v(TAG, "notrunning");
finish();
}
}
};
worker.schedule(task, 100, TimeUnit.MILLISECONDS);
}
}
RecorderService.java
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() {
Log.v(TAG, "oncreate");
mRecordingStatus = false;
mServiceCamera = CameraRecorder.mCamera;
mSurfaceView = CameraRecorder.mSurfaceView;
mSurfaceHolder = CameraRecorder.mSurfaceHolder;
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v(TAG, "onstartcom");
super.onStartCommand(intent, flags, startId);
if (mRecordingStatus == false)
startRecording();
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.v(TAG, "ondestroy");
stopRecording();
mRecordingStatus = false;
super.onDestroy();
}
public boolean startRecording(){
try {
Log.v(TAG, "startrecording");
Toast.makeText(getBaseContext(), "Recording Started", Toast.LENGTH_SHORT).show();
mServiceCamera = Camera.open();
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);
Log.v(TAG, "setparams");
try {
mServiceCamera.setPreviewDisplay(mSurfaceHolder);
mServiceCamera.startPreview();
Log.v(TAG, "setpreview");
}
catch (IOException e) {
Log.e(TAG, e.getMessage() + "woo");
e.printStackTrace();
}
mServiceCamera.unlock();
Log.v(TAG, "camunlock");
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.DEFAULT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
mMediaRecorder.setOutputFile("/sdcard/video.mp4");
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mPreviewSize.width, mPreviewSize.height);
mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mMediaRecorder.stop();
mMediaRecorder.reset();
mServiceCamera.stopPreview();
mMediaRecorder.release();
mServiceCamera.release();
mServiceCamera = null;
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.transfertofile"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.transfertofile.CameraRecorder"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.example.transfertofile.RecorderService"></service>
</application>
</manifest>
答案 0 :(得分:0)
请在清单文件中添加以下权限。
<uses-permission android:name="android.permission.RECORD_VIDEO" />