VideoView与SurfaceView的使用

VideoView

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

通过VideoView播放视频的步骤:

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

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

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

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

VideoView的使用:
1.1系统方法

<?xml version="1.0" encoding="utf-8"?>
<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:layout_height="match_parent"
    tools:context="com.example.android_playvideo.VideoActivity">

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

</LinearLayout>
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自定义

<?xml version="1.0" encoding="utf-8"?>
<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()<seekBar.getMax()){
                seekBar.setProgress(mediaPlayer.getCurrentPosition());
            }
        }
    }
}

配置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:

<?xml version="1.0" encoding="utf-8"?>
<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();
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值