Friday, 28 August 2015

Android Programming Tutorials 30 : MediaPlayer Functionality In Android

OUT PUT Of The Tutorial:






Create Android Application:

  • File >> New >> Android Application
  • Enter Project name: MediaPlayerApp
  • Package: com.ambilpursunil.newapp
  • Keep other default selections, click Next until you reach Finish

                                   

     1.Simply Copy and Past the Code which is display below in the  MainActivity.java 

    package com.ambilpursunil.newapp;

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

    public class MainActivity extends Activity implements OnClickListener{
               
                private MediaPlayer mp;

                @Override
                protected void onCreate(Bundle savedInstanceState) {
                            super.onCreate(savedInstanceState);
                            setContentView(R.layout.activity_main);
                            setVolumeControlStream(AudioManager.STREAM_MUSIC);
                            Button btnSong1 = (Button) findViewById(R.id.btnSong1);
                            Button btnSong2 = (Button) findViewById(R.id.btnSong2);
                           
                            btnSong1.setOnClickListener(this);
                            btnSong2.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                            int resId;
                            switch(v.getId()){
     //Place songs in the raw folder of res, create raw folder res=>New Folder=>name it as raw=>click ok
                            case R.id.btnSong1:
                                        resId = R.raw.song;
                                        break;
     //Place songs in the raw folder of res, create raw folder res=>New Folder=>name it as raw=>click ok 
                            case R.id.btnSong2:
                                        resId = R.raw.songone;
                                        break;
                                       
                            default:
                                        resId = R.raw.song;
                                        break;
                            }
                           
               //if we having any resource from other media player we have to release
                            if(mp != null){
                                        mp.release();
                            }
                            //now creating a new media player to play song
                            mp = MediaPlayer.create(this, resId);
                            mp.start();
                }
                @Override
                protected void onDestroy() {
                            if(null !=mp){
                                        mp.release();
                            }
                            super.onDestroy();
                }
    }

     2.Simple Copy and Past the below code:  actvitiy_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/btnSong1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SONG 1"
            android:textSize="20dip"
             />

        <Button
            android:id="@+id/btnSong2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SONG 2"
            android:textSize="20dip" />

    </LinearLayout>

    3.Copy and past the code for manifest.xml

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

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

        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                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>

    4.Right click on the project and Run As-->Android Application




    OUT PUT Of The Tutorial:





    OutPut:

    When our application launch Two button will display song1 and song 2 to listen click on the buttons.


    Please Send Your Comments To ambilpura.sunil@gmail.com

    Stay Tune For Next Tutorial...  Alert Dialog Box In Android:

    No comments:

    Post a Comment