Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) { Uri videoUri = data.getData(); vdoView.setVideoURI(videoUri); } }
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" android:required="true" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="Camera Recorder Videos" android:layout_span="1" android:textAppearance="?android:attr/textAppearanceLarge" /> </TableRow> <View android:layout_height="1dip" android:background="#CCCCCC" /> <TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.1" android:orientation="horizontal" > <VideoView android:id="@+id/vdoView" android:layout_width="wrap_content" android:layout_height="214dp" android:layout_weight="0.64" /> <Button android:id="@+id/btnRecorder" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Camera Recorder" /> <Button android:id="@+id/btnPlay" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play" /> </TableLayout> <View android:layout_height="1dip" android:background="#CCCCCC" /> <LinearLayout android:id="@+id/LinearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dip" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="By.. ThaiCreate.Com" /> </LinearLayout> </TableLayout>
package com.myapp; import android.os.Bundle; import android.provider.MediaStore; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.view.View; import android.view.Menu; import android.widget.Button; import android.widget.MediaController; import android.widget.VideoView; public class MainActivity extends Activity { VideoView vdoView; static final int REQUEST_VIDEO_CAPTURE = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // *** VideoView vdoView = (VideoView) findViewById(R.id.vdoView); // *** Camera Recorder final Button btnRecorder = (Button) findViewById(R.id.btnRecorder); // Perform action on click btnRecorder.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); if (takeVideoIntent.resolveActivity(getPackageManager()) != null) { startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE); } } }); // *** Play final Button btnPlay = (Button) findViewById(R.id.btnPlay); // Perform action on click btnPlay.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { vdoView.setMediaController(new MediaController(MainActivity.this)); vdoView.requestFocus(); vdoView.start(); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) { Uri videoUri = data.getData(); vdoView.setVideoURI(videoUri); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท