• TekArt

    TekArt is an Organisation where people develop Android App through innovative ideas. App for the next Generation....

Thursday 3 April 2014

Posted by Unknown
1 comment | 21:46
Hello Guys, how are you today? Its super Friday. Hope having a good day.

Today we are going to see how to use MediaPlayer for for performing basic operations like play, pause, stop in Android. So lets get started.



1) First of all, create an Android Application Project.

2) Then go to "res/layout/activity_main.xml" and create 3 buttons there play, pause and stop. So the code looks like.

<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:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity"

    android:orientation="vertical"

     >



    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Play"

        android:id="@+id/play"

        android:layout_gravity="center" />

    

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Pause"

        android:id="@+id/pause"

        android:layout_gravity="center" />

    

    <Button

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:text="Stop"

        android:id="@+id/stop"

        android:layout_gravity="center" />



</LinearLayout>

3) Now we are going to implement OnClickListener for all these buttons and do some operations on clicking those buttons. Before that we create a folder "raw" in "res/" . In raw folder we store a music file "music.mp3" .

4) Now we are going to create a MediaPlayer object mp and going to use for performing our tasks.

    MediaPlayer mp;

5) In the onClick event of play button we are going to play the music. So, our code will be

     mp = MediaPlayer.create(this, R.raw.music);
     mp.start();

6) In the onClick event of pause button we are going to pause the music. So, our code will be

    mp.pause();

7)  In the onClick event of stopbutton we are going to stop the music. So, our code will be

     mp.stop();

The final code looks like

import android.app.Activity;

import android.media.MediaPlayer;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;



public class MainActivity extends Activity implements OnClickListener {

    MediaPlayer mp;

    Button play, pause,stop;



    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

        play = (Button) findViewById(R.id.play);

        pause = (Button) findViewById(R.id.pause);

        stop  =(Button) findViewById(R.id.stop);

        play.setOnClickListener(this);

        pause.setOnClickListener(this);

        stop.setOnClickListener(this);

    }



    @Override

    public void onClick(View v) {

        // TODO Auto-generated method stub

        switch(v.getId()) {
  case R.id.play : if(mp == null) {
   mp = MediaPlayer.create(this, R.raw.music);
   
  }
  mp.start();
  break;
  case R.id.pause : mp.pause();break;
  
  case R.id.stop : mp.stop();mp = null;break;
  }

       

    }



}


Now we are done. Post your questions in the comment. I will be happy to answer those.

For more info visit on facebook https://www.facebook.com/androidcoolstuffs

Thank you


1 comment:

  1. Great tutorial! It really helped a lot. But, a quick question. How would you make the buttons work for multiple audio files.

    ReplyDelete