We can play and control the audio files in android by the help of MediaPlayer class. The android.media.MediaPlayer class is used to control the audio or video files.
Methods of MediaPlayer class
There are many methods of the MediaPlayer class. Some of them are as follows:
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 the media player is playing. |
public void seekTo(int millis) | seeks to specify time in milliseconds. |
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 the duration of the file. |
public void setVolume(float leftVolume,float rightVolume) | sets the volume on this player. |
Video
By the help of MediaController and VideoView classes, we can play the video files in android. The android.widget.MediaController is a view that contains media controls like play/pause, previous, next, fast-forward, rewind etc. The android.widget.VideoView class provides methods to play and control the video player. The commonly used methods of VideoView class are as follows:
Method | Description |
public void setMediaController(MediaController controller) | sets the media controller to the 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. |
public void seekTo(int millis) | seeks to specify time in milliseconds. |
Drag the VideoView from the pallete, now the activity_main.xml file will like this:
<VideoView
android:id=”@+id/videoView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_centerVertical=”true” />
Java Code
VideoView videoView =(VideoView)findViewById(R.id.videoView1);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
Uri uri =Uri.parse(Environment.getExternalStorageDirectory().getPath()+”/media/1.mp4″);
videoView.setMediaController(mediaController);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
Animations
Animation is the process of creating motion and shape change. Android provides a large number of classes and interfaces for the animation development. Most of the classes and interfaces are given in the android.animation package. Android Animation enables you to change the object property and behaviour at run time. There are various ways to do animation in android. The AnimationDrawable class provides methods to start and end the animation.
Rotate Animation
Rotate animation is a special kind of animation in Android which controls the Rotation of an object. These types of animations are usually used by developers to give a feel to the user about the changes happening in the application like loading content, processing data, etc. By using the rotate animation effect, an object can be rotated in the X-Y plane of activity and it allows the rotation in both Clockwise and Anticlockwise direction.
XML attributes which define the rotation of an object
android:pivotX
To define the X coordinate of the point about which the object is being rotated
android:pivotY
To define the Y coordinate of the point about which the object is being rotated
android:fromDegrees
Rotation of the object starts from this geometrical degree
android:toDegrees
Rotation of the object ends at this geometrical degree
Android:duration
Used to define the duration of the animation in millisecond
android:startOffset
Used to delay the animation time in millisecond
FadeIn / FadeOut Animation
In Android Animations are the visuals that are added to make the user interface more interactive, clear and good looking. Fade In and Fade out animations are used to modify the appearance of any view over a set interval of time so that users can be notified about the changes that are occurring in our application.
XML Attributes | Description |
android:duration | It is used to specify the duration of animation |
android:fromAlpha | It is the starting alpha value for the animation, where 1.0 means fully opaque and 0.0 means fully transparent |
android:toAlpha | It is the ending alpha value |
android:id | Sets unique id of the view |
Add anim folder
In this folder, we will be adding the XML files which will be used to produce the animations. For this to happen, go to app/res right click and then select Android Resource Directory and name it as anim.
Again right click this anim folder and select the Animation resource file and name it as fade_in. Similarly, also create fade_out.xml and paste the following code.
fade_in.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
android:interpolator=”@android:anim/linear_interpolator”>
<alpha
android:duration=”2000″
android:fromAlpha=”0.1″
android:toAlpha=”1.0″ />
</set>
fade_out.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”>
android:interpolator=”@android:anim/linear_interpolator”>
<alpha
android:duration=”2000″
android:fromAlpha=”1.0″
android:toAlpha=”0.1″ />
</set>
Zoom Animation
Zoom In and Zoom Out animations are used to enlarge and reduce the size of a view in Android applications respectively. These types of animations are often used by developers to provide a dynamic nature to the applications. Users also feel the changes happening in the application by watching these kinds of animations.
XML Attributes of Scale Tag
The characteristics of Zoom In and Zoom Out animations are defined in the XML files by using scale tag.
XML attribute | Description |
android:duration | Used to define the duration of the animation in millisecond |
android:fromXScale | Used to set initial size of the view in X-axis |
android:fromYScale | Used to set initial size of the view in Y-axis |
android:pivotX | To define the X coordinate of the point about which the object is being zoom in/out |
android:pivotY | To define the Y coordinate of the point about which the object is being zoom in/out |
android:toXScale | Used to set final size of the view in X-axis |
android:toYScale | Used to set final size of the view in Y-axis |
Define XML file for Zoom In and Zoom Out animation of the image
Create a new directory in the res folder of the application through right-click on res => New => Android Resource Directory. Select Resource Type as anim and Directory name should also be anim.
In this directory create 2 Animation Resource Files namely zoom_in and zoom_out. These 2 files are the XML file which holds the details of the animation.
Below is the code for both the files.
Filename: zoom_in.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android“
android:fillAfter = “true”>
<scale
xmlns:android = “http://schemas.android.com/apk/res/android”
android:duration = “1000”
android:fromXScal = “1”
android:fromYScale = “1”
android:pivotX = “50%”
android:pivotY = “50%”
android:toXScale = “2”
android:toYScale = “2”/>
</set>
Filename: zoom_out.xml
<?xml version=”1.0″ encoding=”utf-8″?>
<set xmlns:android=”http://schemas.android.com/apk/res/android”
android:fillAfter = “true”>
<scale
xmlns:android=”http://schemas.android.com/apk/res/android”
android:duration = “2500”
android:fromXScale = “1”
android:fromYScale = “1”
android:pivotX = “50%”
android:pivotY = “50%”
android:toXScale = “.2”
android:toYScale = “.2” />
</set>