Open top menu

               

 Audio file play from Raw folder


 . I have explained step by step…
 

Step 1:

You have to create raw  folder inside res folder. Then you have to paste the Mp3 audio file.

Step 2:

You have to used MediaPlayer class in MainActivity.java class MediaPlayer
MediaPlayer mp=MediaPlayer.create(this,R.raw.baby);

Step 3:
Create a activity_main.xml class inside your res/layout
<?xml version="1.0" encoding="utf-8"?>
<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"
     >

  <TextView 
        android:id="@+id/textView1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentTop="true" 
        android:layout_marginTop="30dp"
        android:textSize="20dip"
        android:text="Android beginner point" />

    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="25dp"
        android:text="stop"
        tools:ignore="RtlHardcoded" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="30dp"
        android:text="start"
        tools:ignore="RtlHardcoded" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="41dp"
        android:layout_toRightOf="@+id/button1"
        android:text="pause"
        tools:ignore="RtlHardcoded" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button1"
        android:layout_alignLeft="@+id/button1"
        android:layout_marginBottom="54dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_alignRight="@+id/button3"
        android:layout_marginRight="15dp"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_above="@+id/textView3"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:src="@drawable/iconprofile" />

</RelativeLayout>




 

Step 4:

Create MainActivity.java inside src folder.
package com.androidbeginner.testcode;

import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Environment; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView;
 
public class MainActivity extends Activity { 
    Button start,pause,stop;
    MediaPlayer mp;
    TextView satrttime,lasttime;
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
         
        start=(Button)findViewById(R.id.button1); 
        pause=(Button)findViewById(R.id.button2); 
        stop=(Button)findViewById(R.id.button3);
        satrttime=(TextView)findViewById(R.id.textView2);
        lasttime=(TextView)findViewById(R.id.textView3);
       
        //creating media player 
     
        mp= MediaPlayer.create(this, R.raw.baby);
       
        int current=mp.getDuration(); 
        lasttime.setText(String.valueOf(current));
        start.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                mp.start(); 
                int current=mp.getCurrentPosition();
                satrttime.setText(String.valueOf(current));
            } 
        }); 
        pause.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                mp.pause();
                int current=mp.getCurrentPosition();
                satrttime.setText(String.valueOf(current));
            } 
        }); 
        stop.setOnClickListener(new OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                mp.stop(); 
                int current=mp.getCurrentPosition();
                satrttime.setText(String.valueOf(current));
            } 
        }); 
    } 
}  



Tagged

0 comments