How To start audio using Service in android

public class MyServices extends Service{
private static final String TAG = “MyService”;
MediaPlayer player;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
//Toast.makeText(this, “Back”, Toast.LENGTH_LONG).show();
Log.d(TAG, “onCreate”);
player = MediaPlayer.create(MyServices.this, R.raw.natural);
player.setLooping(true); // Set looping
}
@Override
public void onDestroy() {
Toast.makeText(this, “Background Sound Stopped”, Toast.LENGTH_LONG).show();
Log.d(TAG, “onDestroy”);
player.stop();
player.release();
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, “Background Sound Enabled”, Toast.LENGTH_LONG).show();
Log.d(TAG, “onStart”);
player.start();
}
}



// in manifest file
<service android:enabled=”true” android:name=”.class name” />


TO start audio
startService(new Intent(Settings.this, MyServices.class));

TO stop service
stopService(new Intent(Settings.this, MyServices.class));


Android Training in Kerala