0




Hey There, Welcome to AndroidPlus. In our last tutorial we learned about some different types of buttons and toast notification.


see the last article here:

Today we shall Develop Classic Media Player App. By this Classis Media Player App we will see the use of some different media functions. We will learn to play, pause, stop, forward and backward a song. This tutorial will also teach you the use of handler. So let’s begin –

Layout For Classis Media Player-

  • Create an xml file in your layout folder and named it activity_main.xml.

learn to add files in your project here: http://www.androidplus.org/2014/05/learn-xml-and-java.html

  • Copy the below code in your xml file.

<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:background="#1c1c1c"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="26dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="120dp"
        android:text="PLAY" />

    <Button
        android:id="@+id/button2"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/seekBar1"
        android:text="fwd" />

    <Button
        android:id="@+id/button3"
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/seekBar1"
        android:text="bwd" />

    <Button
        android:id="@+id/button4"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="Stop" />

</RelativeLayout>


  • Your layout should look like this-


Android Media Player


#Note: you can also create your own layout but do not change the ID of the buttons and seekbar.

  • Now create a folder in res folder and name it raw. Copy an audio file in this folder. Remember the name of audio file should be in small letters.


JAVA Code For Classis Media Player-


Create a Java file in your SRC folder and named it Main_Activity.java.
Copy the below code in your Java file.




package com.example.mediaplayer;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;

public class MainActivity extends Activity {


 MediaPlayer mp;
 Button play,fwd,bwd,stop;
 SeekBar sb;
 int t=0,tym=5000,i,starttime;
 Handler hand=new Handler();
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  init();
 
  play.setOnClickListener(new OnClickListener() {
  
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    if(!mp.isPlaying()&&t==0){
    mp.start();
    rr.run();
    play.setText("Pause");
    sb.setProgress(mp.getCurrentPosition());
       hand.postDelayed(rr,100);
   t=0;
    }
    else if(t==1){
     init();
     sb.setProgress(0);
        hand.postDelayed(rr,100);
     mp.start();
     t=0;
    }
   
   
   
    else{
    mp.pause();
    sb.setProgress(mp.getCurrentPosition());
    hand.postDelayed(rr, 0);
    play.setText("Play");
    t=0;
    }
   }


  
  });

 fwd.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
  
  mp.seekTo(mp.getCurrentPosition()+tym);
 
  }
 });

 bwd.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
  
   mp.seekTo(mp.getCurrentPosition()-tym);
  
  }
 });


 stop.setOnClickListener(new OnClickListener() {
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   mp.reset();
   sb.setProgress(0);
   play.setText("Play");
   hand.removeCallbacks(rr);
   t=1;
  }
 });





 }

 private void init() {
  // TODO Auto-generated method stub
  play=(Button) findViewById(R.id.button1);
  fwd=(Button) findViewById(R.id.button2);
  bwd=(Button) findViewById(R.id.button3);
  stop=(Button)findViewById(R.id.button4);
  sb=(SeekBar)findViewById(R.id.seekBar1);
  mp=MediaPlayer.create(this, R.raw.nokia);
 }

 Runnable rr = new Runnable() {

  @Override
  public void run() {
   // TODO Auto-generated method stub
   sb.setProgress(mp.getCurrentPosition());
   hand.postDelayed(rr, 100);
  }
 };

}

AndroidManifest.xml File for Classis Media Player-

Copy the below code into androidmanifest.xml file-


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

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

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


Output-




Today we learned to create Android Classis Media Player App. Though this app is not a perfect media player but we get the some idea to make a media player app. In later tutorials we will fetch the music from the SD CARD and will show the songs in the list view. For any query feel free to comment.

Post a Comment

 
Top