Tuesday, 11 March 2014

Implementation of AudioMediaPlayer Functionality in Android Applications

This blog is relevant to implementation of AudioMediaPlayer functionality in android applications. To get this functionality need raw folder in Resource (res) pan pest audio file in this folder.


main.xml


<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">

    <Button
        android:id="@+id/btnStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_marginTop="20dp"
        android:layout_gravity="center"
        android:textStyle="bold"/>
  
    <Button
        android:id="@+id/btnStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        android:layout_gravity="center"
        android:layout_marginTop="40dp"
        android:textStyle="bold"/>
</LinearLayout>


AudioMediaPlayerActivity.java

package com.rakesh.tiwari.audiomediaplayer;

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

public class AudioMediaPlayerActivity extends Activity  {
 Button btnStart,btnStop;
 MediaPlayer mPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart=(Button)findViewById(R.id.btnStart);
btnStop=(Button)findViewById(R.id.btnStop);

//     Start functionality in media player..................................................

btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer.isPlaying()==false){
mPlayer.start();
}
play();
}
});

//     Stop functionality in media player..................................................
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mPlayer.isPlaying()==true){
mPlayer.stop();
}
play();
}
});

//  Connect MediaPlayer function with resource of media file............................................................

mPlayer=MediaPlayer.create(getApplicationContext(), R.raw.yaaron);
}
public void play(){
boolean play=mPlayer.isPlaying();
if(play==true){
Toast.makeText(getApplicationContext(), "Song is playing", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getApplicationContext(), "Song is not playing", Toast.LENGTH_LONG).show();
}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.audio_media_player, menu);
return true;
}

}


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rakesh.tiwari.audiomediaplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:screenOrientation="portrait"
            android:name="com.rakesh.tiwari.audiomediaplayer.AudioMediaPlayerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



AudioMediaPlayer Functionality


Start AudioMediaPlayer Functionality

Stop AudioMediaPlayer Functionality