Wednesday, 28 October 2015

Android Media Player Example

Android provides many ways to control playback of audio/video files and streams. One of this way is through a class called MediaPlayer
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"
   
xmlns:app="http://schemas.android.com/apk/res-auto"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
app:layout_behavior="@string/appbar_scrolling_view_behavior"
   
tools:showIn="@layout/activity_main" tools:context=".MainActivity"
   
android:orientation="vertical">


    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Play"
       
android:id="@+id/button"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Pause"
       
android:id="@+id/button2"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />

    <
Button
       
android:layout_width="wrap_content"
       
android:layout_height="wrap_content"
       
android:text="Stop"
       
android:id="@+id/button3"
       
android:layout_gravity="center_horizontal"
       
android:layout_marginTop="25dp" />
</
LinearLayout>

MainActivity.Java
------------------------------
package com.android.anil.androidaudio;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button
playbtn,pausebtn,stopbtn;
    MediaPlayer
mediaPlayer;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.
toolbar);
        setSupportActionBar(toolbar);
       
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.a);
       
playbtn=(Button)findViewById(R.id.button);
       
pausebtn=(Button)findViewById(R.id.button2);
       
stopbtn=(Button)findViewById(R.id.button3);
       
playbtn.setOnClickListener(this);
       
pausebtn.setOnClickListener(this);
       
stopbtn.setOnClickListener(this);

    }

   
@Override
   
public void onClick(View v) {
       
switch (v.getId()){
           
case R.id.button:
               
mediaPlayer.start();
               
break;
           
case R.id.button2:
               
mediaPlayer.pause();
               
break;
           
case R.id.button3:
               
mediaPlayer.stop();
               
mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.a);
               
break;
        }
    }

   
@Override
   
protected void onPause() {
       
mediaPlayer.pause();
       
super.onPause();
    }
}

Output :
-------------------------------


1 comment: