1

我是一名学生,正在做我的 Android APP 任务。我遇到了一个无法解决的问题。请给我一些建议,谢谢。我想要进行视频录制活动,并且我已经使用此代码完成了。 使用媒体录像机录制视频 这是我的代码,它在其他手机中运行良好,但在 moto-razr 中无法正常运行,这是 HTC 渴望和 MOTO razr 拍摄的两个视频。愿望:http: //youtu.be/suPF9Hk6iYk razr:http: //youtu.be/wLvH7SXdcIs 谁能帮我解决我的问题?

package video.pac;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Display;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class video extends Activity{

private MediaRecorder recorder;
private Preview mPreview;

boolean flag=false; 
boolean startedRecording=false;
boolean stoppedRecording=false;
boolean key = false;

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);
  WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);

  recorder = new MediaRecorder();
  recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  recorder.setVideoSize(640,480);
  recorder.setVideoFrameRate(20);
  recorder.setVideoEncodingBitRate(3000000);
  recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  //recorder.setMaxDuration(5000);
  mPreview = new Preview(video.this,recorder);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  setContentView(mPreview);

 } 

private CountDownTimer mCountDownTimer = new CountDownTimer(9000, 1000) {

    public void onTick(long millisUntilFinished) {}

    public void onFinish() {

        recorder = null;
        System.out.println("stop");
        video.this.finish();
    }
};

class stopThread implements Runnable {   
    public void run() {  

          try {
              mCountDownTimer.start();  
               Thread.sleep(100);    
          } catch (InterruptedException e) {   
               Thread.currentThread().interrupt();   
          }   

    }   
}


class Preview extends SurfaceView implements SurfaceHolder.Callback{
  //Create objects for MediaRecorder and SurfaceHolder.
  SurfaceHolder mHolder;
  MediaRecorder tempRecorder;

  public Preview(Context context,MediaRecorder recorder) {
    super(context);
    tempRecorder=recorder;
    mHolder=getHolder();
    mHolder.addCallback(this);
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    // TODO Auto-generated constructor stub
  }

  public Surface getSurface(){
    return mHolder.getSurface();
  }

  public void surfaceCreated(SurfaceHolder holder){

    tempRecorder.setOutputFile("/sdcard/test" + ".3gpp");
    tempRecorder.setPreviewDisplay(mHolder.getSurface());
    try{
      tempRecorder.prepare();
      recorder.start();
     new Thread(new stopThread()).start();

      System.out.println("start");
    } catch (Exception e) {
      tempRecorder.release();
      tempRecorder = null;
    }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    if(tempRecorder!=null){
      tempRecorder.stop();
      tempRecorder.release();
      tempRecorder = null;
      System.out.println("release");
    }
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {}
}   
}
4

1 回答 1

0

我认为这个问题是一个安全问题:

您需要设置录像机预览,否则某些设备会将其视为捕获视频而不在屏幕上显示您的捕获/预览的安全漏洞。尝试将此添加到您的录音机设置中:

 .
 .
 recorder.setPreviewDisplay(mHolder.getSurface());
 .
 .
于 2013-02-26T19:15:11.023 回答