You are on page 1of 18

Chapter Four

Multimedia in Android

Tagele B.
1
Multimedia Techniques
The Android platform provides comprehensive
multimedia functionality.
techniques to manipulate
images,
record and play back audio,
and record and play back video.
An application that records any type of media
requires setting the appropriate permission
in the AndroidManifest.xml file (one or both of
the following):
<uses-permission
android:name="android.permission.RECORD_AUDI
O"/>
<uses-permission
android:name="android.permission.RECORD_VIDE 2
Images
Images local to an application are usually put in
the res/drawable/ directory, They can be
accessed with the appropriate resource identifier,
such as R.drawable.my_picture.
Images on the Android device filesystem can be
accessed using the
normal Java classes, such as an InputStream.
However, the preferred method in Android to read

an image into memory for manipulation is to use


the built-in class BitmapFactory.
BitmapFactory creates bitmap objects from files,

streams, or byte arrays. A resource or a file can be


loaded like this:
Bitmap myBitmap1 =
BitmapFactory.decodeResource(getResources(),
R.drawable.my_picture); 3
Media Player Class
Android provides many ways to control playback of
audio/video files and streams. One of this way is
through a class called MediaPlayer.
Android is providing MediaPlayer class to access
built-in media player services like playing
audio, video etc.
In order to use MediaPlayer, we have to call a
static Method create() of this class. This method
returns an instance of MediaPlayer class. Its syntax
is as follows
MediaPlayer mediaPlayer =
MediaPlayer.create (this, R.raw.song);
The second parameter is the name of the song
that you want to play. You have to make a new
folder under your project with name raw and place 4
Methods to start or stop the
music
mediaPlayer.start();
mediaPlayer.pause();
On call to start() method, the music will start
playing from the begininning.
If this method is called again after the pause()
method , the music would start playing from where
it is left and not from the beginning.
In order to start music from the beginning, you
have to call reset() method. Its syntax is given
below.
mediaPlayer.reset();

5
Other methods of MediaPlayer
class
isPlaying()
This method just returns true/false indicating the song is
playing or not
seekTo(positon)
This method takes an integer, and move song to that
particular second
getCurrentDuration()
This method returns the current position of song in
milliseconds
getDuration()
This method returns the total time duration of song in
milliseconds
release()
This method releases any resource attached with
MediaPlayer object
setVolume(float leftVolume, float 6
Contd..
selectTrack(int index)
This method takes an integer, and select the track from
the list on that particular index
getTrackInfo()
This method returns an array of track information

Example
Here is an example demonstrating the use of
MediaPlayer class. It creates a basic media player
that allows you to
forward,
backward,
play and
pause a song.
Note: for this application to work you need 7
8
Media_player.xml

9
10
11
12
13
Animation in Android (Tween
Animation)
Tween Animation takes some parameters such as
start value, end value, size, time duration,
rotation angle etc. and perform the required
animation on that object.
It can be applied to any type of object. So in order
to use this, android has provided us a class called
Animation.
In order to perform animation in android, we are
going to call a static function loadAnimation() of
the class AnimationUtils. We are going to receive
the result in an instance of Animation Object. Its
syntax is as follows:
Animation animation =
AnimationUtils.loadAnimation
(getApplicationContext(), 14
Functions(methods) of
animation class
start()
This method starts the animation.

setDuration(long duration)
This method sets the duration of an animation.

getDuration()
This method gets the duration which is set by

above method.
end()
This method ends the animation.

cancel()
This method cancels the animation.

In order to apply this animation to an object, we


will just call the startAnimation() method of the
object. Its syntax is: 15
Zoom in animation
In order to perform a zoom in animation, create an
XML file under anim folder under res directory and
put this code in the file.

fromXScale and fromYScale defines the start


point
toXScale and toYScale defines the end point. 16
Rotate Clockwise animation
In order to perform a rotate clockwise animation,
create an XML file under anim folder under res
directory and put this code in the file.

The parameter fromDegrees defines the initial


degree of rotation
toDegrees parameter defines the final degree of
rotation.
17
Fade in Animation (Alpha
animation)
In order to perform fade in animation create an
XML file under anim folder under res directory
and put this code in the file.

The parameter fromAlpha defines the initial


visibility level of the component to be animated
while
toAlpha parameter defines the final visibility
level of the component to be animated (where 0
is invisible state and 1 is fully visible state).
18

You might also like