一、播放音频的API
1.1 MediaPlay

public void play(View v) {
	String path = et_path.getText().toString();
	
	//1.判空操作,2,判断是否真的存在这个文件,有可能用户输入的是非法的文件路径
	
	
	try {
		//1,创建一个多媒体播放器
		mediaPlayer = new MediaPlayer();
		//2.设置播放资源
		mediaPlayer.setDataSource(path);
		//设置播放错误监听--一般是音频文件损坏了。。
	/*	mediaPlayer.setOnErrorListener(new OnErrorListener() {
			
			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				return false;
			}
		});*/
		
		//3.准备一下
		mediaPlayer.prepare();
		
		//4. 开始播放
		mediaPlayer.start();
		
		//播放完毕的监听,如果已经播放完毕,那么可以播放下一首歌,或者继续播放当前的这首歌
		mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
			
			@Override
			public void onCompletion(MediaPlayer mp) {
				mediaPlayer.seekTo(0);
				mediaPlayer.start();
			}
		});
	} catch (Exception e) {
		e.printStackTrace();
	}
}

public void pause(View v) {
	
	if(mediaPlayer.isPlaying()){
		mediaPlayer.pause();
	}else{
		mediaPlayer.start();
	}
}

1.2 SoundPool ---声音池


一般用来播放游戏的音效,大量密集的出现的时候


public class MainActivity extends Activity {
	int id ; 
	SoundPool pool ; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		pool = new SoundPool(
				3,  //最多能放多少道音频文件
				 AudioManager.STREAM_MUSIC,  // 存储的流类型是什么类型
				0); //没有生命影响,给0
	
		//加载音频文件到池子里面。
		id = pool.load(this, R.raw.shoot, 1);
	}
	
	public void shoot(View v){
		pool.play(id, 1, 1, 0, 0, 1.0f);
	}
}

1.3 视频播放


public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		//1.找到播放视频的控件
		VideoView vv = (VideoView) findViewById(R.id.vv);
		
		//2.指定播放的视频源
		vv.setVideoPath("/mnt/sdcard/oppo.3gp");
		
		MediaController controller = new MediaController(this);
		controller.setAnchorView(vv);
		
		//设置视频的控制器  暂停、播放、快进、快退
		vv.setMediaController(controller);
		
		//3.开始播放
		vv.start();
	}
}

二、SurfaceView


(1)可以单位时间内大量的渲染、显示不同的画面


(2)内部使用的是双缓冲机制


(3)surfaceView是一个比较耗费内存的控件,系统不会一开始上来就渲染这个 控件,而是等界面能够看到的时候才去渲染这个控件,才去显示它。那由于一开始就在onCreate方法里面 ,所以画布是空的。


2.1 在子线程中更新UI


/**
 * surfaceView使用的是双缓冲的机制,所以可以单位时间内更新大量的画面
 * 
 * 线程0: 显示画面---计算数据 ---显示画面
 * 线程1: 计算数据----显示画面---计算数据
 * 
 * 可以在子线程里面更新UI  ProgressBar 进度条
 *
 */
public class MainActivity extends Activity {


	SurfaceView sv ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sv = (SurfaceView) findViewById(R.id.sv);
		
		new Thread(){
			public void run() {
				SystemClock.sleep(1000);
				//获取到surfaceview的控制器
				SurfaceHolder holder = sv.getHolder();
				int radius = 5; 
				
				for (int i = 0; i < 10; i++) {
					//由于界面没显示,所以画布是空的。
					Canvas canvas = holder.lockCanvas();
					canvas.drawColor(Color.BLACK);
					Paint paint = new Paint();
					paint.setColor(Color.RED);
					radius +=i;
					canvas.drawCircle(100, 100, radius, paint);
					//解锁画布
					holder.unlockCanvasAndPost(canvas);
					SystemClock.sleep(100);
				}
			};
		}.start();
		
	}
	
	public void update(View v){
		new Thread(){
			public void run() {
				//获取到surfaceview的控制器
				SurfaceHolder holder = sv.getHolder();
				int radius = 5; 
				
				for (int i = 0; i < 10; i++) {
					Canvas canvas = holder.lockCanvas();
					canvas.drawColor(Color.BLACK);
					Paint paint = new Paint();
					paint.setColor(Color.RED);
					radius +=i;
					canvas.drawCircle(100, 100, radius, paint);
					//解锁画布
					holder.unlockCanvasAndPost(canvas);
					SystemClock.sleep(100);
				}
			};
		}.start();
	}
}

2.2 SurfaceView的生命周期


SurfaceView可见的时候执行SurfaceCreate方法,不可见的时候调用SurfaceDestroy方法。


/**
 * surfaceView使用的是双缓冲的机制,所以可以单位时间内更新大量的画面
 * 
 * 线程0: 显示画面---计算数据 ---显示画面
 * 线程1: 计算数据----显示画面---计算数据
 * 
 * 可以在子线程里面更新UI  ProgressBar 进度条
 *
 */
public class MainActivity extends Activity {

	SurfaceView sv ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
				
		sv	= (SurfaceView) findViewById(R.id.sv);
		
