Android में Service से बैकग्राउंड म्यूजिक प्ले करने का तरीका
यह उदाहरण दिखाता है कि Service का उपयोग करके म्यूजिक को बैकग्राउंड में कैसे चलाया जाए ताकि ऐप बंद होने या दूसरी एक्टिविटी खुलने पर भी म्यूजिक बजता रहे।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="16dp">
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="PlayBackgroundSound"
android:text="Run background Sound" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Playing Music in Background"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>
चरण 3: MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void PlayBackgroundSound(View view) {
Intent intent = new Intent(this, BackgroundSoundService.class);
startService(intent);
}
}
चरण 4: रॉ रिसोर्स फोल्डर में ऑडियो फाइल रखें
res → New → Android Resource Directory → Type: raw बनाएं और उसमें ऑडियो फाइल (जैसे sound.mp3) कॉपी करें।
चरण 5: BackgroundSoundService.java
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
import androidx.annotation.Nullable;
public class BackgroundSoundService extends Service {
MediaPlayer mediaPlayer;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mediaPlayer = MediaPlayer.create(this, R.raw.sound);
mediaPlayer.setLooping(true);
mediaPlayer.setVolume(1.0f, 1.0f); // 0.0 से 1.0 तक
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mediaPlayer.start();
Toast.makeText(getApplicationContext(), "बैकग्राउंड में म्यूजिक चल रहा है", Toast.LENGTH_SHORT).show();
return START_STICKY; // सर्विस किल होने पर रीस्टार्ट
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
}
चरण 6: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".BackgroundSoundService" />
</application>
</manifest>
महत्वपूर्ण नोट्स
START_STICKYरिटर्न करने से सिस्टम द्वारा किल होने पर सर्विस रीस्टार्ट होती है।- Android 8+ (Oreo) में बैकग्राउंड सर्विस लिमिटेशन के कारण Foreground Service के साथ नोटिफिकेशन दिखाना आवश्यक है।
- मीडिया प्लेबैक के लिए MediaSessionCompat और MediaController उपयोग करना बेस्ट प्रैक्टिस है।