Introduction
Adding audio to your android application is really not a hard task. All we need is to add your audio content into your Android Studio Project and try to play and pause the audio you have added. In this article, I will demonstrate adding audio into your Android Application and how to control it.
Steps to add your audio to your Android Studio project
Step 1
Open your Android Studio and create a new project.
Step 2
Create a new folder named "raw" in your Android project's "res" folder and place your audio file inside the "raw" folder.
You can see the audio is now successfully added into your Android Studio project by viewing the "raw" subfolder under "res" folder.
There is no visual component to interact with the audio that already comes within the Android Studio. The audio added will run in the background.
Steps to play and pause audio
Step 1
Set up the MediaPlayer class in onCreate() method of your Java code.
- MediaPlayer mediaPlayer = MediaPlayer.create(context: this, R.raw.your_audioname );
Android is providing MediaPlayer class to access built-in mediaplayer services like playing audio, video etc,.
Step 2
Since there is no visual component to control the audio, I am adding two buttons with id 'play' and 'pause' for the purpose of playing the audio and pausing it respectively.
And also I have set up the onClickListener for both the buttons.
Step 3
To start the audio, use the start() method of MediaPlayer Class
Step 4
To pause the audio, use the pause() method of MediaPlayer Class
For reference, here I provide the source code of my Android project.
activity_main.xml
- <?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/play"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="160dp"
- android:layout_marginTop="232dp"
- android:text="play"
- app:layout_constraintBottom_toTopOf="@+id/pause"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.006"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.578" />
-
- <Button
- android:id="@+id/pause"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="324dp"
- android:text="pause"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent" />
- </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
- package com.thetechiechum.audiodemo;
-
- 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 {
-
- MediaPlayer mediaPlayer;
- Button play,pause;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- mediaPlayer = MediaPlayer.create(this, R.raw.audioclip);
-
- play = (Button)findViewById(R.id.play);
- pause =(Button) findViewById(R.id.pause);
-
- play.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mediaPlayer.start();
- }
- });
-
- pause.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- mediaPlayer.pause();
- }
- });
-
- }
- }
In this article, we have seen the steps to add audio to your Android application and the ways to control the audio. In the next article, I will demonstrate how to adjust the volume of the audio.