安卓笔记:多媒体应用开发

音频和视频的播放

Android提供了简单的API来播放音频视频,支持的音频格式有MP3,WAV,3GP等,支持的视频格式有MP4,3GP等

安卓9增强的MediaPlayer

音乐特效控制

使用AudioEffect及其子类

SoundPool播放音效

主要用于播放一些短的声音片段。使用音效池的概念来管理多个短促的音效。与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低,反映延迟小。SoundPool支持自行设置声音的品质,音量,播放比率等参数。

Builder内部类专门用于创建SoundPool。

VideoView播放视频

使用VideoView播放视频的步骤

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

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

  • setVideopath(String path):加载path文件所代表的视频
  • setVideoURI(Uri uri):加载uri所对应的视频

3.调用VideoView的start(),stop(),pause()方法来控制视频播放

部分代码:

xml

	
	

MainActivity.java 

public class MainActivity extends Activity
{
	private VideoView videoView;
	private MediaController mController;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 获取界面上的VideoView组件
		videoView = findViewById(R.id.video);
		// 创建MediaController对象
		mController = new MediaController(this);
		requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0x123);
	}

	@Override public void onRequestPermissionsResult(int requestCode,
		@NonNull String[] permissions, @NonNull int[] grantResults)
	{
		if (requestCode == 0x123
				&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
			// 设为横屏
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			File video = new File("/mnt/sdcard/movie.mp4");
			if (video.exists()) {
				videoView.setVideoPath(video.getAbsolutePath()); // ①
				// 设置videoView与mController建立关联
				videoView.setMediaController(mController);  // ②
				// 设置mController与videoView建立关联
				mController.setMediaPlayer(videoView);  // ③
				// 让VideoView获取焦点
				videoView.requestFocus();
				videoView.start(); // 开始播放
			}
		}
	}
}

配置 

	
	

MediaPlayer和SurfaceView播放视频

使用MediaPlayer播放音频,使用SurfaceView来显示MediaPlayer播放的图像输出。

步骤:

1.创建MediaPlayer对象,并让它加载指定的视频文件

2.在界面布局文件中定义SurfaceView组件,或在程序中创建SurfaceView组件,并为SurfaceView的SurfaceHolder添加Callback监听器

3.调用MediaPlayer对象的setDisplay(SurfaceHolder sh)方法将所播放的视频图像输出到指定的SurfaceView组件

4.调用MediaPlayer对象的start(),stop()和pause()方法控制视频的播放。

使用MediaRecorder录制音频

MediaRecorder类

控制摄像头拍照(详细)

Camera v2主要涉及如下API

  • CameraManager:摄像头管理器
  • CameraCharateristics:摄像头特性
  • CameraDevice:代表系统摄像头
  • CameraCaptureSession:当程序需要预览、拍照时,都需要先通过该类的实例创建Session。

控制其预览的方法为setRepeatingRequest();控制拍照的方法为capture()

为了监听CameraCaptureSession的差un构建过程以及监听CameraCaptureSession的拍照过程,Camera v2 API为CameraCaptureSession提供了StateCallback,CaptureCallback等内部类

  • CameraRequest和CameraRequest.Builder:当程序调用setRepeatingRequset()方法进行浏览时,或调用capture()方法进行拍照时,都需要传入CameraRequest参数。CameraRequest代表了一次捕获请求,用于描述图片的各种参数设置。CameraRequest.Builder负责生成CameraRequest对象。

 

Android 9对相机API进行了进一步增强,支持双摄像头,多摄像头。实现功能有:无缝缩放、散景、立体效果等。还允许调用合适或融合的相机数据流,以便在不同的摄像头之间切换。

控制拍照的步骤:

1.调用CameraManager的openCamer(String cameraId, CameraDevice.SteteCallback callback,Handler handler)方法打开制定摄像头。

2.摄像头被打开之后,程序即可获取CamerDevice(即根据摄像头ID获取制定摄像头设备,然后调用CamerDevice的createCaptureSession(Listoutputs,CameraCaptureSession.StateCallback callback,Handler handler)方法来创建CameraCaptureSession)

3.程序调用CameraDevice的createCaptureRequest(int templateType)方法创建CaptureRequest.Builder,该方法支持TEMPLATE_ATE_PREVIEW(预览)、TEMPLATE_RECORD(拍摄视频)、TEMPLATE_STILL_CAPTURE(拍照)等参数

