Multimedia
Audio/video/animation
Audio/Video
• The Android multimedia framework includes support for playing variety of
common media types, so that we can easily integrate audio, video and images
into your applications.
• we can play audio or video from media files stored in our application's
resources (raw resources), from standalone files in the filesystem, or from a
data stream arriving over a network connection
Audio/Video Classes
• MediaPlayer
This class is the primary API for playing sound and video.
• AudioManager
This class manages audio sources and audio output on a device.
Manifest Permissions
Wake Lock Permission
If your player application needs to keep the screen from dimming or the processor
from sleeping, or uses the [Link]() or
[Link]() methods, you must request this permission.
<uses-permission android:name="[Link].WAKE_LOCK" />
Internet Permission
If you are using MediaPlayer to stream network-based content, your
application must request network access.
<uses-permission android:name="[Link]" />
Audio
Using MediaPlayer
The MediaPlayer class can fetch, decode, and play both audio and video with
minimal setup.
It supports several different media sources such as:
Local resources
Internal URIs, such as one you might obtain from a Content Resolver
External URLs (streaming)
MediaPlayer Classes
Method Description
public void setDataSource(String path) sets the data source (file path or http url) to use.
public void prepare() prepares the player for playback synchronously.
public void start() it starts or resumes the playback.
public void stop() it stops the playback.
public void pause() it pauses the playback.
public boolean isPlaying() checks if media player is playing.
public void seekTo(int millis) seeks to specified time in miliseconds.
public void setLooping(boolean looping) sets the player for looping or non-looping.
public boolean isLooping() checks if the player is looping or non-looping.
public void selectTrack(int index) it selects a track for the specified index.
public int getCurrentPosition() returns the current playback position.
public int getDuration() returns duration of the file.
public void setVolume(float leftVolume,float
sets the volume on this player.
rightVolume)
Play audio
MediaPlayer myplayer=new MediaPlayer();
[Link]("/sdcard/Music/arrahman.mp3");
[Link]();
[Link]();
Try It!!!
Try to add buttons to control the
Media Player
Video
Important classes
The [Link] is a view that contains media
controls like play/pause, previous, next, fast-forward, rewind etc.
Important classes
The [Link] class provides methods to play and control
the video player.
Important Methods of VideoView Class
Method Description
public void sets the media controller to the
setMediaController(MediaController) video view.
public void setVideoURI (Uri uri) sets the URI of the video file.
public void start() starts the video view.
public void stopPlayback() stops the playback.
public void pause() pauses the playback.
public void suspend() suspends the playback.
public void resume() resumes the playback.
seeks to specified time in
public void seekTo(int millis)
miliseconds.
Play a Video
Step 1 :
Add videoview to the activity xml.
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
Play a Video
Step 2:
Identify the VideoView in ActivityMain
VideoView videoView =(VideoView)findViewById([Link].videoView1);
Step 3:
Create the MediaController Object
MediaController mediaController= new MediaController(this);
[Link](videoView);
Play a Video
Step 5:
//specify the location of media file
Uri uri=[Link]([Link]().getPath()+"/media/1.mp4");
Step 6:
//Setting MediaController and URI, then starting the videoView
[Link](mediaController);
[Link](uri);
[Link]();
Drawing
Important classes
• The [Link] can be used to draw graphics in
android.
• It provides methods to draw shapes like rectangle, picture, text, line
etc.
• The [Link] class is used with canvas to hold the
information of color and style.
• onDraw(Canvas) method enables custom Drawing option in screen
Important Methods of Paint Class
Method Description
setStyle([Link] style) Set the paint's style
setColor(int color) Set the paint's color.
smooths out the edges
setAntiAlias(boolean aa)
of what is being drawn
Important Methods
Method Description
drawCircle(float cx, float cy, float radius, Paint paint) Circle with x,y,radius
drawLine(float startX, float startY, float stopX, float
Line with x & y axis values
stopY, Paint paint)
drawOval(float left, float top, float right, float bottom, Draw the specified oval using the specified
Paint paint) paint.
drawRect(float left, float top, float right, float bottom, Draw the specified Rect using the
Paint paint) specified paint.
rotate(float degrees) Provide rotation of the current view
Draw the text, with origin at (x,y), using the
drawText(String text, float x, float y, Paint paint)
specified paint.
is used to remove all modifications to the
restore()
matrix/clip state since the last save call.