Kushal Patil Kushal Patil - 1 month ago 17
Android Question

Music services in android studio

I am very new to android world so u might see this question not relevant.
I am trying to develop Music app in android. But i don’t know how to create Good Service in android. i have created one but its not working correctly, I research on github and found some of the samples but its very confusing., I dont know how to processed. i have only one services which interact with play screen. Main Activity i bind service and stored the object on Static object and later on used that on Play screen. But Play puase and next button not working correctly.

here is my services

public class MusicService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener,
MediaPlayer.OnCompletionListener {
private MediaPlayer mPlayer;
private Uri mSongUri;

private ArrayList<Song> mListSongs;
private int SONG_POS = 0;

private final IBinder musicBind = new PlayerBinder();

private final String ACTION_STOP = "com.test.STOP";
private final String ACTION_NEXT = "com.test.NEXT";
private final String ACTION_PREVIOUS = "com.test.PREVIOUS";
private final String ACTION_PAUSE = "com.test.PAUSE";

private static final int STATE_PAUSED = 1;
private static final int STATE_PLAYING = 2;
private int mState = 0;
private static final int REQUEST_CODE_PAUSE = 101;
private static final int REQUEST_CODE_PREVIOUS = 102;
private static final int REQUEST_CODE_NEXT = 103;
private static final int REQUEST_CODE_STOP = 104;
public static int NOTIFICATION_ID = 11;
private Notification.Builder notificationBuilder;
private Notification mNotification;
private RecentStore mRecentStore;

public class PlayerBinder extends Binder {
//Service connection to play in background
public MusicService getService() {
Log.d("test", "getService()");
return MusicService.this;
}
}

@Override
public IBinder onBind(Intent intent) {
Log.d("test", "onBind Called ");
return musicBind;
}

@Override
public void onCreate() {
super.onCreate();
//Initializing the media player object
mPlayer = new MediaPlayer();
initPlayer();
mPlayer.setOnPreparedListener(this);
mPlayer.setOnCompletionListener(this);
mPlayer.setOnErrorListener(this);

mRecentStore = RecentStore.getInstance(this);

notificationBuilder = new Notification.Builder(getApplicationContext());
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (intent != null) {
String action = intent.getAction();
if (!TextUtils.isEmpty(action)) {
if (action.equals(ACTION_PAUSE)) {
playPauseSong();
} else if (action.equals(ACTION_NEXT)) {
nextSong();
} else if (action.equals(ACTION_PREVIOUS)) {
previousSong();
} else if (action.equals(ACTION_STOP)) {
stopSong();
stopSelf();
}
}
}
return super.onStartCommand(intent, flags, startId);
}

@Override
public boolean onUnbind(Intent intent) {
//Stop the Mediaplayer
mPlayer.stop();
mPlayer.release();
return false;
}

@Override
public void onCompletion(MediaPlayer mp) {
mPlayer.reset();
try {
if (SONG_POS != mListSongs.size() - 1) {
SONG_POS++;
} else
SONG_POS = 0;
mPlayer.setDataSource(getApplicationContext(), mListSongs.get(SONG_POS).songuri);
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
mPlayer.prepareAsync();

}

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
return false;
}

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

private void initPlayer() {
mPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}

public void startSong(Uri songuri, String songName) {//Set data & start playing music


mPlayer.reset();
mState = STATE_PLAYING;
mSongUri = songuri;
try {
mRecentStore.addSongId(getAudioId());
mPlayer.setDataSource(getApplicationContext(), mSongUri);
} catch (Exception e) {
Log.e("MUSIC SERVICE", "Error setting data source", e);
}
mPlayer.prepareAsync();
//updateNotification(songName);
}

public long getAudioId() {
Song track = getCurrentTrack();
if (track != null) {
return track.id;
}
return -1;
}

public Song getCurrentTrack() {
return getTrack(getPosn());
}

public synchronized Song getTrack(int index) {
if (index >= 0 && index < mListSongs.size() && mPlayer.isPlaying()) {
return mListSongs.get(index);
}

return null;
}

public int getState() {

return mState;
}

public void playPauseSong() {

if (mState == STATE_PAUSED) {
mState = STATE_PLAYING;
mPlayer.start();
} else {
mState = STATE_PAUSED;
mPlayer.pause();
}
}

public void stopSong() {
mPlayer.stop();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(NOTIFICATION_ID);
System.exit(0);
}

public void nextSong() {
if (SONG_POS == mListSongs.size() - 1) {
SONG_POS = 0;
startSong(mListSongs.get(SONG_POS).songuri, mListSongs.get(SONG_POS).title);
Common.CurrentSong = mListSongs.get(SONG_POS);
} else {
startSong(mListSongs.get(SONG_POS + 1).songuri, mListSongs.get(SONG_POS + 1).title);
Common.CurrentSong = mListSongs.get(SONG_POS + 1);
SONG_POS++;
}

}

public void previousSong() {
if (SONG_POS == 0) {
SONG_POS = mListSongs.size() - 1;
startSong(mListSongs.get(SONG_POS).songuri, mListSongs.get(SONG_POS).title);
Common.CurrentSong = mListSongs.get(SONG_POS);
} else {
startSong(mListSongs.get(SONG_POS - 1).songuri, mListSongs.get(SONG_POS - 1).title);
Common.CurrentSong = mListSongs.get(SONG_POS - 1);
SONG_POS--;
}

}

// public void nextSong() {
// if (SONG_POS == mListSongs.size() - 1) {
// SONG_POS = 0;
// startSong(mListSongs.get(SONG_POS).songuri, mListSongs.get(SONG_POS).title);
// } else {
// startSong(mListSongs.get(SONG_POS + 1).songuri, mListSongs.get(SONG_POS + 1).title);
// SONG_POS++;
// }
// }
//
// public void previousSong() {
// if (SONG_POS == 0) {
// SONG_POS = mListSongs.size() - 1;
// startSong(mListSongs.get(SONG_POS).songuri, mListSongs.get(SONG_POS).title);
// } else {
// startSong(mListSongs.get(SONG_POS - 1).songuri, mListSongs.get(SONG_POS - 1).title);
// SONG_POS--;
// }
// }

public void setSongURI(Uri uri) {
this.mSongUri = uri;
}

public int getPosn() {
return mPlayer.getCurrentPosition();
}

public int getDur() {
return mPlayer.getDuration();
}

public boolean isPlaying() {
return mPlayer.isPlaying();
}

public void pausePlayer() {
mPlayer.pause();
}

public void seek(int posn) {
mPlayer.seekTo(posn);
}

public void go() {
mPlayer.start();
}

public void setSongList(ArrayList<Song> listSong, int pos, int notification_id) {
mListSongs = listSong;
SONG_POS = pos;
NOTIFICATION_ID = notification_id;
}

public void setSelectedSong(int pos, int notification_id) {
SONG_POS = pos;
NOTIFICATION_ID = notification_id;
setSongURI(mListSongs.get(SONG_POS).songuri);
//showNotification();
startSong(mListSongs.get(SONG_POS).songuri, mListSongs.get(SONG_POS).title);
}


public int getAudioSessionId() {
return mPlayer.getAudioSessionId();
}

public void setSongList(ArrayList<Song> listSong) {
mListSongs = listSong;
}
}