Introduction
This article explains the Audio Player in Android.
In this application, we will start audio using the service component. In this, you will first extend the service class that provides the methods of the service lifecycle.
The service lifecycle has the following methods:
-
onStartCommand(): when the component like an Activity requests a service to start by calling the startService() method then the system calls the onStartCommand() method to start the service.
-
onCreate(): this method is called by the system when the service is first created.
-
onDestroy(): this method is called when the service is no longer needed.
Create a project as in the following:
Step 1
Create an XML file and write this. In this, you will use two buttons inside the RelativeLayout.
- <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" >
- <Button
- android:id="@+id/button1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="pAudio"
- android:text="Play" />
- <Button
- android:layout_marginTop="80dp"
- android:id="@+id/button2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:onClick="sAudio"
- android:text="Stop" />
- </RelativeLayout>