multimedia capture & storage. introduction a rich set of api for recording of audio & video....

35
Multimedia Capture & storage

Upload: hugo-richardson

Post on 19-Jan-2016

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

MultimediaCapture & storage

Page 2: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Introduction

• A rich set of API for recording of audio & video.• A developer has two choices launch the built-in app using intent use the in-app mechanism (more flexibility & fine grained control).

Page 3: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Built-in app mechanism

• Recording audio using intent 1 Intent intent= new Intent( MediaStore.Audio.Media.RECORD_SOUND_ACTION); 2 startActivityForResult(intent,REQUEST_CODE);• Recording video using intent 1 Intent intent = new Intent (MediaStore.ACTION_VIDEO_CAPTURE); 2 intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); 3 startActivityForResult(intent,REQUEST_CODE);

Page 4: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

CAPTURE IMAGE USING AN INTENT

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String filepath=Environment.getExternalStorageDirectory()+ “/mypicture.jpg”; File myImage = new File(filepath); Uri imageUri = Uri.fromFile(myImage); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,REQUEST_CODE);

Page 5: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

IN-APP MECHANISM

More control & flexibility.MediaRecorder API is used.Steps1. MediaRecorder object is created & take it to initial state.2. It is assigned with input sources to record from(using

setAudioSource()/setVedioSource()).3. Define the output format(using setOutputFormat()) & take to data

source configured state4. setAudioEncoder()/setVedioEncoder() for media encoding

Page 6: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

5. setVideoFrameRate()/setVideoSize() used to specify the frame rate & output size6.The setOutputFile() used to set the path of the output file.

MediaRecorder object moves to prepared state by calling prepare().The recording by start() then correspondingly use stop() & release().

Page 7: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

The key life-cycle methods & states of the MediaRecorder objects are• setAudioSource()/setVedioSource() Initialized• setOutputFormat() datasource configured• prepare() prepared• start() recording• stop() Initial• release() released

Page 8: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

record.txt

Page 9: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

playback.txt

Page 10: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

The general steps for creating a custom camera interface for your application are as follows:

• Detect and Access Camera - Create code to check for the existence of cameras and request access.• Create a Preview Class - Create a camera preview class that extends

SurfaceView and implements the SurfaceHolder interface. This class previews the live images from the camera.• Build a Preview Layout - Once you have the camera preview class,

create a view layout that incorporates the preview and the user interface controls you want.

Page 11: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• Setup Listeners for Capture - Connect listeners for your interface controls to start image or video capture in response to user actions, such as pressing a button.• Capture and Save Files - Setup the code for capturing pictures or

videos and saving the output.• Release the Camera - After using the camera, your application must

properly release it for use by other applications.

Page 12: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Detecting camera hardware

• If your application does not specifically require a camera using a manifest declaration, you should check to see if a camera is available at runtime. To perform this check, use the PackageManager.hasSystemFeature() method

Page 13: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• Android devices can have multiple cameras, for example a back-facing camera for photography and a front-facing camera for video calls. Android 2.3 (API Level 9) and later allows you to check the number of cameras available on a device using the Camera.getNumberOfCameras() method.

Page 14: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Checking camera features

• Once you obtain access to a camera, you can get further information about its capabilities using the Camera.getParameters() method and checking the returned Camera.Parameters object for supported capabilities. • When using API Level 9 or higher, use the Camera.getCameraInfo() to

determine if a camera is on the front or back of the device, and the orientation of the image.

Page 15: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Creating a preview class

• For users to effectively take pictures or video, they must be able to see what the device camera sees. • A camera preview class is a SurfaceView that can display the live

image data coming from a camera, so users can frame and capture a picture or video.

Page 16: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

The following example code demonstrates how to create a basic camera preview class that can be included in a View layout. This class implements SurfaceHolder.Callback in order to capture the callback events for creating and destroying the view, which are needed for assigning the camera preview input.

Page 17: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using
Page 18: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

On most devices, the default orientation of the camera preview is landscape. For simplicity in rendering a camera preview, you should change your application's preview activity orientation to landscape by adding the following to your manifest.

Page 19: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

The following example shows how to modify a camera activity to attach the preview class shown in Creating a preview class.

Page 20: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Capturing pictures

• In your application code, you must set up listeners for your user interface controls to respond to a user action by taking a picture.• In order to retrieve a picture, use the Camera.takePicture() method.

This method takes three parameters which receive data from the camera. • In order to receive data in a JPEG format, you must implement an

