视频播放器一:
1.布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.asus.android_26_mediaplayer.MainActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/sv_main_surface"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar_main"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="isPlay"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
2.MainActivity:
package com.asus.android_26_mediaplayer;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekbar_main;
private SurfaceView sv_main_surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar_main = (SeekBar) findViewById(R.id.seekbar_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
//给滚动条设置滑动监听事件
seekbar_main.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_main.getProgress();
//音乐随滚动条进度进行播放
mediaPlayer.seekTo(progress);
}
});
}
public void isPlay(View view){
final ImageButton imageButton= (ImageButton) view;
if (mediaPlayer==null){
//2.播放sd卡内音乐
mediaPlayer = new MediaPlayer();
//设置类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
//设置音源
mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Movies/sister.mp4"));
//准备一下
mediaPlayer.prepare();
//将媒体播放器捕捉的视屏展示到SurfaceView
mediaPlayer.setDisplay(sv_main_surface.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
//播放音乐
mediaPlayer.start();
//将播放图标变为暂停图标
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐总时长
int duration=mediaPlayer.getDuration();
//设置进度条最大值为音乐总时长
seekbar_main.setMax(duration);
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_main.getProgress()<=seekbar_main.getMax()){
//获取当前音乐播放位置
int currentPosition=mediaPlayer.getCurrentPosition();
//让进度条动起来
seekbar_main.setProgress(currentPosition);
}
}
}
}
3.不要忘记配置权限:
<!-- 读取系统文件权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
视频播放器二:
1.布局文件:
由于引用的系统控制器,所以只需一个VideoView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.asus.android_26_mediaplayer.VideoActivity">
<VideoView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/vv_main_video"/>
</RelativeLayout>
2.VideoActivity:
package com.asus.android_26_mediaplayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoActivity extends AppCompatActivity {
private VideoView vv_main_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
vv_main_video = (VideoView) findViewById(R.id.vv_main_video);
//给VideoView设置播放源
vv_main_video.setVideoPath("file://mnt/sdcard/Movies/sister.mp4");
MediaController controller=new MediaController(this);
vv_main_video.setMediaController(controller);
controller.setMediaPlayer(vv_main_video);
}
}
2.MainActivity:
3.配置权限
2. RecorderActivity:
1.布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.asus.android_26_mediaplayer.MainActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/sv_main_surface"/>
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/seekbar_main"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="isPlay"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
2.MainActivity:
package com.asus.android_26_mediaplayer;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekbar_main;
private SurfaceView sv_main_surface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekbar_main = (SeekBar) findViewById(R.id.seekbar_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
//给滚动条设置滑动监听事件
seekbar_main.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_main.getProgress();
//音乐随滚动条进度进行播放
mediaPlayer.seekTo(progress);
}
});
}
public void isPlay(View view){
final ImageButton imageButton= (ImageButton) view;
if (mediaPlayer==null){
//2.播放sd卡内音乐
mediaPlayer = new MediaPlayer();
//设置类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
//设置音源
mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Movies/sister.mp4"));
//准备一下
mediaPlayer.prepare();
//将媒体播放器捕捉的视屏展示到SurfaceView
mediaPlayer.setDisplay(sv_main_surface.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
//播放音乐
mediaPlayer.start();
//将播放图标变为暂停图标
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取音乐总时长
int duration=mediaPlayer.getDuration();
//设置进度条最大值为音乐总时长
seekbar_main.setMax(duration);
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_main.getProgress()<=seekbar_main.getMax()){
//获取当前音乐播放位置
int currentPosition=mediaPlayer.getCurrentPosition();
//让进度条动起来
seekbar_main.setProgress(currentPosition);
}
}
}
}
3.不要忘记配置权限:
<!-- 读取系统文件权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
视频播放器二:
1.布局文件:
由于引用的系统控制器,所以只需一个VideoView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.asus.android_26_mediaplayer.VideoActivity">
<VideoView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="@+id/vv_main_video"/>
</RelativeLayout>
2.VideoActivity:
package com.asus.android_26_mediaplayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoActivity extends AppCompatActivity {
private VideoView vv_main_video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
vv_main_video = (VideoView) findViewById(R.id.vv_main_video);
//给VideoView设置播放源
vv_main_video.setVideoPath("file://mnt/sdcard/Movies/sister.mp4");
MediaController controller=new MediaController(this);
vv_main_video.setMediaController(controller);
controller.setMediaPlayer(vv_main_video);
}
}
完成以上步骤即可。
拍照:
1.布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.asus.android_27_camera.MainActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_main_surface"
android:layout_weight="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照"
android:onClick="photo"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
2.MainActivity:
package com.asus.android_27_camera;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private SurfaceView sv_main_surface;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
//给sv_main_surface添加回调函数
sv_main_surface.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",85);
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(sv_main_surface.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
//开启预览效果
camera.startPreview();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
}
});
}
public void photo(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);
//图片压缩路径
try {
FileOutputStream fos=new FileOutputStream("mnt/sdcard/G150820_"+System.currentTimeMillis()+".jpeg");
//图片压缩的格式,质量及路径
bitmap.compress(Bitmap.CompressFormat.JPEG,85,fos);
camera.stopPreview();
camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
3.配置权限
<!-- 打开相机权限 --> <uses-permission android:name="android.permission.CAMERA" /> <!-- 创建文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- 写入内存卡权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
录像(录音):
1布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_recorder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.asus.android_27_camera.RecorderActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_recorder_surface"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:id="@+id/bt_start"
android:text="开始"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:layout_below="@+id/bt_start"
android:text="结束"
/>
</RelativeLayout>
2. RecorderActivity:
package com.asus.android_27_camera; import android.media.MediaRecorder; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.SurfaceView; import android.view.View; import java.io.IOException; public class RecorderActivity extends AppCompatActivity { private SurfaceView sv_recorder_surface; private MediaRecorder recorder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_recorder); sv_recorder_surface = (SurfaceView) findViewById(R.id.sv_recorder_surface); //实例化媒体录制器 recorder = new MediaRecorder(); } public void start(View view) throws IOException { //从照相机采集视频 recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从麦克风采集录音 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置格式 recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置视频编码方式 recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置录音编码方式 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置保存路径 recorder.setOutputFile("/mnt/sdcard/G150820_"+System.currentTimeMillis()+".mp4"); //画面展示出来 recorder.setPreviewDisplay(sv_recorder_surface.getHolder().getSurface()); //准备 recorder.prepare(); //开始录制 recorder.start(); } public void stop(View view){ if (recorder!=null){ recorder.stop(); recorder.release();; recorder=null; } } }
3.配置权限:
<!--录音的权限--> <uses-permission android:name="android.permission.RECORD_AUDIO"/>