Forum

January 29th, 2009
Current User: Guest
Please consider registering

MediaPlayer in Service problem

UserPost

6:31 pm
February 18, 2010


Bob

Guest

 
1

Hi, I want my app to run a service in the background playing a sound.

I created this service and when it create’s it is set to start the media player.

I want the media player to stop simply by stopping the service.

In my activity i have startService called and that starts the music but when I call stopService I have to force close.

If I put a simple Toast in the onDestroy than the whole thing works like a charm but I still have the problem of the sound hasn’t stopped.

Here is the code from my service..

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

public class MyService extends Service {

private MediaPlayer player;

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

@Override
public void onCreate() {
super.onCreate();
MediaPlayer playerlayer = MediaPlayer.create(MyService.this, R.raw.my_sound);
player.start();
player.setLooping(true);
}

@Override
public void onDestroy() {
super.onDestroy();
player.stop();
}

}



Reply to Topic: MediaPlayer in Service problem

Guest Name (Required):

Guest EMail (Required):

Topic Reply:

HTML Editor
Save New PostSmileys

Guest URL (required)

Math Required!
What is the sum of:
7 + 3
   

 
Confused Cool Cry Embarassed Frown Kiss Laugh Smile Surprised Wink Yell

 


Comments are closed.