Audio Player


Audio Player

Generally, if you use an android system, with a Media Player class, you can get access to the audio or video files. Furthermore, you can have various playback options like a resume, pause, forward, reverse, and many more options.

The implementation of an audio player to perform various actions using a Media Player is given below.

Code 

To play a file you need to make a new raw folder in the res directory. After that, add an audio file with the help of a MediaPlayer class.

Then, from \res\layout folder path, open activity_main.xml file and start writing the code. 

Then, from \java\com.tutlane.audioplayerexample pathopen the main activity file MainActivity.java. Now, write the code as follows:

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingLeft="10dp"     android:paddingRight="10dp">     <TextView         android:id="@+id/txtVw1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="Now Playing: "         android:layout_marginTop="30dp"         android:textAppearance="?android:attr/textAppearanceMedium" />     <TextView         android:id="@+id/txtSname"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignBaseline="@+id/txtVw1"         android:layout_toRightOf="@+id/txtVw1"         android:text="TextView" />     <ImageView         android:id="@+id/imgLogo"         android:layout_width="match_parent"         android:layout_height="450dp"         android:layout_below="@+id/txtVw1"         android:src="@drawable/tutlane" />     <ImageButton         android:id="@+id/btnBackward"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_marginBottom="44dp"         android:layout_marginLeft="20dp"         android:src="@android:drawable/ic_media_rew" />     <ImageButton         android:id="@+id/btnPlay"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/btnBackward"         android:layout_marginLeft="20dp"         android:layout_toRightOf="@+id/btnBackward"         android:src="@android:drawable/ic_media_play" />     <ImageButton         android:id="@+id/btnPause"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/btnPlay"         android:layout_marginLeft="20dp"         android:layout_toRightOf="@+id/btnPlay"         android:src="@android:drawable/ic_media_pause" />     <ImageButton         android:id="@+id/btnForward"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/btnPause"         android:layout_marginLeft="20dp"         android:layout_toRightOf="@+id/btnPause"         android:contentDescription="@+id/imageButton3"         android:src="@android:drawable/ic_media_ff" />       <TextView         android:id="@+id/txtStartTime"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignTop="@+id/sBar"         android:text="0 min, 0 sec" />     <SeekBar         android:id="@+id/sBar"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_above="@+id/btnBackward"         android:layout_toLeftOf="@+id/txtSongTime"         android:layout_toRightOf="@+id/txtStartTime" />     <TextView         android:id="@+id/txtSongTime"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_toRightOf="@+id/btnForward"         android:layout_alignTop="@+id/sBar"         android:text="0 min, 0 sec " /> </RelativeLayout>

MainActivity.java

package com.tutlane.mediaplayerexample; import android.media.MediaPlayer; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast; import java.util.concurrent.TimeUnit; public class MainActivity extends AppCompatActivity {     private ImageButton forwardbtn, backwardbtn, pausebtn, playbtn;     private MediaPlayer mPlayer;     private TextView songName, startTime, songTime;     private SeekBar songPrgs;     private static int oTime =0, sTime =0, eTime =0, fTime = 5000, bTime = 5000;     private Handler hdlr = new Handler();     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         backwardbtn = (ImageButton)findViewById(R.id.btnBackward);         forwardbtn = (ImageButton)findViewById(R.id.btnForward);         playbtn = (ImageButton)findViewById(R.id.btnPlay);         pausebtn = (ImageButton)findViewById(R.id.btnPause);         songName = (TextView)findViewById(R.id.txtSname);         startTime = (TextView)findViewById(R.id.txtStartTime);         songTime = (TextView)findViewById(R.id.txtSongTime);         songName.setText("Baitikochi Chuste");         mPlayer = MediaPlayer.create(this, R.raw.baitikochi_chuste);         songPrgs = (SeekBar)findViewById(R.id.sBar);         songPrgs.setClickable(false);         pausebtn.setEnabled(false);         playbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 Toast.makeText(MainActivity.this, "Playing Audio", Toast.LENGTH_SHORT).show();                 mPlayer.start();                 eTime = mPlayer.getDuration();                 sTime = mPlayer.getCurrentPosition();                 if(oTime == 0){                     songPrgs.setMax(eTime);                     oTime =1;                 }                 songTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(eTime),                         TimeUnit.MILLISECONDS.toSeconds(eTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS. toMinutes(eTime))) );                 startTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime),                         TimeUnit.MILLISECONDS.toSeconds(sTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS. toMinutes(sTime))) );                 songPrgs.setProgress(sTime);                 hdlr.postDelayed(UpdateSongTime, 100);                 pausebtn.setEnabled(true);                 playbtn.setEnabled(false);             }         });         pausebtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 mPlayer.pause();                 pausebtn.setEnabled(false);                 playbtn.setEnabled(true);                 Toast.makeText(getApplicationContext(),"Pausing Audio", Toast.LENGTH_SHORT).show();             }         });         forwardbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if((sTime + fTime) <= eTime)                 {                     sTime = sTime + fTime;                     mPlayer.seekTo(sTime);                 }                 else                 {                     Toast.makeText(getApplicationContext(), "Cannot jump forward 5 seconds", Toast.LENGTH_SHORT).show();                 }                 if(!playbtn.isEnabled()){                     playbtn.setEnabled(true);                 }             }         });         backwardbtn.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 if((sTime - bTime) > 0)                 {                     sTime = sTime - bTime;                     mPlayer.seekTo(sTime);                 }                 else                 {                     Toast.makeText(getApplicationContext(), "Cannot jump backward 5 seconds", Toast.LENGTH_SHORT).show();                 }                 if(!playbtn.isEnabled()){                     playbtn.setEnabled(true);                 }             }         });     }     private Runnable UpdateSongTime = new Runnable() {         @Override         public void run() {             sTime = mPlayer.getCurrentPosition();             startTime.setText(String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(sTime),                     TimeUnit.MILLISECONDS.toSeconds(sTime) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(sTime))) );             songPrgs.setProgress(sTime);             hdlr.postDelayed(this, 100);         }     }; }

Image

Image Not Found