4.通过第3步所调用方法返回的CaptureRequest.Builder设置拍照的各种参数,比如对焦模式、曝光模式等

5.调用CaptureRequest.Builder的build()方法即可得到CaptureRequest对象,接下来程序可通过CameraCaptureSession的setRepeatingRequest()方法开始预览,或调用capture()方法拍照。

 

 

实例:拍照自动对焦

public class AutoFitTextureView extends TextureView
{
	private int mRatioWidth = 0;
	private int mRatioHeight = 0;

	public AutoFitTextureView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	}

	void setAspectRatio(int width, int height)
	{
		mRatioWidth = width;
		mRatioHeight = height;
		requestLayout();
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	{
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int width = MeasureSpec.getSize(widthMeasureSpec);
		int height = MeasureSpec.getSize(heightMeasureSpec);
		if (0 == mRatioWidth || 0 == mRatioHeight)
		{
			setMeasuredDimension(width, height);
		}
		else
		{
			if (width < height * mRatioWidth / mRatioHeight)
			{
				setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
			}
			else
			{
				setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
			}
		}
	}
}
public class MainActivity extends Activity
{
	private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

	static {
		ORIENTATIONS.append(Surface.ROTATION_0, 90);
		ORIENTATIONS.append(Surface.ROTATION_90, 0);
		ORIENTATIONS.append(Surface.ROTATION_180, 270);
		ORIENTATIONS.append(Surface.ROTATION_270, 180);
	}
	// 定义界面上根布局管理器
	private FrameLayout rootLayout;
	// 定义自定义的AutoFitTextureView组件,用于预览摄像头照片
	private AutoFitTextureView textureView;
	// 摄像头ID(通常0代表后置摄像头,1代表前置摄像头)
	private String mCameraId = "0";
	// 定义代表摄像头的成员变量
	private CameraDevice cameraDevice;
	// 预览尺寸
	private Size previewSize;
	private CaptureRequest.Builder previewRequestBuilder;
	// 定义用于预览照片的捕获请求
	private CaptureRequest previewRequest;
	// 定义CameraCaptureSession成员变量
	private CameraCaptureSession captureSession;
	private ImageReader imageReader;
	private final TextureView.SurfaceTextureListener mSurfaceTextureListener
			= new TextureView.SurfaceTextureListener()
	{
		@Override
		public void onSurfaceTextureAvailable(SurfaceTexture texture
				, int width, int height)
		{
			// 当TextureView可用时,打开摄像头
			openCamera(width, height);
		}

		@Override
		public void onSurfaceTextureSizeChanged(SurfaceTexture texture
				, int width, int height)
		{
			configureTransform(width, height);
		}

		@Override
		public boolean onSurfaceTextureDestroyed(SurfaceTexture texture)
		{
			return true;
		}

		@Override
		public void onSurfaceTextureUpdated(SurfaceTexture texture)
		{
		}
	};
	private final CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback()
	{
		//  摄像头被打开时激发该方法
		@Override
		public void onOpened(@NonNull CameraDevice cameraDevice)
		{
			MainActivity.this.cameraDevice = cameraDevice;
			// 开始预览
			createCameraPreviewSession();  // ②
		}

		// 摄像头断开连接时激发该方法
		@Override
		public void onDisconnected(CameraDevice cameraDevice)
		{
			cameraDevice.close();
			MainActivity.this.cameraDevice = null;
		}

		// 打开摄像头出现错误时激发该方法
		@Override
		public void onError(CameraDevice cameraDevice, int error)
		{
			cameraDevice.close();
			MainActivity.this.cameraDevice = null;
			MainActivity.this.finish();
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		rootLayout = findViewById(R.id.root);
		requestPermissions(new String[]{Manifest.permission.CAMERA}, 0x123);
	}

	@Override
	public void onRequestPermissionsResult(int requestCode,
										   @NonNull String[] permissions, @NonNull int[] grantResults)
	{
		if (requestCode == 0x123 && grantResults.length == 1
				&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
			// 创建预览摄像头图片的TextureView组件
			textureView = new AutoFitTextureView(MainActivity.this, null);
			// 为TextureView组件设置监听器
			textureView.setSurfaceTextureListener(mSurfaceTextureListener);
			rootLayout.addView(textureView);
			findViewById(R.id.capture).setOnClickListener(view -> captureStillPicture());
		}
	}

	private void captureStillPicture()
	{
		try {
			if (cameraDevice == null) {
				return;
			}
			// 创建作为拍照的CaptureRequest.Builder
			CaptureRequest.Builder captureRequestBuilder = cameraDevice
					.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
			// 将imageReader的surface作为CaptureRequest.Builder的目标
			captureRequestBuilder.addTarget(imageReader.getSurface());
			// 设置自动对焦模式
			captureRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
					CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
			// 设置自动曝光模式
			captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
					CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
			// 获取设备方向
			int rotation = getWindowManager().getDefaultDisplay().getRotation();
			// 根据设备方向计算设置照片的方向
			captureRequestBuilder.set(CaptureRequest.JPEG_ORIENTATION,
					ORIENTATIONS.get(rotation));
			// 停止连续取景
			captureSession.stopRepeating();
			// 捕获静态图像
			captureSession.capture(captureRequestBuilder.build(),
					new CameraCaptureSession.CaptureCallback()  // ⑤
					{
						// 拍照完成时激发该方法
						@Override
						public void onCaptureCompleted(@NonNull CameraCaptureSession session,
													   @NonNull CaptureRequest request, @NonNull TotalCaptureResult result)
						{
							try {
								// 重设自动对焦模式
								previewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
										CameraMetadata.CONTROL_AF_TRIGGER_CANCEL);
								// 设置自动曝光模式
								previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
										CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
								// 打开连续取景模式
								captureSession.setRepeatingRequest(previewRequest, null, null);
							} catch (CameraAccessException e) {
								e.printStackTrace();
							}
						}
					}, null);
		} catch (CameraAccessException e) {
			e.printStackTrace();
		}
	}
	// 根据手机的旋转方向确定预览图像的方向
	private void configureTransform(int viewWidth, int viewHeight) {
		if (null == previewSize) {
			return;
		}
		// 获取手机的旋转方向
		int rotation = getWindowManager().getDefaultDisplay().getRotation();
		Matrix matrix = new Matrix();
		RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
		RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
		float centerX = viewRect.centerX();
		float centerY = viewRect.centerY();
		// 处理手机横屏的情况
		if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
			bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
			matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
			float scale = Math.max(
					(float) viewHeight / previewSize.getHeight(),
					(float) viewWidth / previewSize.getWidth());
			matrix.postScale(scale, scale, centerX, centerY);
			matrix.postRotate(90 * (rotation - 2), centerX, centerY);
		}
		// 处理手机倒置的情况
		else if (Surface.ROTATION_180 == rotation)
		{
			matrix.postRotate(180, centerX, centerY);
		}
		textureView.setTransform(matrix);
	}
	// 打开摄像头
	private void openCamera(int width, int height)
	{
		setUpCameraOutputs(width, height);
		configureTransform(width, height);
		CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
		try {
			// 如果用户没有授权使用摄像头,直接返回
			if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
				return;
			}
			// 打开摄像头
			manager.openCamera(mCameraId, stateCallback, null); // ①
		}
		catch (CameraAccessException e)
		{
			e.printStackTrace();
		}
	}
	private void createCameraPreviewSession()
	{
		try
		{
			SurfaceTexture texture = textureView.getSurfaceTexture();
			texture.setDefaultBufferSize(previewSize.getWidth(), previewSize.getHeight());
			Surface surface = new Surface(texture);
			// 创建作为预览的CaptureRequest.Builder
			previewRequestBuilder = cameraDevice
					.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
			// 将textureView的surface作为CaptureRequest.Builder的目标
			previewRequestBuilder.addTarget(new Surface(texture));
			// 创建CameraCaptureSession,该对象负责管理处理预览请求和拍照请求
			cameraDevice.createCaptureSession(Arrays.asList(surface, imageReader.getSurface()),
				new CameraCaptureSession.StateCallback() // ③
				{
					@Override
					public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession)
					{
						// 如果摄像头为null,直接结束方法
						if (null == cameraDevice)
						{
							return;
						}
						// 当摄像头已经准备好时,开始显示预览
						captureSession = cameraCaptureSession;
						// 设置自动对焦模式
						previewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE,
							CaptureRequest.CONTROL_AF_MODE_CONTINUOUS_PICTURE);
						// 设置自动曝光模式
						previewRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE,
							CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
						// 开始显示相机预览
						previewRequest = previewRequestBuilder.build();
						try {
							// 设置预览时连续捕获图像数据
							captureSession.setRepeatingRequest(previewRequest, null, null);  // ④
						}
						catch (CameraAccessException e)
						{
							e.printStackTrace();
						}
					}
					@Override public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession)
					{
						Toast.makeText(MainActivity.this, "配置失败!",
								Toast.LENGTH_SHORT).show();
					}
				}, null);
		}
		catch (CameraAccessException e)
		{
			e.printStackTrace();
		}
	}
	private void setUpCameraOutputs(int width, int height)
	{
		CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
		try {
			// 获取指定摄像头的特性
			CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
			// 获取摄像头支持的配置属性
			StreamConfigurationMap map = characteristics.get(CameraCharacteristics.
					SCALER_STREAM_CONFIGURATION_MAP);
			// 获取摄像头支持的最大尺寸
			Size largest = Collections.max(
					Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
					new CompareSizesByArea());
			// 创建一个ImageReader对象,用于获取摄像头的图像数据
			imageReader = ImageReader.newInstance(largest.getWidth(),
					largest.getHeight(), ImageFormat.JPEG, 2);
			imageReader.setOnImageAvailableListener(reader -> {
					// 当照片数据可用时激发该方法
					// 获取捕获的照片数据
					Image image = reader.acquireNextImage();
					ByteBuffer buffer = image.getPlanes()[0].getBuffer();
					byte[] bytes = new byte[buffer.remaining()];
					// 使用IO流将照片写入指定文件
					File file = new File(getExternalFilesDir(null), "pic.jpg");
					buffer.get(bytes);
					try (
						FileOutputStream output = new FileOutputStream(file))
					{
						output.write(bytes);
						Toast.makeText(MainActivity.this, "保存: "
								+ file, Toast.LENGTH_SHORT).show();
					}
					catch (Exception e)
					{
						e.printStackTrace();
					}
					finally
					{
						image.close();
					}
			},null);
			// 获取最佳的预览尺寸
			previewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
				width, height, largest);
			// 根据选中的预览尺寸来调整预览组件(TextureView的)的长宽比
			int orientation = getResources().getConfiguration().orientation;
			if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
				textureView.setAspectRatio(previewSize.getWidth(), previewSize.getHeight());
			} else {
				textureView.setAspectRatio(previewSize.getHeight(), previewSize.getWidth());
			}
		}
		catch (CameraAccessException e)
		{
			e.printStackTrace();
		}
		catch (NullPointerException e)
		{
			System.out.println("出现错误。");
		}
	}
	private static Size chooseOptimalSize(Size[] choices
			, int width, int height, Size aspectRatio)
	{
		// 收集摄像头支持的打过预览Surface的分辨率
		List bigEnough = new ArrayList<>();
		int w = aspectRatio.getWidth();
		int h = aspectRatio.getHeight();
		for (Size option : choices)
		{
			if (option.getHeight() == option.getWidth() * h / w &&
					option.getWidth() >= width && option.getHeight() >= height)
			{
				bigEnough.add(option);
			}
		}
		// 如果找到多个预览尺寸,获取其中面积最小的。
		if (bigEnough.size() > 0)
		{
			return Collections.min(bigEnough, new CompareSizesByArea());
		}
		else
		{
			System.out.println("找不到合适的预览尺寸!!!");
			return choices[0];
		}
	}
	// 为Size定义一个比较器Comparator
	static class CompareSizesByArea implements Comparator
	{
		@Override
		public int compare(Size lhs, Size rhs)
		{
			// 强转为long保证不会发生溢出
			return Long.signum((long) lhs.getWidth() * lhs.getHeight() -
					(long) rhs.getWidth() * rhs.getHeight());
		}
	}
}


	

	



	
	
	
		
			
				

				
			
		
	

录制视频短片

屏幕捕捉

感到自己知识的匮乏,研究几天camera API,暂时停止更新。

12月找到实习,长期暂停更新。

你可能感兴趣的:(安卓笔记:多媒体应用开发)