package com.taggstr.AndroidApp;
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import com.taggstr.tools.AttachedImageAdapter;
public class VideoActivity extends Activity {
private SurfaceView preview;
private SurfaceHolder previewHolder;
private String locationName;
private String filepath;
private File video;
public void onCreate(Bundle videocawk) {
super.onCreate(videocawk);
setContentView(R.layout.video_layout);
setSurface();
locationName = getIntent().getStringExtra("locationName");
filepath = getFilePath(locationName);
try {
MediaRecorder r = getMediaRecorder(filepath, previewHolder
.getSurface());
setButtonListeners(r);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private String getFilePath(String locName) {
String dir = Environment.getExternalStorageDirectory().getPath();
String add = "/Taggstr/data/video/";
String name = locName + " -- 1";
String total = dir + add + name;
video = new File(total);
return total;
}
private void setSurface() {
preview = (SurfaceView) findViewById(R.id.preview);
previewHolder = preview.getHolder();
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private void setButtonListeners(final MediaRecorder r) {
Button start = (Button) findViewById(R.id.start_video);
Button end = (Button) findViewById(R.id.stop_video);
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startRecording(r);
}
});
end.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
stopRecording(r);
setPassPrefs();
startActivity(setPassPrefs());
finish();
}
});
}
private Intent setPassPrefs() {
AttachedImageAdapter adapter = new AttachedImageAdapter(locationName,
VideoActivity.this);
adapter.setVideoPath(filepath);
Intent i = new Intent(VideoActivity.this, EnterTag.class);
i.putExtras(getIntent());
return i;
}
private void startRecording(MediaRecorder r) {
r.start();
}
private void stopRecording(MediaRecorder r) {
r.stop();
}
private MediaRecorder getMediaRecorder(String filepath, Surface s)
throws IllegalStateException, IOException {
MediaRecorder m_recorder = new MediaRecorder();
m_recorder.setPreviewDisplay(s);
m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
m_recorder.setMaxDuration(20000); // length of video in MS
m_recorder.setVideoSize(320, 240);
m_recorder.setVideoFrameRate(15);
m_recorder.setOutputFile(video.getPath());
m_recorder.prepare();
return m_recorder;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////LAYOUT HERE//////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/camera_layout" android:layout_height="fill_parent"
android:layout_width="fill_parent">
<android.view.SurfaceView android:id="@+id/preview"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</android.view.SurfaceView>
<Button android:id="@+id/start_video" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Start Video">
</Button>
<Button android:id="@+id/stop_video" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Stop Video"
android:layout_toRightOf="@id/start_video">
</Button>
</RelativeLayout>