		//1.得到surfaceView的控制器
		SurfaceHolder holder = sv.getHolder();
		
		
		holder.addCallback(new Callback() {
			
			//当surfaceView销毁的时候调用
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				System.out.println("---surfaceDestroyed--");
			}
			
			//当surfaceView创建的时候调用
			@Override
			public void surfaceCreated(final SurfaceHolder holder) {
				System.out.println("---surfaceCreated--"+Thread.currentThread().getName());
				//获取到surfaceview的控制器
				//SurfaceHolder holder = sv.getHolder();
				
				new Thread(){
					public void run() {
						int radius = 5; 
						
						for (int i = 0; i < 10; i++) {
							//由于界面没显示,所以画布是空的。
							Canvas canvas = holder.lockCanvas();
							canvas.drawColor(Color.BLACK);
							Paint paint = new Paint();
							paint.setColor(Color.RED);
							radius +=i;
							canvas.drawCircle(100, 100, radius, paint);
							//解锁画布
							holder.unlockCanvasAndPost(canvas);
							SystemClock.sleep(100);
						}
					};
				}.start();
			}
			
			//当surfaceView发生改变的时候调用
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				System.out.println("---surfaceChanged--");
				
			}
		});
	
		/*
		new Thread(){
			public void run() {
				SystemClock.sleep(1000);
				//获取到surfaceview的控制器
				SurfaceHolder holder = sv.getHolder();
				int radius = 5; 
				
				for (int i = 0; i < 10; i++) {
					//由于界面没显示,所以画布是空的。
					Canvas canvas = holder.lockCanvas();
					canvas.drawColor(Color.BLACK);
					Paint paint = new Paint();
					paint.setColor(Color.RED);
					radius +=i;
					canvas.drawCircle(100, 100, radius, paint);
					//解锁画布
					holder.unlockCanvasAndPost(canvas);
					SystemClock.sleep(100);
				}
			};
		}.start();*/
		
	}

	public void update(View v){
		new Thread(){
			public void run() {
				//获取到surfaceview的控制器
				SurfaceHolder holder = sv.getHolder();
				int radius = 5; 
				
				for (int i = 0; i < 10; i++) {
					Canvas canvas = holder.lockCanvas();
					canvas.drawColor(Color.BLACK);
					Paint paint = new Paint();
					paint.setColor(Color.RED);
					radius +=i;
					canvas.drawCircle(100, 100, radius, paint);
					//解锁画布
					holder.unlockCanvasAndPost(canvas);
					SystemClock.sleep(100);
				}
			};
		}.start();
	}
}

2.3 SurfaceView播放视频


public class MainActivity extends Activity {
	
	MediaPlayer mediaPlayer;
	SharedPreferences sp ; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		sp = getSharedPreferences("config", 0);
		SurfaceView sv = (SurfaceView) findViewById(R.id.sv);
		
		sv.getHolder().addCallback(new Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				if(mediaPlayer != null){
					//获取到当前的播放位置
					int position = mediaPlayer.getCurrentPosition();
					Editor editor = sp.edit();
					editor.putInt("position", position);
					editor.commit();
					mediaPlayer.release();
					mediaPlayer = null;
				}
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				//应该在这个地方去播放视频了。
				try {
					int position = sp.getInt("position", 0);
					mediaPlayer = new MediaPlayer();
					mediaPlayer.setDataSource("/mnt/sdcard/oppo.3gp");
					mediaPlayer.prepare();
					//指定播放的画面显示到holder里面去,实际上就是显示到surfaceView里面去
					mediaPlayer.setDisplay(holder);
					mediaPlayer.seekTo(position);
					mediaPlayer.start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				
			}
		});
	}
}

三、拍照


public class MainActivity extends Activity {
	 File file  ;
	ImageView iv ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		iv = (ImageView)findViewById(R.id.iv);
	}

	public void click(View v){		
	    // create Intent to take a picture and return control to the calling application
	    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

	   // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
	    	    
	    file = new File("/mnt/sdcard/"+System.currentTimeMillis()+".jpg");
	    Uri fileUri = Uri.fromFile(file);
	    
	    //这个传递的数值代表的意思是:告诉系统照相机,拍完照片之后,把那张照片保存到给你的uri路径下
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

	    // start the image capture Intent
	    startActivityForResult(intent, 1);

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//转化图片
		if(file != null){
			Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
			iv.setImageBitmap(bitmap);
		}
	}
}

四、录制视频


public class MainActivity extends Activity {
	 File file  ;
	VideoView vv ; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		vv = (VideoView)findViewById(R.id.vv);
	}

	public void click(View v){
	    // create Intent to take a picture and return control to the calling application
	    Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);


	   // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
	    
	    
	    file = new File("/mnt/sdcard/"+System.currentTimeMillis()+".3gp");
	    Uri fileUri = Uri.fromFile(file);
	    
	    //这个传递的数值代表的意思是:告诉系统照相机,拍完照片之后,把那张照片保存到给你的uri路径下
	    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
	    
	    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high

	    // start the image capture Intent
	    startActivityForResult(intent, 1);

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		//转化图片
		if(file != null){
			vv.setVideoPath(file.getAbsolutePath());
			
			MediaController controller = new MediaController(this);
			controller.setAnchorView(vv);
			
			vv.setMediaController(controller);
			
			vv.start();
		}
	}
}