What is Media-Player

The media player is used to play the music in android.

In my prgram when user click on start button the music will play when click on pause the music will pause when click on stop the music will stop.


Media-Player:.

In this section i am going to show the media player with practical example.

First open the android studio and create a project and give the name of your project like MediaPlayerDemo.

The screenshots of the program is as follows:

media image media image

Drag and drop the one TextView and three Button from pallet. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="139dp"/>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="50dp"
    android:layout_marginBottom="170dp"
    android:onClick="play"
    android:text="play" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:layout_alignTop="@+id/button"
    android:layout_marginLeft="150dp"

    android:onClick="stop"
    android:text="stop" />
<Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignTop="@+id/button"
        android:layout_marginLeft="250dp"

        android:onClick="pause"
        android:text="pause" />




</RelativeLayout>
       

Open the MainActivity.java file and write code look like this.


package com.example.niitpc.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.renderscript.ScriptIntrinsicYuvToRGB;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    TextView textView;
    MediaPlayer mediaPlayer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView=(TextView)findViewById(R.id.textView);
        mediaPlayer=MediaPlayer.create(this,R.raw.abc);
        mediaPlayer.setLooping(false);


    }
    public  void play(View v)
    {
         mediaPlayer.start();
         textView.setText("Audio play");

    }

    public  void stop(View v)
    {
        mediaPlayer.stop();
        textView.setText("Audio stop");

    }
    public  void pause(View v)
    {
        mediaPlayer.pause();
        textView.setText("Audio pause");
    }

}



Explanation:

TextView textView;:This statement declare the object textView of TextView.

MediaPlayer mediaPlayer:This statement declare the object mediaPlayer of MediaPlayer.

textView=(TextView)findViewById(R.id.textView):This statement initialized the textView object.

mediaPlayer=MediaPlayer.create(this,R.raw.abc):The create() of MediaPlayer take two argument first is reference and second the audio path like R.raw.abc.For this you simple need to right click on res folder and a dialog box will display click on new->android resource directory again a dialog box will display then you select the Resource type as raw and then click on finish.

mediaPlayer.setLooping(false):This statement initialized the textView object.

public void play(View v):Here i have created the method play for playing the music.

mediaPlayer.start():
Here we call the start() of MediaPlayer to play the song.

textView.setText("Audio play"):This statement set the text audo start when play button click

public void stop(View v):Here we created the stop() to stop the music.This is user defined method.

mediaPlayer.stop():The stop() of MediaPlayer stop the music.It is in built method.

textView.setText("Audio stop"):This statement set the text of textView as Audio stop.

public void pause(View v):Here i have created the pause() to pause the music.This is user defined.

mediaPlayer.pause():The pause() of MediaPlayer pause the musice.This is in built method of MediaPlayer.

textView.setText("Audio pause"):This statement set the text of textView as Audio pause.

You have need to set the start() on android:onClick property of start button:

You have need to set the stop() on android:onClick property of stop button:

You have need to set the pasue() on android:onClick property of pause button:


Media-Player

what is media player for video click here

back image                     back image