Wednesday, 9 November 2011

PLAY/PAUSE BUTTON IN MUSIC PLAYER

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MplayActivity extends Activity {
    Button b,
// BUTTON B is used for play/pause ...
    MediaPlayer r;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        b =(Button)findViewById(R.id.button1);
       
        r=MediaPlayer.create(this,R.raw.a);
// this is a path of my mp3 file.. just create a raw named folder in resuorce folder and placed or copy a mp3 file in it.
       
        b.setOnClickListener(new View.OnClickListener()
         {
           
           
           
            @Override
            public void onClick(View v) {
               
                    if(r.isPlaying())
                    {
                        r.pause();
                        b.setText("pause");
                    }
                    else
                    {
                        r.start();
                       b.setText("play");
                    }
                   
                   
                // TODO Auto-generated method stub
               
            }
        });
       
    }


XML FILE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


  

</LinearLayout>

1 comment: