Tuesday, 10 September 2019

How to Create Audio Music Player App for Android | Audio Media Player App in Android Studio


  
 
               In this post, we will see How to Create Audio Music Player App for Android or Audio Media Player App in Android Studio. 



                How to create Android First App Hello World using Android Studio, check in following link:
https://www.comrevo.com/2019/09/Android-First-App-Hello-World-using-Android-Studio.html 
 
                To know how to install Android Studio in Linux, check following link:
https://www.comrevo.com/2019/01/how-to-install-android-studio-in-linux.html.

                To find details about Architecture of Android Operating System, check following link:
https://www.comrevo.com/2018/10/architecture-of-android-operating-system.html

Explanation:
                 We need to create raw folder (directory) in res folder. Copy and paste audio file in this /res/raw folder.
                   


Program (activity_main.xml) 

Design Mode:
                   Add three buttons PLAY, PAUSE, STOP on Activity as shown in following screenshot.


Text Mode:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginTop="92dp"
        android:text="PLAY"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button_pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="156dp"
        android:layout_marginTop="76dp"
        android:text="PAUSE"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_play" />

    <Button
        android:id="@+id/button_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="75dp"
        android:text="STOP"
        android:textSize="24sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_pause" />
</androidx.constraintlayout.widget.ConstraintLayout>

Program (MainActivity.java)


package com.example.audioplayerapp;

import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    Button Play_button, Pause_button, Stop_button;

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

        Play_button=(Button) findViewById(R.id.button_play);
        Pause_button=(Button) findViewById(R.id.button_pause);
        Stop_button=(Button) findViewById(R.id.button_stop);

        final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.mazemaherpandhari);

        // Register Play_button for event
        Play_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                mPlayer.start();

            } } );

        Pause_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mPlayer.pause();

            } } );

        Stop_button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mPlayer.stop();

            } } );

    }
}
 
  
  
Output:  
                 Screenshot of the this app from mobile phone is as follows:  






No comments:

Post a comment