Chapter Six
1 Multimedia & Graphics In Android
Compiled by Chalew Z.(MSc)
2 Introduction
In todays world of converging technologies the mobile phone is used for a variety of tasks
beyond simple voice calls.
Multimedia capabilities, or the playing and recording of audio and video, is one such
significant task that many users find to be of great value.
Take a quick look around and you will find people using the phone as a means to enjoy a
variety of programs as well as share self-recorded media among friends.
Android provides the APIs to easily access this capability as well as embed multimedia and its
manipulation directly within an application.
Three main aspects in multimedia include Media, Audio, and Video.
Compiled by Chalew Z.(MSc)
3 Cont.…
Android provides a huge set of 2D-drawing APIs that allow you to create graphics.
Android has got visually appealing graphics and mind-blowing animations.
The Android framework provides a rich set of powerful APIS for applying animation to UI
elements and graphics as well as drawing custom 2D and 3D graphics.
Compiled by Chalew Z.(MSc)
4 Multimedia & Graphics In Android
The Android SDK provides a set of APIs to handle multimedia files, such as audio, video and
images.
Moreover, the SDK provides other API sets that help developers to implement interesting
graphics effects, like animations and so on.
The modern smart phones and tablets have an increasing storage capacity so that we can
store music files, video files, images etc.
Not only the storage capacity is important, but also the High Definition Camera makes it
possible to take impressive photos.
In this context, the Multimedia API plays an important role.
Compiled by Chalew Z.(MSc)
5 Multimedia API
Android supports a wide list of audio, video and image formats.
You can give a look here to have an idea; just to name a few formats supported:
Compiled by Chalew Z.(MSc)
6 Cont.…
Android, additionally can handle local files, meaning files that are stored inside the smart
phone or tablet or remote file using data streaming.
All the classes provided by the Android SDK that we can use to add multimedia
capabilities to our apps are under the [Link] package.
In this package, the heart class is called MediaPlayer.
This class has several methods that we can use to play audio and video file stored in our
device or streamed from a remote server.
To stream Internet media using the Media Player, your application must include the
INTERNET permission:
Compiled by Chalew Z.(MSc)
7 Cont.…
There are 4 state of Android multimedia file play either video or music:
1. Idle state: The MediaPlayer object is created, but it's not yet initialized with any data
source.
2. Initialization state: Triggered when you call setDataSource, meaning you're providing a file
path, URI, or another source for the media content.
3. Prepared state: After calling prepare() or prepareAsync(), the MediaPlayer is ready to start
playback. prepare() is synchronous and blocks until the preparation is complete, whereas
prepareAsync() returns immediately and preparation completes in the background,
notifying you via a callback.
4. Completed state: This state indicates that playback has finished. The MediaPlayer is in a
state where it can be re-prepared or released.
Compiled by Chalew Z.(MSc)
8 Cont.…
In addition to these states, it's useful to know about other states like:
Started state: When playback begins, generally triggered by start().
Paused state: When playback is paused via pause().
Stopped state: When playback is stopped via stop().
End state: After calling release(), the MediaPlayer transitions to this state and resources are
freed.
Compiled by Chalew Z.(MSc)
9 Cont.…
Compiled by Chalew Z.(MSc)
10 Android Audio Recorder
In android, MediaRecorder class will provide a functionality to record audio or video files.
The android multimedia framework provides a built in support for capturing and encoding
a variety of common audio and video formats.
We have a multiple ways to record audio or video but by using MediaRecorder class we
can easily implement audio or video recording.
Following is the code snippet of defining the permissions in android manifest file to record
audio and save it in device.
Compiled by Chalew Z.(MSc)
11 Image Components
ImageView class is used to display any kind of image resource in the android application
either it can be [Link] or [Link]
Image file is easy to use but hard to master in Android, because of the various screen sizes
in Android devices.
An android is enriched with some of the best UI design widgets that allows us to build good
looking and attractive UI based application.
Below is an ImageView code in XML:
Compiled by Chalew Z.(MSc)
12 Using Android Camera
If we want to add to our apps the capability to take photos using the integrated smart
phone camera, then the best way is to use an Intent. For example, let us suppose we want
to start the camera as soon as we press a button and show the result in our app.
to Integrate Camera Functionality Add Permissions Ensure your app has the necessary
permissions to use the camera. Add the following lines to your [Link]:
<uses-permission android:name="[Link]"/>
<uses-permission android:name="[Link].WRITE_EXTERNAL_STORAGE"/>
Compiled by Chalew Z.(MSc)
13 Using Android Camera
.
Compiled by Chalew Z.(MSc)
14 Graphics in Android
Android implements complete 2-D functions in one package, named [Link].
This package provides various kinds of graphics tools, such as canvas, color filter, point, line, and
rectangles.
The [Link] class is used with canvas to draw objects. It holds the information of
color and style.
Canvas
To draw 2-dimensional objects an own
class can be derived from the View class
and its onDraw(Canvas canvas) method
can be overwritten
The Canvas class contains various
methods for drawing 2D primitives such
as lines, rectangles, circles, etc
Compiled by Chalew Z.(MSc)
15 Cont.…
The easiest way to use 2D graphics is extending the View class and overriding the onDraw
method. We can use this technique when we do not need a graphics intensive app.
In this case, we can use the Canvas class to create 2D graphics. This class provides a set of
method starting with draw * that can be used to draw different shapes like:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
[Link]([Link]);
[Link](0, 0, 100, 100, paint);
} Compiled by Chalew Z.(MSc)
16
Compiled by Chalew Z.(MSc)