綜合討論
瀏覽 : 1642
分享

[問題] 請問如何寫出一個有120fps的錄影程式

一般用戶

等級1

路過旅人

robinsd2016-1-26 20:52

我想自己寫一個有120fps錄影功能的程式,以下是我自己寫的錄影程式,下面設定各種參數設定基本上都沒問題,只有setvideoframerate這項是無效的,不管我設多少的值,錄出來的影片都是30fps,我知道android studio官網有說  setVideoFrameRate是用來設定裝置framerate的最大值,那我想要請問如何設定錄影參數才能達到我想要的影片真實

framerate呢??(我的手機是htc one m9本身內建相機就有支援到  720p   120fps慢速錄影)




public class VideoRecorderActivity extends Activity {

    private static final String TAG = "VideoRecorderActivity";
    private MediaRecorder mr;
    private Button recBtn;
    private SurfaceView sv;
    private boolean isRecording;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    this.recBtn = (Button) this.findViewById(R.id.rec);
    this.isRecording = false;
    // 預覽視窗
    this.sv = (SurfaceView) this.findViewById(R.id.sv);
    this.sv.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

    public void onRec(View v) {
        if (this.isRecording) {
            Log.d(TAG, "停止錄影");
            this.isRecording = false;
            this.recBtn.setText("錄影");
            this.mr.stop();
            this.mr.release();
        }
        else {
            Log.d(TAG, "開始錄影");
            this.isRecording = true;
            this.recBtn.setText("停止");
            this.mr = new MediaRecorder();
            // 以下 this.mr.setXXX() 順序非常非常重要
            this.mr.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            //Log.d(TAG, "setVideoSource");
            this.mr.setAudioSource(MediaRecorder.AudioSource.MIC);
            //Log.d(TAG, "setAudioSource");
            this.mr.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            //Log.d(TAG, "setOutputFormat");
            this.mr.setOutputFile(this.createFilePath());
            //Log.d(TAG, "setOutputFile");
            this.mr.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            //Log.d(TAG, "setVideoEncoder");
            this.mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            //Log.d(TAG, "setAudioEncoder");
            // 長寬比有關系
            this.mr.setVideoSize(
1280, 720);
            
this.mr.setVideoFrameRate(120);
            
// 設定預覽視窗
            this.mr.setPreviewDisplay(this.sv.getHolder().getSurface());
            
try {
               
this.mr.prepare();
               
this.mr.start();
            }
            
catch (IOException e) {
                Log.
e(TAG, e.getMessage(), e);
            }
        }
    }

   
@Override
    protected void onPause() {
        
super.onPause();
        
// 不顧一切,就是要停止並釋放
        if (this.mr != null) {
            
this.mr.stop();
            
this.mr.release();
        }
    }

   
private String createFilePath() {
        File sdCardDir = Environment.
getExternalStorageDirectory();
        File vrDir =
new File(sdCardDir, "cw1205");
        
if (!vrDir.exists()) {
            vrDir.mkdir();
        }
        File file =
new File(vrDir, System.currentTimeMillis() + ".mp4");
        String filePath = file.getAbsolutePath();
        Log.
d(TAG, "輸出路徑:" + filePath);
        
return filePath;
    }
}
        





檢舉 回應

分享