Advertisement
Guest User

hwrd

a guest
Jul 11th, 2010
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. package com.taggstr.AndroidApp;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.media.MediaRecorder;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.util.Log;
  12. import android.view.Surface;
  13. import android.view.SurfaceHolder;
  14. import android.view.SurfaceView;
  15. import android.view.View;
  16. import android.widget.Button;
  17.  
  18. import com.taggstr.tools.AttachedImageAdapter;
  19.  
  20. public class VideoActivity extends Activity {
  21.  
  22.     private SurfaceView preview;
  23.     private SurfaceHolder previewHolder;
  24.     private String locationName;
  25.     private String filepath;
  26.     private File video;
  27.  
  28.     public void onCreate(Bundle videocawk) {
  29.         super.onCreate(videocawk);
  30.         setContentView(R.layout.video_layout);
  31.         setSurface();
  32.         locationName = getIntent().getStringExtra("locationName");
  33.         filepath = getFilePath(locationName);
  34.         try {
  35.             MediaRecorder r = getMediaRecorder(filepath, previewHolder
  36.                     .getSurface());
  37.             setButtonListeners(r);
  38.         } catch (IllegalStateException e) {
  39.             // TODO Auto-generated catch block
  40.             e.printStackTrace();
  41.         } catch (IOException e) {
  42.             // TODO Auto-generated catch block
  43.             e.printStackTrace();
  44.         }
  45.     }
  46.  
  47.     private String getFilePath(String locName) {
  48.         String dir = Environment.getExternalStorageDirectory().getPath();
  49.         String add = "/Taggstr/data/video/";
  50.         String name = locName + " -- 1";
  51.         String total = dir + add + name;
  52.         video = new File(total);
  53.         return total;
  54.     }
  55.  
  56.     private void setSurface() {
  57.         preview = (SurfaceView) findViewById(R.id.preview);
  58.         previewHolder = preview.getHolder();
  59.         previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  60.     }
  61.  
  62.     private void setButtonListeners(final MediaRecorder r) {
  63.         Button start = (Button) findViewById(R.id.start_video);
  64.         Button end = (Button) findViewById(R.id.stop_video);
  65.  
  66.         start.setOnClickListener(new View.OnClickListener() {
  67.  
  68.             public void onClick(View v) {
  69.                 startRecording(r);
  70.  
  71.             }
  72.         });
  73.  
  74.         end.setOnClickListener(new View.OnClickListener() {
  75.  
  76.             public void onClick(View v) {
  77.                 stopRecording(r);
  78.                 setPassPrefs();
  79.                 startActivity(setPassPrefs());
  80.                 finish();
  81.  
  82.             }
  83.         });
  84.  
  85.     }
  86.  
  87.     private Intent setPassPrefs() {
  88.         AttachedImageAdapter adapter = new AttachedImageAdapter(locationName,
  89.                 VideoActivity.this);
  90.         adapter.setVideoPath(filepath);
  91.         Intent i = new Intent(VideoActivity.this, EnterTag.class);
  92.         i.putExtras(getIntent());
  93.         return i;
  94.  
  95.     }
  96.  
  97.     private void startRecording(MediaRecorder r) {
  98.         r.start();
  99.     }
  100.  
  101.     private void stopRecording(MediaRecorder r) {
  102.         r.stop();
  103.     }
  104.  
  105.     private MediaRecorder getMediaRecorder(String filepath, Surface s)
  106.             throws IllegalStateException, IOException {
  107.         MediaRecorder m_recorder = new MediaRecorder();
  108.         m_recorder.setPreviewDisplay(s);
  109.         m_recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
  110.         m_recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
  111.         m_recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  112.         m_recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
  113.         m_recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
  114.         m_recorder.setMaxDuration(20000); // length of video in MS
  115.         m_recorder.setVideoSize(320, 240);
  116.         m_recorder.setVideoFrameRate(15);
  117.         m_recorder.setOutputFile(video.getPath());
  118.         m_recorder.prepare();
  119.  
  120.         return m_recorder;
  121.     }
  122.  
  123. }
  124.  
  125.  
  126. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  127. ////////LAYOUT HERE//////////////////////////////////////////////////////////////////////////////////////////////////////
  128. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  129.  
  130. <?xml version="1.0" encoding="utf-8"?>
  131. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  132.     android:id="@+id/camera_layout" android:layout_height="fill_parent"
  133.     android:layout_width="fill_parent">
  134.     <android.view.SurfaceView android:id="@+id/preview"
  135.         android:layout_width="wrap_content" android:layout_height="wrap_content"
  136.         android:layout_alignParentTop="true">
  137.     </android.view.SurfaceView>
  138.     <Button android:id="@+id/start_video" android:layout_width="wrap_content"
  139.         android:layout_height="wrap_content" android:text="Start Video">
  140.     </Button>
  141.     <Button android:id="@+id/stop_video" android:layout_width="wrap_content"
  142.         android:layout_height="wrap_content" android:text="Stop Video"
  143.         android:layout_toRightOf="@id/start_video">
  144.     </Button>
  145. </RelativeLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement