#android
#Android
Вопрос:
Я хочу записать видео с помощью пользовательского видеомагнитофона, чтобы сохранить видео mp4 с помощью Media Recorder в Android.
кто-нибудь может предоставить код? я выполнил код (показан ниже), но он показывает незаконное исключение. может кто-нибудь сказать, в чем проблема?
import java.io.IOException;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
public class CustomCameraRecordActivity extends Activity implements OnClickListener, SurfaceHolder.Callback
{
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
recorder = new MediaRecorder();
initRecorder();
SurfaceView cameraView = (SurfaceView) findViewById(R.id.CameraView);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
cameraView.setClickable(true);
cameraView.setOnClickListener(this); }
private void initRecorder()
{
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(Environment.getExternalStorageDirectory() "VideoGcm" System.currentTimeMillis() ".mp4");
recorder.setMaxDuration(50000); // 50 seconds recorder.setMaxFileSize(5000000); // Approximately 5 megabytes
}
private void prepareRecorder()
{
recorder.setPreviewDisplay(holder.getSurface());
try
{
recorder.prepare();
}
catch (IllegalStateException e)
{
e.printStackTrace();
finish();
} catch (IOException e)
{ e.printStackTrace();
finish();
}
}
public void onClick(View v)
{
if (recording)
{
recorder.stop();
recording = false;
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
}
else
{
recording = true;
recorder.start();
}
}
public void surfaceCreated(SurfaceHolder holder)
{
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height)
{
}
public void surfaceDestroyed(SurfaceHolder holder)
{ if (recording)
{
recorder.stop();
recording = false;
}
recorder.release();
finish();
}
}
Спасибо
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hcl.camera/com.hcl.camera.CustomCameraRecordActivity}: java.lang.IllegalStateException
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1748)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1764)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread.access$1500(ActivityThread.java:122)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1002)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.os.Looper.loop(Looper.java:132)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread.main(ActivityThread.java:4025)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at java.lang.reflect.Method.invokeNative(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at java.lang.reflect.Method.invoke(Method.java:491)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at dalvik.system.NativeStart.main(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): Caused by: java.lang.IllegalStateException
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.media.MediaRecorder.setOutputFormat(Native Method)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.media.MediaRecorder.setProfile(MediaRecorder.java:295)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at com.hcl.camera.CustomCameraRecordActivity.initRecorder(CustomCameraRecordActivity.java:45)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at com.hcl.camera.CustomCameraRecordActivity.onCreate(CustomCameraRecordActivity.java:32)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1712)
10-20 19:19:37.540: ERROR/AndroidRuntime(2718): ... 11 more
10-20 19:20:40.720: ERROR/MediaRecorder(2759): start called in an invalid state: 4
10-20 19:20:40.730: ERROR/AndroidRuntime(2759): FATAL EXCEPTION: main
Ответ №1:
public void surfaceCreated(SurfaceHolder holder)
{
prepareRecorder();
}
Я думаю, что приведенный выше код вызывается раньше initRecorder()
Из документации : setOutputFormat()
Задает формат выходного файла, создаваемого во время записи. Вызовите это после setAudioSource() / setVideoSource(), но перед prepare() .