VideoView与SurfaceView的使用
2017-07-21 21:34
337 查看
VideoView
概述:
这个类其实也是继承了SurfaceView 类,集成度高,开发难度小,只是灵活性差。
通过VideoView播放视频的步骤:
1、在界面布局文件中定义VideoView组件,或在程序中创建VideoView组件
2、调用VideoView的如下两个方法来加载指定的视频
setVidePath(String path):加载path文件代表的视频
setVideoURI(Uri uri):加载uri所对应的视频
VideoView的使用:
1.1系统方法
1.2自定义
配置xml:
SurfaceView:
概述:
这个类其实也是继承了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>
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(); c35e 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(); } }); } }
相关文章推荐
- VideoView与SurfaceView的使用
- Android视频播放框架Vitamio集成及使用MediaPlayer+SurfaceView、VideoView+MediaController实现播放网络视频
- 使用VideoView 或者SurfaceView 实现视频播放功能
- vitamio 开源框架使用(VideoView)
- surfaceview, videoview,texturalview区别
- 使用VideoView播放视频,锁屏暂停播放之后,过一会自动播放,而且可以通过滑动屏幕调节音量。。。
- Android SurfaceView的理解与使用
- android中使用surfaceview+MediaPlayer播放视频
- android-tip-关于SurfaceView的使用
- Android 使用MediaPlayer与SurfaceView进行视频播放
- 如何去使用一个SurfaceView
- Android中SurfaceView的使用详解
- 使用MediaPlayer和SurfaceView播放视频
- Android UI SurfaceView的使用-绘制组合图型,并使其移动
- VideoView 和 SurfaceView 实例
- SurfaceView的简单使用_熊德海
- (转)【Android游戏开发十四】深入Animation,在SurfaceView中照样使用Android—Tween Animation!
- [Android] SurfaceView使用实例(正余弦移动)
- SurfaceView基本使用--动态画正弦函数
- 安卓MediaPlayer和VideoView简单使用