Unit 3
Unit 3
Camera
Audio: Android provides APIs for playing various audio
formats like MP3, WAV, etc. You can use the MediaPlayer
class to control playback, volume, and other
functionalities.
Playing Audio:
MediaPlayer Class:
o Usage: Used to control playback of audio and video
files.
o Common Methods:
o create(context, resourceID): Initializes a MediaPlayer
object with a resource.
o setDataSource(String path): Sets the data source to
a file path.
o prepare(): Prepares the MediaPlayer for playback.
o start(): Starts or resumes playback.
o pause(): Pauses playback.
o stop(): Stops playback.
o release(): Releases the resources associated with the
MediaPlayer.
AudioManager Class:
Common Methods:
MediaController Class:
Usage: Provides controls for the VideoView.
Common Methods:
o setMediaPlayer(MediaPlayerControl player):
Attaches the MediaPlayer to the
MediaController.
o setAnchorView(View view): Sets the view that
acts as the anchor for the controller.
Camera2 API:
o Usage: Provides a more flexible API for controlling
the camera.
o Key Components:
o CameraManager: Manages camera devices.
o CameraDevice: Represents a single camera device.
o CameraCaptureSession: Handles the capture of
images from the camera.
o CaptureRequest: Contains the settings and
parameters for a single capture.
Common Operations:
Opening the Camera
CameraManager manager = (CameraManager)
getSystemService(Context.CAMERA_SERVICE);
manager.openCamera(cameraId, stateCallback,
null);
Capturing an Image:
CaptureRequest.Builder builder =
cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_C
APTURE);builder.addTarget(imageReader.getSurface());
Common Methods:
o drawLine(float startX, float startY, float stopX,
float stopY, Paint paint): Draws a line.
o drawRect(float left, float top, float right, float
bottom, Paint paint): Draws a rectangle.
o drawCircle(float cx, float cy, float radius, Paint
paint): Draws a circle.
Paint Class:
o Usage: Holds the style and color information
about how to draw geometries, text, and
bitmaps.
o Common Methods:
o setColor(int color): Sets the color.
o setStrokeWidth(float width): Sets the width for
stroking.
Property Animation:
o ObjectAnimator: Animates a property of an object.
Example
ObjectAnimator animator =
ObjectAnimator.ofFloat(view, "translationX", 0f,
100f); animator.setDuration(1000);
animator.start();
View Animation:
o TranslateAnimation: Moves a view from one
position to another.
o RotateAnimation: Rotates a view.
o ScaleAnimation: Scales a view.
o AlphaAnimation: Changes the alpha level of a
view.
Types of Sensors:
Motion Sensors: Accelerometer, Gyroscope.
Environmental Sensors: Thermometer, Barometer.
Position Sensors: Magnetometer, Proximity sensor.
Sensor Framework:
o SensorManager:
o Usage: Manages all sensors on the device.
Common Methods:
o getDefaultSensor(int type): Returns the default
sensor for a given type.
o registerListener(SensorEventListener listener,
Sensor sensor, int samplingPeriodUs): Registers a
listener.
SensorEventListener:
Methods:
Sensor Class:
o Usage: Represents a specific sensor.
o Properties:
o getName(): Returns the sensor's name.
o getType(): Returns the sensor's type.
Bluetooth: Bluetooth Adapter,
Paired Devices, Enabling/Disabling
Bluetooth Adapter: The BluetoothAdapter class represents
the Bluetooth radio on the device. It allows you to check
its availability, enable/disable Bluetooth, and manage
paired devices.
BluetoothAdapter:
Usage: Represents the device's Bluetooth adapter.
Common Methods:
o getDefaultAdapter(): Gets the default Bluetooth adapter.
o enable(): Enables Bluetooth.
o disable(): Disables Bluetooth.
o isEnabled(): Checks if Bluetooth is enabled.
o getBondedDevices(): Returns a set of paired (bonded)
devices.
Paired Devices:
Bonding Process: Exchange of security
codes.
Methods:
o getBondedDevices(): Gets a set of paired
devices.
Enabling/Disabling Bluetooth: You can use the
BluetoothAdapter methods to enable or disable Bluetooth
functionality on the device.
Enabling Bluetooth:
BluetoothAdapter bluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();
if (!bluetoothAdapter.isEnabled()) {
bluetoothAdapter.enable();
Disabling Bluetooth:
if (bluetoothAdapter.isEnabled()) {
bluetoothAdapter.disable();
Adding a Map:
XML Layout
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragm
ent"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Listeners:
setOnMapClickListener(GoogleMap.OnMapClickListener listener): Handles
map click events.
setOnMarkerClickListener(GoogleMap.OnMarkerClickListener listener):
Handles marker click events.