在Android中录制,保存和播放视频

前端之家收集整理的这篇文章主要介绍了在Android中录制,保存和播放视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试制作一个使用相机应用程序录制视频的应用程序,然后将该视频保存在SD卡上,以便我可以播放它.我有一些代码,但我已经迷失了如何继续,因为我是 Android的初学者.

我的活动:

public class Camcorder extends Activity {

     private CamcorderView camcorderView; 
     private boolean recording = false; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
          super.onCreate(savedInstanceState);
          //irrelevant code

          camcorderView = (CamcorderView) findViewById(R.id.camcorder_preview); 
     } 

     @Override 
     public boolean onKeyDown(int keyCode,KeyEvent event) 
     { 
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) 
         { 
          if (recording) { 
                camcorderView.stopRecording();
                finish(); 
            } else { 
                recording = true; 
                camcorderView.startRecording(); 
            } 
             return true; 
         } 
         return super.onKeyDown(keyCode,event); 
     }       
}

CamcorderView类:

public class CamcorderView extends SurfaceView implements
    SurfaceHolder.Callback {

MediaRecorder recorder;
SurfaceHolder holder;
String outputFile = "/sdcard/default.mp4";

public CamcorderView(Context context,AttributeSet attrs) {
    super(context,attrs);

    holder = getHolder();
    holder.addCallback(this);
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    // recorder.setVideoSize(480,320);
    // recorder.setVideoFrameRate(15);
    // recorder.setMaxDuration(10000);
}

public void surfaceCreated(SurfaceHolder holder) {
    recorder.setOutputFile(outputFile);
    recorder.setPreviewDisplay(holder.getSurface());
    if (recorder != null) {
        try {
            recorder.prepare();
        } catch (IllegalStateException e) {
            Log.e("IllegalStateException",e.toString());
        } catch (IOException e) {
            Log.e("IOException",e.toString());
        }
    }
}

public void surfaceChanged(SurfaceHolder holder,int format,int width,int height) {
}

public void surfaceDestroyed(SurfaceHolder holder) {
}

public void setOutputFile(String filename)
{
    outputFile = filename;
    recorder.setOutputFile(filename);
}

public void startRecording()
{
    recorder.start();
}

public void stopRecording()
{
    recorder.stop();
    recorder.release();
}
}

解决方法

嘿,我想你没试过这个.所以请检查此链接,如果您有任何问题,请告诉我
video recording example
原文链接:https://www.f2er.com/android/309181.html

猜你在找的Android相关文章