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

Posted

技术标签:

【中文标题】我正在尝试使用服务录制视频【英文标题】:I am trying to record a video using a service 【发布时间】:2015-08-20 05:55:22 【问题描述】:

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

我的主要活动:

 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();

我的 RecorderService:

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)

            

【问题讨论】:

你不能在你的服务中做 UI 相关的事情。不要为此使用服务。 它的发生是背景 【参考方案1】:

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

【讨论】:

以上是关于我正在尝试使用服务录制视频的主要内容,如果未能解决你的问题,请参考以下文章

使用 AVCaptureSession 录制视频

Android:同时录制视频和播放 mp3?

前置摄像头视频录制 iPhone 4?

无法使用 AVFoundation 将录制的视频上传到服务器 - Swift 3

RIM黑莓录制3GP视频

如何使用 AVCaptureDeviceFormat 从 iPhone 录制延时视频?