视频播放,我们今天了解两种
1、使用VideoView播放视频
VideoView vv_main_video = (VideoView) findViewById(R.id.vv_main_video);
//给VideoView设置视频的来源
vv_main_video.setVideoPath("file://mnt/sdcar/tencent/QQfile_recv/VID20160807164557.mp4");
//实例化媒体控制器
MediaController mediaController=new MediaController(this);
vv_main_video.setMediaController(mediaController);
mediaController.setMediaPlayer(vv_main_video);
vv_main_video.start();
2、使用SurfaceView+MediaPlayer播放视
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
private GoogleApiClient client;
private SurfaceView sv_main_surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
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) {
//获取拖动后的进度
int progress=seekBar.getProgress();
mediaPlayer.seekTo(progress);
}
});
}
public void isPlayOrPause(View view) throws IOException {
final ImageButton imageButton = (ImageButton) view;
if (mediaPlayer == null) {
//应用音乐
// mediaPlayer = MediaPlayer.create(this, R.raw.a4);
mediaPlayer =new MediaPlayer();
//播放内存卡中的音乐
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置音频
mediaPlayer.setDataSource(this,Uri.parse("file://mnt/sdcar/tencent/QQfile_recv/VID20160807164557.mp4"));
// mediaPlayer.prepare();
//播放网络音乐
//mediaPlayer.setDataSource(this, Uri.parse("http://192.168.43.135/internet.mp3"));
//准备(异步准备)
// mediaPlayer.prepareAsync();
//设置准备完成的监听
// mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// @Override
// public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.setDisplay(sv_main_surface.getHolder());
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐的播放时长
int duration=mediaPlayer.getDuration();
//设置进度条的最大值为:音乐的播放时长
seekBar.setMax(duration);
new MyThread().start();
// }
// });
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐播放时长
int duraion = mediaPlayer.getDuration();
/*//设置进度条的最大值:音乐的播放时长
seekBar.setMax(duraion);
new MyThread().start();*/
} else if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
} else {
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while(seekBar.getProgress()<=seekBar.getMax()){
//获取音乐当前位置
int currertposition=mediaPlayer.getCurrentPosition();
seekBar.setProgress(currertposition);
}
}
}
}
二.拍照
拍照实现步骤:
<1>界面使用SurfaceView控件,显示照相机的画面
<2>SurfaceView接受摄像头界面变化的信息
surfaceView.getHolder().addCallback()
<3>重写三个方法
surfaceDestroyed--->当预览界面关闭时,触发
一般在这里执行销毁照相机的操作,释放内存
camera.stopPreview();
camera.release();
camera = null;
surfaceCreated----->预览界面被创建时,调用
一般在这里打开照相机和设置照相机的一些参数
// 打开摄像头
camera = Camera.open();
// 给摄像头设置参数
Parameters parameters = camera.getParameters();
// 设置每秒3栈
parameters.setPreviewFrameRate(3);
// 设置照片的输出格式
parameters.setPictureFormat(PixelFormat.JPEG);
// 设置照片的质量
parameters.set("jpeg-quality", 85);
// 给照相机设置参数
camera.setParameters(parameters);
//将摄像头的画面显示到SurfaceView控件上
camera.setPreviewDisplay(sv_main_camera.getHolder());
// 开始预览
camera.startPreview();
surfaceChanged----->当预览界面的格式和大小发生改变时,触发
<4>保存
camera.takePicture
将照片保存到内存卡(压缩)
实现步骤:
<1>将视频动画显示到SurfaceView控件上
<2>使用MediaRecorder类进行视频的录制
常用的方法:
mediaRecorder.reset();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoFrameRate(3); //每秒3帧
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp");
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare();//预期准备
mediaRecorder.start();//开始刻录
mediaRecorder.stop();//停止刻录
注意:权限
<uses-permission android:name="android.permission.RECORD_AUDIO"/>