0% found this document useful (0 votes)
22 views5 pages

Media Player

The document discusses using the MediaPlayer class in Android to play audio files. It explains that MediaPlayer allows controlling audio playback and requires creating a MediaPlayer instance using MediaPlayer.create(). This returns a MediaPlayer object that can then call methods like start(), pause(), and stop() to control playback of the audio file stored in the raw folder. The document also provides an example Android app code that implements these MediaPlayer features to play, pause and stop an mp3 file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views5 pages

Media Player

The document discusses using the MediaPlayer class in Android to play audio files. It explains that MediaPlayer allows controlling audio playback and requires creating a MediaPlayer instance using MediaPlayer.create(). This returns a MediaPlayer object that can then call methods like start(), pause(), and stop() to control playback of the audio file stored in the raw folder. The document also provides an example Android app code that implements these MediaPlayer features to play, pause and stop an mp3 file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

LAB 7

Android-Media Player
Android provides many ways to control the playback of audio/video files and streams. One of
these ways is through a class called MediaPlayer.

Android provides 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 the 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 must make a new
folder under your project with the name raw and place the music file into it.

Once you have created the Mediaplayer object you can call some methods to start or stop the
music. These methods are listed below.

mediaPlayer.start();
mediaPlayer.pause();
mediaPlayer.stop();

On the call to start() method, the music will start playing from the beginning. If this method is
called again after the pause() method, the music will start playing from where it is left and not
from the beginning.

Example
Here is an example demonstrating the use of the MediaPlayer class. It creates a basic media
player that allows you to play, pause, and stop a song.

To experiment with this example, you need to run this on an actual device to hear the audio
sound.

Steps Description

You will use Android Studio IDE to create an Android application under a package
1
package com.example.mediaplayer;

2 Modify the src/MainActivity.java file to add MediaPlayer code.

3 Modify the res/layout/activity_main to add respective XML components


Create a new folder under MediaPlayer with the name raw and place an mp3 music file in
4
it with the name sound.mp3

Run the application choose a running Android device and install the application on it and
5
verify the results

Following is the content of the modified main activity file src/MainActivity.java.

package com.example.mediaplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button start,pause,stop;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

start=(Button)findViewById(R.id.playButton);
pause=(Button)findViewById(R.id.pauseButton);
stop=(Button)findViewById(R.id.stopButton);
//creating media player
mp = MediaPlayer.create(this, R.raw.sound);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
});
pause.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
}
});
}
}

Following is the modified content of the xml res/layout/activity_main.xml.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/headingText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="32dp"
android:text="MEDIA PLAYER"
android:textSize="25dp"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="40dp"
android:layout_gravity="center"
android:background="@drawable/img" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/headingText"
android:layout_marginTop="40dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="STOP"
android:textColor="@android:color/white" />
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="PLAY"
android:onClick="Play"
android:textColor="@android:color/white" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PAUSE"
android:textColor="@android:color/white" />
</LinearLayout>
</LinearLayout>

You might also like