Android Play Audio file Tutorial

Android play audio :

 

In this tutorial we will deal with android playing audio file, i.e, sometimes we need to play a sound or even when we want to play audio songs just like media player this tutorial is for you.

 

We will be using a service class to do the process if you are not aware of using service class i will explain about it in coming tutorials.

Here now we will play audio file on button click.You can also automate it on a msg received or push notification or on a task completion.

https://androidcoding.in/

 

 

 

activity_main.xml :

 

We are tying to make a android play audio file from assets / android play audio file from raw so for this we need a simple UI so that it has a play and stop button.

So you have the full freedom in designing in implementing according to your desire, but make sure that buttons are working correctly.

 

<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="horizontal"
    android:background="@drawable/pic"
    android:gravity="center">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="#234789"
        android:layout_marginTop="160dp"
        android:gravity="center">

    <Button
        android:id="@+id/play"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@android:drawable/ic_media_play" />

    <Button
        android:id="@+id/stop"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="50dp"
        android:background="@android:drawable/checkbox_off_background" />

    </LinearLayout>

</LinearLayout>

 

Creating Media File :

 

Now here comes our service class so that audio clip/music/song can be played using this class where we will be having…

 

–> Creating service class

 

public void onCreate(){
    super.onCreate();
   audioplayer = MediaPlayer.create(this,R.raw.tune);
}

 

–> Starting service i.e., playing audio file

 

public int onStartCommand(Intent intent, int flags, int startId){
   audioplayer.start();
   Toast.makeText(Media.this, "Player Started", Toast.LENGTH_SHORT).show();
   if(audioplayer.isLooping() != true){
      Toast.makeText(Media.this, "Player Stopped", Toast.LENGTH_SHORT).show();
    }
    return 1;
}

 

–> Stopping service

 

public void onStop(){
   audioplayer.stop();
   audioplayer.release();
}

 

package com.androidcoding.abhishek.audioplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;

public class Media extends Service{
   MediaPlayer audioplayer;

   public void onCreate(){
       super.onCreate();
      audioplayer = MediaPlayer.create(this,R.raw.tune);
   }

   public int onStartCommand(Intent intent, int flags, int startId){
      audioplayer.start();
      Toast.makeText(Media.this, "Player Started", Toast.LENGTH_SHORT).show();
      if(audioplayer.isLooping() != true){
         Toast.makeText(Media.this, "Player Stopped", Toast.LENGTH_SHORT).show();
       }
       return 1;
   }

   public void onStop(){
      audioplayer.stop();
      audioplayer.release();
   }
   
   public void onPause(){
      audioplayer.stop();
      audioplayer.release();
   }
   
   public void onDestroy(){
      audioplayer.stop();
      audioplayer.release();
   }

   @Override
   public IBinder onBind(Intent objIndent) {
       return null;
   }
}

 

MainActivity File :

 

So the main functionality in MainActivity file is that we will be starting and ending a audio file using our created service class i.e.,¬† Media.class using two buttons “Start” And “Stop”.

 

–> Start /Play button

If you notice here we are starting service as

 

startService(objIntent);
start.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent objIntent = new Intent(MainActivity.this, Media.class);
        startService(objIntent);

    }
});

 

–> End /Stop button

If you notice here we are stopping service as

 

stopService(objIntent);

 

stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Intent objIntent = new Intent(MainActivity.this, Media.class);
        stopService(objIntent);

    }
});

 

Android play audio full code :

Providing the full code for android play audio tutorial implementation.

 

package com.androidcoding.abhishek.audioplayer;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.content.Intent;
import android.widget.Button;

public class MainActivity extends Activity {

    Button start,stop;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button)findViewById(R.id.play);
        stop = (Button)findViewById(R.id.stop);


        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent objIntent = new Intent(MainActivity.this, Media.class);
                startService(objIntent);

            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent objIntent = new Intent(MainActivity.this, Media.class);
                stopService(objIntent);

            }
        });
    }

}

 

Android play audio output :

This screen depicts the android play music mode.

android play audio

If you have any query in this tutorial on android play audio do let us know in the comment section below.If you like this tutorial do like and share us for more interesting updates.

Show Buttons
Hide Buttons
Read previous post:
Android Expandable Textview

  Android Expandable Textview : Introduction : Android Expandable Textview¬† will help you to show large sentences or paragraphs with...

Close