Back to project page playground-android-video.
The source code is released under:
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...
If you think the Android project playground-android-video listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package v.app.Fragments; //from w w w.j ava2 s .c om import android.hardware.Camera; import android.os.Environment; import android.support.v4.app.Fragment; import android.media.MediaRecorder; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import java.io.File; import java.io.FileOutputStream; import v.app.R; /** * Created by vasanth on 26/03/14. */ public class RecorderFragment extends Fragment { private MediaRecorder mediaRecorder = new MediaRecorder(); private Button recordButton, pauseButton, resumeButton; private SurfaceHolder surfaceHolder; private SurfaceView surfaceView; private Camera camera; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_recorder, container, false); recordButton = (Button) rootView.findViewById(R.id.recordButton); pauseButton = (Button) rootView.findViewById(R.id.pauseButton); resumeButton = (Button) rootView.findViewById(R.id.resumeButton); surfaceView = (SurfaceView) rootView.findViewById(R.id.surfaceView); initButtons(); return rootView; } private void initButtons() { File mediaDirectory = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_MOVIES); String filename = "abc.mp4"; File file = new File(mediaDirectory, filename); final String path = mediaDirectory.getPath() +"/"+ filename; Log.v("V-app", path); recordButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startRecording(path); } }); pauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pauseRecording(path); } }); resumeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { resumeRecording(); } }); } public void startRecording(String path) { mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //Camera camera = Camera.open(); //mediaRecorder.setCamera(camera); //Log.v("V-app", camera.getParameters().toString()); try { mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); mediaRecorder.setOutputFile(path); mediaRecorder.prepare(); } catch(Exception e) { e.printStackTrace(); } } public void pauseRecording(String path) { mediaRecorder.stop(); try { FileOutputStream pausedFile = new FileOutputStream(path); mediaRecorder.setOutputFile(pausedFile.getFD()); } catch(Exception e) { e.printStackTrace(); } } public void resumeRecording() { try { mediaRecorder.prepare(); } catch(Exception e) { e.printStackTrace(); } mediaRecorder.start(); } }