Om Chant – Android Application

In this post, I am going to discuss about how to create ‘Om Chant’ application. Let me give you an overview of ‘Om Chant’ application:

Om Chant – Android Application

  1. The Om Chant application chants the universal mantra – ‘Om‘ continuously. Om is one of the most powerful mantra which brings inner peace to whoever listens to it.
  2. Application chants Om continuously once you press play button.
  3. One can stop playing it by pressing pause button.

[pgsubscribe]

Quick Links

Project Structure

Create layout XML

  • Create new android project [File >> New >> Android Project] with project name OmChant
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.prgguru.android’
  • Click finish

Code Listings

Open main.xml, now you can view the layout as either XML or in graphical view and just replace the XML with below one:

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

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/omblue"
        android:contentDescription="@string/ImageName" />

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="10dip"
        android:background="@drawable/img_btn_play"/>

</RelativeLayout>

Make sure you placed below media files in the respective folders.[Right click and save]

Om.mp3 – Under res/raw
– Under res/drawable-mdpi
– Under res/drawable-mdpi
MainActivity.java
Here is MainActivity code. Each line has comment to make it self explanatory:

package com.prgguru.example;

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;
import android.widget.Toast;

public class MusicAndroidActivity extends Activity {
	//MediaPlayer object
	static MediaPlayer mPlayer;
	//Play button object
	Button buttonPlayStop;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//Find the play button view using its ID
		buttonPlayStop = (Button) findViewById(R.id.play);
		//Register a method to be invoked when play button is clicked
		buttonPlayStop.setOnClickListener(new OnClickListener() {
			//When play button is clicked
			public void onClick(View v) {
				// TODO Auto-generated method stub
				try {
					//When Om chant is not playing
					if (mPlayer==null || !mPlayer.isPlaying()) {
						//Set button background image as 'Pause'
						buttonPlayStop.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.img_btn_pause));
						//Create MediaPlayer object with raw resource 'om.mp3'
						mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.om);
						//Start playing mp3
						mPlayer.start();
						//Set player to be looping to continuously play om.mp3
						mPlayer.setLooping(true);
					} else {
						//When Om chant is playing
						if (mPlayer != null && mPlayer.isPlaying()) {
							//Set button background image as 'Play'
							buttonPlayStop.setBackgroundDrawable(getApplicationContext().getResources().getDrawable(R.drawable.img_btn_play));
							//Stop playing om.mp3
							mPlayer.stop();
						}
					}
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Toast.makeText(getApplicationContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
				} 
			}

		});
	}
	//When Activity is destroyed
	protected void onDestroy() {
		super.onDestroy();
		// TODO Auto-generated method stub
		if (mPlayer != null) {
			//Release media player
			mPlayer.release();
			mPlayer = null;
		}
	}

}

[pglinkadssmall1]

Demo

Let us test the application:
Right click on the project >> Run as >> Android application
You could see following screen:



Download Source Code

Entire project is zipped and is available for download. Unzip the downloaded project and to import the project into eclipse, launch eclipse >> File >> Import.. >> Choose downloaded project(How to import android project in eclipse). If you just want to run the application in your mobile and see the output but don’t want to hit your head with source code, download application(apk) file and install it in your mobile device.

Download Source Code Download Application(apk)

*apk in Android is the installation file simliar to exe in windows.

[pglinkadssmall]

I hope you enjoyed the post!! :)
Keep us posted with your valuable comments below :)
[pgfeedback]
[pgwriteforus]

Related Articles

Author: Android Guru

Share This Post On
468 ad
  • Sandy

    How
    to create a JSON api for ASP.net website that connect to MS SQL
    Database and retrieve stored procedures values from the database?

    I want to create the android app which will communicate with the
    existing website the site is in asp.net how to make both of them
    communicate i dont have any idea i read about json but how to implement
    it and to conncet it with the webservice or the website dont have any
    ideas, so any suggestions regarding this will be appreciated, For
    example if i log in from the android app the user should be logged on
    the website user account from that app and when someone signs up from
    the app user should be automatically created on the app.

Join now to get more access to our Android Tutorials

Join now to get more access to our Android Tutorials

Once you Join, you will receive Mail containing latest Android Tutorials once a month !!

I promise I wont spam you !!

You have Successfully Subscribed!

Pin It on Pinterest

Shares