Skip to main content

Media Player Programming in Android

Hi Friends.

This is Small Media Player (Start and Stop) Application

1) First Create Android Project.(mediap)

2) Create raw  Folder in res Folder
     now copy paste one .mp3 file in raw folder show in above image res/raw/a.mp3
3) Now Edit main.xml  see in Bellow

main.xml

<?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" />
    <LinearLayout
        android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:orientation="horizontal">

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

        <Button
            android:id="@+id/Stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop" />
       
    </LinearLayout>
4) Now Create Following Activity

 package media.p.in;

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;
/*
 * O@K Tech : Sukhadiya Raviraj
 */
public class MediapActivity extends Activity implements OnClickListener {

MediaPlayer mPlayer;// Media Player Object
Button start,stop;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mPlayer = MediaPlayer.create(this, R.raw.a);
        start = (Button)findViewById(R.id.Start);
        stop = (Button)findViewById(R.id.Stop);
        start.setOnClickListener(this);
        stop.setOnClickListener(this);
    }

public void onClick(View v) {
 if(start.isPressed())
         {
      mPlayer.start();// this method for Start Song
         }
       else if(stop.isPressed())
          {
      mPlayer.stop();//this method for Stop Song
      mPlayer.reset();
          }
}
}

Here MediaPlayer Object create and give the path of .mp3 file in create() method and use stop() and start() method for start and stop music .

5)  AndroidManifest.xml


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

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MediapActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

6) Now Run in Emulator . and see below Out put



Click on Start and Stop Button for Start and Stop Music.......

All iz Well

Comments

Post a Comment

Popular posts from this blog

Login Application using Database with dialog demo

Follow Below Steps for make simple login applicaiton in Android using database with Dialog demo. 1)DataBaseDemoActivity.java This file diplay login page and trasfer two edit text data into second activity........  package database.co.in; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class DataBaseDemoActivity extends Activity implements OnClickListener {     /** Called when the activity is first created. */     EditText mtxt_email, mtxt_password;     Button mLogin; @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);         mtxt_email = (EditText)findViewById(R.id.editEmail_id);   ...

Camara Application in Android(Use Hardware Camara)

hi .... write Following code in your project first create camaratest android project....... this example for take image and view in below image view check it........ and enjoy 1) CamaratestActivity.java   package com.in.test; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.hardware.Camera.ShutterCallback; import android.os.Bundle; import android.os.Handler; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; /*  * O@K tech...  */ public class CamaratestActivity extends Activity implements SurfaceHolder.Callback ,OnClickListener { Camera camera; SurfaceView surfaceview;     SurfaceHolder mHolder ;     Button btnSnap; ...

Audio Recording in Android

Simple State Diagram for Media Recorder... Here Sequence of Method call is as per state diagram of media Recorder .... Otherwise Invalid sate call Error will be Occur........ Steps: 1) Also her two permission add in Android manifest file <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 2) This is java file coding  audiorecording.java package com.example.recording; import java.io.File; import java.io.FileDescriptor; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import ...