0

I'm trying to use service for a background music.

package com.example.neotavraham;

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


public class PlayMusicService extends Service implements MediaPlayer.OnPreparedListener {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION_PLAY)) {
      mMediaPlayer = MediaPlayer.create(this, R.raw.yedid_nefesh);
      mMediaPlayer.setOnPreparedListener(this);
      mMediaPlayer.prepareAsync(); // prepare async to not block main thread
    }
    return flags;
  }

  @Override
  public void onPrepared(MediaPlayer mp) {
    mp.start();
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }
}

I call it from the MainActivity with the line: startService(new Intent(PlayMusicService.ACTION_PLAY));

and of course I added an intent-filter

<intent-filter>
  <action android:name="com.example.neotavraham.PLAY"/>
</intent-filter>

I was looking for a fine solution in the internet but couldn't find one...what should I do?

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
kitsuneFox
  • 1,243
  • 3
  • 18
  • 31
  • so what is the problem with above? what is not working? – SMR Mar 04 '15 at 09:08
  • @SMR,Oops,i forgat to add the error, although i wrote it in the title, the app crashes and the error is "Service Intent must be explicit" – kitsuneFox Mar 04 '15 at 09:26
  • What did you name your service in the manifest? Please show us that part. – Stephan Branczyk Mar 04 '15 at 09:30
  • how did you start the service ? – dharmendra Mar 04 '15 at 09:37
  • 1
    Possible duplicate of [Android L (API 21) - java.lang.IllegalArgumentException: Service Intent must be explicit](http://stackoverflow.com/questions/27183164/android-l-api-21-java-lang-illegalargumentexception-service-intent-must-be) – regisd Feb 20 '16 at 22:07

2 Answers2

2

Please call the service from intent and set the action of that intent like this:

Intent i = new Intent(this,PlayMusicService.class);
i.setAction("com.example.neotavraham.PLAY");
startService(i);

This code will work.

Background service for start media player

package com.example.neotavraham;

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


public class PlayMusicService extends Service  {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getAction().equals(ACTION_PLAY)) {

      mMediaPlayer = MediaPlayer.create(this, R.raw.idil);
        mMediaPlayer.setLooping(true); // Set looping
        mMediaPlayer.setVolume(100,100);
        mMediaPlayer.start();
    }
    return flags;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

    @Override
    public void onDestroy() {
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }
}

Thanks

Sulabh Jain
  • 342
  • 1
  • 9
1

In your activity do the following:

Intent i = new Intent(this,PlayMusicService.class);
i.putExtra("action","com.example.neotavraham.PLAY");
startService(i);

In your service do the following:

package com.example.neotavraham;

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


public class PlayMusicService extends Service  {
  public static final String ACTION_PLAY = "com.example.neotavraham.PLAY";
  MediaPlayer mMediaPlayer = null;

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    if (intent.getStringExtra("action").equals(ACTION_PLAY)) {
      mMediaPlayer = MediaPlayer.create(this, R.raw.yedid_nefesh);
      mMediaPlayer = MediaPlayer.create(this, R.raw.idil);
        mMediaPlayer.setLooping(true); // Set looping
        mMediaPlayer.setVolume(100,100);
        mMediaPlayer.start();
    }
    return flags;
  }

  @Override
  public IBinder onBind(Intent intent) {
    return null;
  }

    @Override
    public void onDestroy() {
        mMediaPlayer.stop();
        mMediaPlayer.release();
    }
}

In your manifiest do the following:

<service
            android:name="com.example.neotavraham.PlayMusicService"
           />

This is the perfect code for your app. Hope this will work finally....

Sulabh Jain
  • 342
  • 1
  • 9
  • It works!, thank you very much! one last thing, i wan't to process this on another thread so im trying to use prepareAsync() as shown in my code, but it gives "java.lang.IllegalStateException" error – kitsuneFox Mar 04 '15 at 12:18
  • Since service is already in background you dont need to work with another thread... – Sulabh Jain Mar 04 '15 at 12:23