VideoView与SurfaceView的使用

VideoView

概述:
这个类其实也是继承了SurfaceView 类,集成度高,开发难度小,只是灵活性差。

通过VideoView播放视频的步骤:

1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件

2、调用VideoView的如下两个方法来加载指定的视频

setVidePath(String path):加载path文件代表的视频

setVideoURI(Uri uri):加载uri所对应的视频

VideoView的使用:
1.1系统方法

"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android_playvideo.VideoActivity">

    "match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vv_mian_Video"
        />


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.example.android_playvideo;

import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
         VideoView videoView= (VideoView) findViewById(R.id.vv_mian_Video);
        videoView.setVideoURI(Uri.parse("file://mnt/sdcard/dcim/camera/VID20170211215528.mp4"));
        MediaController mediaController=new MediaController(this);
        mediaController.setMediaPlayer(videoView);
        videoView.setMediaController(mediaController);
    }

}

1.2自定义


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent" tools:context="com.example.android_playvideo.MainActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/sfv_mian_surface"
        />

    <SeekBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/sb_mian_seekbar"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="开始"
        android:onClick="start"
        />

LinearLayout>
package com.example.android_playvideo;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.widget.SeekBar;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private SurfaceView surfaceView;
    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.sb_mian_seekbar);
        surfaceView = (SurfaceView) findViewById(R.id.sfv_mian_surface);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mediaPlayer.seekTo(seekBar.getProgress());
            }
        });

    }
    public void start(View view){
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
        if(mediaPlayer==null){
         String path=   Environment.getExternalStorageDirectory().getAbsolutePath();
            Log.i("text",""+path);
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            try {
                mediaPlayer.setDataSource(this, Uri.parse("file://"+path+"/dcim/camera/VID20170211215528.mp4"));
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.setDisplay(surfaceView.getHolder());
             int itme=  mediaPlayer.getDuration();
            Log.i("text",""+itme);
            seekBar.setMax(itme);
            mediaPlayer.start();
            new mySeekbar().start();
        }else if(mediaPlayer.isPlaying()){
            mediaPlayer.pause();
        }else{
            mediaPlayer.start();
        }
        }
    }
    class mySeekbar extends  Thread{
        @Override
        public void run() {
            while(seekBar.getProgress()

配置xml:

   
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
      
    <uses-permission android:name="android.permission.CAMERA">uses-permission>  
      
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS">uses-permission>  
      
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">uses-permission>

SurfaceView:


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_playvideo.CameraActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sfv_mian_View"
        />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="拍照"
        android:onClick="paizhao"
        />

RelativeLayout>
package com.example.android_playvideo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

public class CameraActivity extends AppCompatActivity {

    private SurfaceView surfaceView;
    private Camera camera;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        surfaceView = (SurfaceView) findViewById(R.id.sfv_mian_View);
        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //打开摄像头
             camera=  Camera.open();
                //设置摄像头的参数
               /* Camera.Parameters parameters=camera.getParameters();
                parameters.setPictureFormat(PixelFormat.JPEG);
                parameters.set("jpeg-quality",100);
                camera.setParameters(parameters);*/
                //开启预览效果
                camera.startPreview();
                //将画面展示到surfaceView
                try {
                    camera.setPreviewDisplay(surfaceView.getHolder());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                camera.stopPreview();
                camera.release();
            }
        });
    }
    public void paizhao(View view){
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
              Bitmap bitmap= BitmapFactory.decodeByteArray(data,0,data.length);
                Calendar calendar= Calendar.getInstance();
              int year=  calendar.get(Calendar.YEAR);
              int month=  calendar.get(Calendar.MONTH);
              int day= calendar.get(Calendar.DAY_OF_MONTH);
                try {
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/camera/G160628_"+year+(month+1)+day+"_"+System.currentTimeMillis()+".jpg");//保存路径
                    //压缩
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                camera.stopPreview();
                camera.startPreview();
            }
        });
    }
}

你可能感兴趣的:(android)