Camera.PictureCallback interface to receive the image data and write it to a file.

Page 21: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

example

Page 22: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Trigger capturing an image by calling the Camera.takePicture() method. The following example code shows how to call this method from a button View.OnClickListener

Page 23: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Capturing videos

• Video capture using the Android framework requires careful management of the Camera object and coordination with the MediaRecorder class.

• When recording video with Camera, you must manage the Camera.lock() and Camera.unlock() calls to allow MediaRecorder access to the camera hardware, in addition to the Camera.open() and Camera.release() calls.

Page 24: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Unlike taking pictures with a device camera, capturing video requires a very particular call order. You must follow a specific order of execution to successfully prepare for and capture video with your application, as detailed below.• Open Camera - Use the Camera.open() to get an instance of the

camera object.

• Connect Preview - Prepare a live camera image preview by connecting a SurfaceView to the camera using Camera.setPreviewDisplay().

• Start Preview - Call Camera.startPreview() to begin displaying the live camera images.

Page 25: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Start Recording Video - The following steps must be completed in order to successfully record video:

• Unlock the Camera - Unlock the camera for use by MediaRecorder by calling Camera.unlock().• Configure MediaRecorder - Call in the following MediaRecorder methods in

this order. • setCamera() - Set the camera to be used for video capture, use your

application's current instance of Camera.• setAudioSource() - Set the audio source, use

MediaRecorder.AudioSource.CAMCORDER.• setVideoSource() - Set the video source, use

MediaRecorder.VideoSource.CAMERA.

Page 26: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• Set the video output format and encoding.

• setOutputFormat() - Set the output format, specify the default setting or MediaRecorder.OutputFormat.MPEG_4.• setAudioEncoder() - Set the sound encoding type, specify the

default setting or MediaRecorder.AudioEncoder.AMR_NB.• setVideoEncoder() - Set the video encoding type, specify the default

setting or MediaRecorder.VideoEncoder.MPEG_4_SP.

Page 27: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• setOutputFile() - Set the output file, use getOutputMediaFile(MEDIA_TYPE_VIDEO).toString() from the example method in the Saving Media Files section.• setPreviewDisplay() - Specify the SurfaceView preview layout element

for your application. Use the same object you specified for Connect Preview.

Page 28: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• Stop Recording Video - Call the following methods in order, to successfully complete a video recording:

• Stop MediaRecorder - Stop recording video by calling MediaRecorder.stop().• Reset MediaRecorder - Optionally, remove the configuration settings from

the recorder by calling MediaRecorder.reset().• Release MediaRecorder - Release the MediaRecorder by calling

MediaRecorder.release().• Lock the Camera - Lock the camera so that future MediaRecorder sessions

can use it by calling Camera.lock().

Page 29: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

• Stop the Preview - When your activity has finished using the camera, stop the preview using Camera.stopPreview().• Release Camera - Release the camera so that other applications can

use it by calling Camera.release().

Page 30: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Releasing the camera

• Cameras are a resource that is shared by applications on a device. • Your application can make use of the camera after getting an instance

of Camera, and you must be particularly careful to release the camera object when your application stops using it, and as soon as your application is paused (Activity.onPause())

Page 31: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

To release an instance of the Camera object, use the Camera.release() method

Page 32: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Saving Media Files

• Media files created by users such as pictures and videos should be saved to a device's external storage directory (SD Card) to conserve system space and to allow users to access these files without their device.• there are only two standard locations you should consider as a

developer:• Environment.getExternalStoragePublicDirectory(Environment.DIRECT

ORY_PICTURES)• Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)

Page 33: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)• This method returns the standard, shared and recommended location

for saving pictures and videos. • This directory is shared (public), so other applications can easily

discover, read, change and delete files saved in this location.• If your application is uninstalled by the user, media files saved to this

location will not be removed. • To avoid interfering with users existing pictures and videos, you

should create a sub-directory for your application's media files within this directory.

Page 34: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

Context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)• This method returns a standard location for saving pictures and videos

which are associated with your application. • If your application is uninstalled, any files saved in this location are

removed.• Security is not enforced for files in this location and other

applications may read, change and delete them.

Page 35: Multimedia Capture & storage. Introduction A rich set of API for recording of audio & video. A developer has two choices  launch the built-in app using

The following example code demonstrates how to create a File or Uri location for a media file that can be used when invoking a device's camera with an Intent or as part of a Building a Camera App.