Android Program : Create an Android application that plays an audio(song) in the background. Audio will not be stopped even if you switch to another activity. To stop the audio, you need to stop the service.

Create an Android application that plays an audio(song) in the background. Audio  will not be stopped even if you switch to another activity. To stop the audio, you  need to stop the service.


activity_main.xml


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.    >  
  8.   
  9.   
  10.     <Button  
  11.         android:id="@+id/buttonStart"  
  12.         android:layout_width="wrap_content"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_alignParentTop="true"  
  15.         android:layout_centerHorizontal="true"  
  16.         android:layout_marginTop="74dp"  
  17.         android:text="Start Service" />  
  18.   
  19.     <Button  
  20.         android:id="@+id/buttonStop"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:layout_centerVertical="true"  
  25.         android:text="Stop Service" />  
  26.   
  27.     <Button  
  28.         android:id="@+id/buttonNext"  
  29.         android:layout_width="wrap_content"  
  30.         android:layout_height="wrap_content"  
  31.         android:layout_alignParentBottom="true"  
  32.         android:layout_centerHorizontal="true"  
  33.         android:layout_marginBottom="63dp"  
  34.         android:text="Next Page" />  
  35. </RelativeLayout>  


activity_next.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.androidservice.NextPage">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_marginEnd="8dp"  
  14.         android:layout_marginStart="8dp"  
  15.         android:layout_marginTop="200dp"  
  16.         android:text="Next Page"  
  17.         app:layout_constraintEnd_toEndOf="parent"  
  18.         app:layout_constraintStart_toStartOf="parent"  
  19.         app:layout_constraintTop_toTopOf="parent" />  
  20. </android.support.constraint.ConstraintLayout>  



MyService.java

  1. import android.app.Service;  
  2. import android.content.Intent;  
  3. import android.media.MediaPlayer;  
  4. import android.os.IBinder;  
  5. import android.support.annotation.Nullable;  
  6. import android.widget.Toast;  
  7.   
  8. public class MyService extends Service {  
  9.     MediaPlayer myPlayer;  
  10.     @Nullable  
  11.     @Override  
  12.     public IBinder onBind(Intent intent) {  
  13.         return null;  
  14.     }  
  15.     @Override  
  16.     public void onCreate() {  
  17.         Toast.makeText(this"Service Created", Toast.LENGTH_LONG).show();  
  18.   
  19.         myPlayer = MediaPlayer.create(this, R.raw.sun);  
  20.         myPlayer.setLooping(false); // Set looping  
  21.     }  
  22.     @Override  
  23.     public void onStart(Intent intent, int startid) {  
  24.         Toast.makeText(this"Service Started", Toast.LENGTH_LONG).show();  
  25.         myPlayer.start();  
  26.     }  
  27.     @Override  
  28.     public void onDestroy() {  
  29.         Toast.makeText(this"Service Stopped", Toast.LENGTH_LONG).show();  
  30.         myPlayer.stop();  
  31.     }  
  32. }  



 MainActivity.java

  1. import android.content.Intent;  
  2. import android.support.v7.app.AppCompatActivity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.widget.Button;  
  6.   
  7. public class MainActivity extends AppCompatActivity implements View.OnClickListener{  
  8.     Button buttonStart, buttonStop,buttonNext;  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.   
  14.         buttonStart = findViewById(R.id.buttonStart);  
  15.         buttonStop = findViewById(R.id.buttonStop);  
  16.         buttonNext =  findViewById(R.id.buttonNext);  
  17.   
  18.         buttonStart.setOnClickListener(this);  
  19.         buttonStop.setOnClickListener(this);  
  20.         buttonNext.setOnClickListener(this);  
  21.   
  22.   
  23.     }  
  24.     public void onClick(View src) {  
  25.         switch (src.getId()) {  
  26.             case R.id.buttonStart:  
  27.   
  28.                 startService(new Intent(this, MyService.class));  
  29.                 break;  
  30.             case R.id.buttonStop:  
  31.                 stopService(new Intent(this, MyService.class));  
  32.                 break;  
  33.             case R.id.buttonNext:  
  34.                 Intent intent=new Intent(this,NextPage.class);  
  35.                 startActivity(intent);  
  36.                 break;  
  37.         }  
  38.     }  
  39. }  


NextPage.java

  1. import android.support.v7.app.AppCompatActivity;  
  2. import android.os.Bundle;  
  3.   
  4. public class NextPage extends AppCompatActivity {  
  5.   
  6.     @Override  
  7.     protected void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.activity_next);  
  10.     }  
  11. }  



AndroidManifest.xml

  1. <service  
  2.             android:name=".MyService"  
  3.             android:enabled="true" />  






Post a Comment

0 Comments