user3164991
user3164991

Reputation: 33

Android Music Player Service not playing after home/back button is pressed

Am trying to write music player code in android.I wrote the MediaPlayer code in a service.Here is the code:

package com.example.audioservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
  static MediaPlayer mp;



    @Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    System.out.println("in MyService onCreate()");

    mp=MediaPlayer.create(getApplicationContext(),R.raw.subanallah);

}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        System.out.println("in onService onBind()");
        return null;
    }



    @Override
    public int onStartCommand(Intent intent,int flags, int startId) {
        super.onStart(intent, startId);
        Log.d("log", "In onStart.");
        System.out.println("in MyService onStartCommand()");

        mp.start();
        return Service.START_STICKY;
    }

}

and called the service from Activity

Intent s=new Intent(this,MyService.class);
        startService(s);

Added the Service in manifest file

<service android:name="com.example.audioservice.MyService"></service>

But when home/backbutton is pressed app is restarting again.My requirement is when home/back button is pressed the app should continously play the music.Please help me.

Upvotes: 1

Views: 1962

Answers (2)

duy
duy

Reputation: 579

Hope I can help you.

1.Use startForeground() (I haven't tried this way)
2.Put this code under startService(): bindService(Intent,ServiceConnection, BIND_IMPORTAN). Ofcourse, you need implement.(I use it and in my case that works).

Upvotes: 0

Satyaki Mukherjee
Satyaki Mukherjee

Reputation: 2879

Edited full code

Do this step by step ::

crate activity_main.xml and paste this

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Button
    android:id="@+id/playId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/stopId"
    android:layout_marginRight="53dp"
    android:text="Play" />

<Button
    android:id="@+id/stopId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="48dp"
    android:layout_marginTop="50dp"
    android:text="Stop" />

After that create MyService class and paste this

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class MyService extends Service {

    MediaPlayer mp; 
    @Override   
    public IBinder onBind(Intent arg0) {

        return null;

    }

    @Override   
    public void onCreate() 
    {   
      super.onCreate(); 
      mp = MediaPlayer.create(getApplicationContext(), R.raw.ram3);

    }

    @Override   
    public int onStartCommand(Intent intent, int flags, int startId) 
    {   
        mp.start(); 
        mp.setLooping(true);
        return 0;

    }

    @Override   
    public void onDestroy() 
    {   
        mp.release();       
        super.onDestroy();

    }

}

After that create MainActivity1 class and paste this

import android.os.Bundle;

import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.Button;

public class MainActivity1 extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button play, stop;

        play = (Button) findViewById(R.id.playId);      
        stop = (Button) findViewById(R.id.stopId);      
        play.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

        Intent service = new Intent(MainActivity1.this, MyService.class);

        startService(service);

        }

        });

        stop.setOnClickListener(new View.OnClickListener() {

        @Override

          public void onClick(View v) {

        Intent name = new Intent(MainActivity1.this, MyService.class);

        stopService(name);

        }

        });

    }

}

Last step is paste the following code AndroidManifest.XML

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity1"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service
        android:name=".MyService"
        android:enabled="true" >
    </service>
</application>

It's must work, no doubt about this.

Upvotes: 2

Related Questions