如何记录和保存,并在Android中播放视频 [英] How to record and save and play a video in Android
问题描述
它可能看起来像一个愚蠢的或旧的问题,但我不能够找到从previous问题适当的解决方案。所以,请尝试在一个简单的和理解的方式来回答。
我在Android的初学者。我试图让一个应用程序中,我可以使用摄像头的Android手机上录制视频。我想保存在SD卡上的视频,所以我可以再次播放视频。我走过整个的previous的问题和可能的答案和谷歌也看了,但不能这样做。
任何帮助鸭preciated。 :)
这是我的资料来源$ C $ C
it may seem like a stupid or old question but am not able to find an appropriate solution from previous questions. So please try to answer in a easy and understanding way. I am a beginner in Android. I am trying to make an app in which i can record video using camera on Android phone. I want to save that video on sd card so i can play again that video. I have looked through whole of the previous questions and possible answers and on Google also but couldn't do it. Any Help Appreciated. :) Here is my Source code
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);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.camcorder_preview);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
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类的源$ C $ C
Now this is the source code of CamcorderView class
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();
}
}
推荐答案
嘿,我想你没有尝试这一个。所以请检查该链接,如果您有任何问题,然后让我知道
录像例如
hey i think you didn't try this one. so please check this link and if you have any problem then let me know video recording example
这篇关于如何记录和保存,并在Android中播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!