0% found this document useful (0 votes)
20 views6 pages

Android MediaPlayer Usage Example

Uploaded by

Kami
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Android MediaPlayer Usage Example

Uploaded by

Kami
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Android Media

A software program or hardware device capable of playing a media file or disc. For example,
many media players today are capable of playing audio files such as playing an MP3 song file
and video files such as a short video clip or movie. Media players are only designed to play
multimedia content like video and songs. If you want to edit videos, you'll want to use a video
editing software.

We can play and control the audio files in android by the help of MediaPlayer class. We will
see the example to control the audio playback like start, stop, pause etc.

MediaPlayer class

The [Link] class is used to control the audio or video files.

Methods of MediaPlayer class

There are many methods of 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 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)

Activity class

Let's write the code of to play the audio file. Here, we are going to play maine.mp3 file located
inside the sdcard/Music directory.

File: [Link]
1. package [Link].audiomediaplayer1;
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10.
11. public class MainActivity extends Activity {
12.
13. @Override
14. protected void onCreate(Bundle savedInstanceState) {
15. [Link](savedInstanceState);
16. setContentView([Link].activity_main);
17.
18. MediaPlayer mp=new MediaPlayer();
19. try{
20. [Link]("/sdcard/Music/maine.mp3");//Write your location here
21. [Link]();
22. [Link]();
23.
24. }catch(Exception e){[Link]();}
25.
26. }
27. }

You need to run it on the real device to test the application.

Android MediaPlayer Example of controlling the audio

Let's see a simple example to start, stop and pause the audio play.

activity_main.xml

Drag three buttons from pallete to start, stop and pause the audio play. Now the xml file will look
like this:

File: [Link]
1. <RelativeLayout xmlns:androclass="[Link]
2. xmlns:tools="[Link]
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:paddingBottom="@dimen/activity_vertical_margin"
6. android:paddingLeft="@dimen/activity_horizontal_margin"
7. android:paddingRight="@dimen/activity_horizontal_margin"
8. android:paddingTop="@dimen/activity_vertical_margin"
9. tools:context=".MainActivity" >
10.
11. <TextView
12. android:id="@+id/textView1"
13. android:layout_width="wrap_content"
14. android:layout_height="wrap_content"
15. android:layout_alignParentTop="true"
16. android:layout_marginTop="30dp"
17. android:text="Audio Controller" />
18.
19. <Button
20. android:id="@+id/button1"
21. style="?android:attr/buttonStyleSmall"
22. android:layout_width="wrap_content"
23. android:layout_height="wrap_content"
24. android:layout_alignLeft="@+id/textView1"
25. android:layout_below="@+id/textView1"
26. android:layout_marginTop="48dp"
27. android:text="start" />
28.
29. <Button
30. android:id="@+id/button2"
31. style="?android:attr/buttonStyleSmall"
32. android:layout_width="wrap_content"
33. android:layout_height="wrap_content"
34. android:layout_alignTop="@+id/button1"
35. android:layout_toRightOf="@+id/button1"
36. android:text="pause" />
37.
38. <Button
39. android:id="@+id/button3"
40. style="?android:attr/buttonStyleSmall"
41. android:layout_width="wrap_content"
42. android:layout_height="wrap_content"
43. android:layout_alignTop="@+id/button2"
44. android:layout_toRightOf="@+id/button2"
45. android:text="stop" />
46.
47. </RelativeLayout>

Activity class

Let's write the code to start, pause and stop the audio player.

File: [Link]
1. package [Link];
2.
3. import [Link];
4. import [Link];
5. import [Link];
6. import [Link];
7. import [Link];
8. import [Link];
9. import [Link];
10. import [Link];
11.
12. public class MainActivity extends Activity {
13. Button start,pause,stop;
14. @Override
15. protected void onCreate(Bundle savedInstanceState) {
16. [Link](savedInstanceState);
17. setContentView([Link].activity_main);
18.
19. start=(Button)findViewById([Link].button1);
20. pause=(Button)findViewById([Link].button2);
21. stop=(Button)findViewById([Link].button3);
22. //creating media player
23. final MediaPlayer mp=new MediaPlayer();
24. try{
25. //you can change the path, here path is external directory(e.g. sdcard) /Music/[Link]
3
26. [Link]([Link]().getPath()+"/Music/maine.m
p3");
27.
28. [Link]();
29. }catch(Exception e){[Link]();}
30.
31. [Link](new OnClickListener() {
32. @Override
33. public void onClick(View v) {
34. [Link]();
35. }
36. });
37. [Link](new OnClickListener() {
38. @Override
39. public void onClick(View v) {
40. [Link]();
41. }
42. });
43. [Link](new OnClickListener() {
44. @Override
45. public void onClick(View v) {
46. [Link]();
47. }
48. });
49. }
50. }

Output:

You might also like