I had created music player service which stop automatically on back pressed. I want to set it continuously until songs are available or user manually close from notification window. Any help appreciated.
Here below i put some code which i had created from reference
private MediaPlayer player;
private final IBinder musicBind = new MusicBinder();
public void onCreate(){
super.onCreate();
player = new MediaPlayer();
initMusicPlayer();
}
public void initMusicPlayer(){
//set player properties
player.setWakeMode(getApplicationContext(),
PowerManager.PARTIAL_WAKE_LOCK);
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
//set listeners
player.setOnPreparedListener(this);
player.setOnCompletionListener(this);
player.setOnErrorListener(this);
}
public class MusicBinder extends Binder {
MusicService getService() {
return MusicService.this;
}
}
//activity will bind to service
@Override
public IBinder onBind(Intent intent) {
return musicBind;
}
//release resources when unbind
@Override
public boolean onUnbind(Intent intent){
//player.stop();
//player.release();
return false;
}
@Override
public void onDestroy() {
//stopForeground(true);
}
this code is used in activity to bind service bindService(playIntent, musicConnection, Context.BIND_AUTO_CREATE); startService(playIntent);
From above code my service is started but i'm not able to play song directly i had to click on play button
public void playSong() {
player.reset();
try {
sendName_image();
player.setDataSource(URI object);
player.prepareAsync();
} catch (Exception e) {
}
}
Above method is called on button click