Android 錄製視頻

來源:互聯網
上載者:User

標籤:android   private   package   import   public   

Activity代碼:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

package eoe.demo.Media;import java.io.File;import java.io.IOException;import android.app.Activity;import android.media.MediaRecorder;import android.os.Bundle;import android.os.Environment;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class VideoActivity extends Activity {private File myRecAudioFile;private SurfaceView mSurfaceView;private SurfaceHolder mSurfaceHolder;private Button buttonStart;private Button buttonStop;private File dir;private MediaRecorder recorder;@Overridepublic void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.video);  mSurfaceView = (SurfaceView) findViewById(R.id.videoView);  mSurfaceHolder = mSurfaceView.getHolder();  mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);  buttonStart=(Button)findViewById(R.id.start);  buttonStop=(Button)findViewById(R.id.stop);  File defaultDir = Environment.getExternalStorageDirectory();  String path = defaultDir.getAbsolutePath()+File.separator+"V"+File.separator;//建立檔案夾存放視頻  dir = new File(path);  if(!dir.exists()){    dir.mkdir();  }  recorder = new MediaRecorder();  buttonStart.setOnClickListener(new OnClickListener() {@Override  public void onClick(View v) {    recorder();  }});buttonStop.setOnClickListener(new OnClickListener() {@Override   public void onClick(View v) {    recorder.stop();    recorder.reset();    recorder.release();    recorder=null;   }  });}public void recorder() {try {  myRecAudioFile = File.createTempFile("video", ".3gp",dir);//建立臨時檔案  recorder.setPreviewDisplay(mSurfaceHolder.getSurface());//預覽  recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//視頻源  recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //錄音源為麥克風  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//輸出格式為3gp  recorder.setVideoSize(800, 480);//視頻尺寸  recorder.setVideoFrameRate(15);//視訊框架頻率  recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//視頻編碼  recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音頻編碼  recorder.setMaxDuration(10000);//最大期限  recorder.setOutputFile(myRecAudioFile.getAbsolutePath());//儲存路徑  recorder.prepare();  recorder.start();} catch (IOException e) {  e.printStackTrace();    }   }}

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

布局代碼:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

    <?xml version="1.0" encoding="utf-8"?>    <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"      android:layout_width="fill_parent"      android:layout_height="fill_parent">    <SurfaceView android:id="@+id/videoView"      android:visibility="visible"      android:layout_width="320px"      android:layout_height="240px">    </SurfaceView>    <RelativeLayout      android:layout_width="fill_parent"      android:layout_height="wrap_content">    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:text="錄製"      android:id="@+id/start"/>    <Button      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:layout_toRightOf="@id/start"      android:text="停止"      android:id="@+id/stop"/>    </RelativeLayout>    </LinearLayout>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

AndroidManifest.xml:

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

   <!-- 授予該程式錄製聲音的許可權 -->    <uses-permission android:name="android.permission.RECORD_AUDIO"/>    <!-- 授予該程式使用網路攝影機的許可權 -->        <uses-permission android:name="android.permission.CAMERA"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <!-- 授予使用外部儲存空間的許可權 -->    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

650) this.width=650;" src="/img/fz.gif" alt="複製代碼" style="border:0px;" />

 


Android 錄製視頻

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.