1. 程式人生 > >安卓實現視訊播放器

安卓實現視訊播放器

package com.example.g150825_android27;

import android.media.MediaRecorder;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceView;
import android.view.View;

import java.io.IOException;

/**
 * Created by Administrator on 2017/2/22 0022.
 */
public class mediarecorder extends AppCompatActivity{ private SurfaceView searchView; private MediaRecorder mediaRecorder; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_mediarecorder); searchView =(SurfaceView) findViewById(R.id.sv_main_surface); mediaRecorder = new
MediaRecorder(); } public void start(View view){ mediaRecorder.reset(); //設定視訊和音訊的來源 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //設定儲存的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //設定編碼格式
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoFrameRate(3); //設定儲存的路徑 mediaRecorder.setOutputFile("mnt/sdcard/DCIM/Camera/G150825_"+System.currentTimeMillis()+".mp4"); //將畫面展示到SurfaceView mediaRecorder.setPreviewDisplay(searchView.getHolder().getSurface()); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } public void stop(View view){ mediaRecorder.stop(); } }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<SurfaceView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lv_v_surface"
    ></SurfaceView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="開始"
        android:onClick="start"
        />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="暫停"
        android:onClick="stop"

        />



</LinearLayout>


</RelativeLayout>