implement the basic functionality

This commit is contained in:
tibbi 2016-01-11 20:18:28 +01:00
parent a53dc5a462
commit 1eab76d920
4 changed files with 124 additions and 9 deletions

View file

@ -61,12 +61,15 @@ public class MainActivity extends AppCompatActivity {
private void songPicked(int pos) {
musicService.setSong(pos, true);
updateSongInfo(songs.get(pos));
updateSongInfo(musicService.getCurrSong());
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.pause));
}
private void updateSongInfo(Song song) {
titleTV.setText(song.getTitle());
artistTV.setText(song.getArtist());
if (song != null) {
titleTV.setText(song.getTitle());
artistTV.setText(song.getArtist());
}
}
private void getSortedSongs() {
@ -129,24 +132,85 @@ public class MainActivity extends AppCompatActivity {
public void previousClicked() {
if (isPlaylistEmpty())
return;
playPreviousSong();
}
@OnClick(R.id.playPauseBtn)
public void playPauseClicked() {
if (isPlaylistEmpty())
return;
resumePauseSong();
}
@OnClick(R.id.nextBtn)
public void nextClicked() {
if (isPlaylistEmpty())
return;
playNextSong();
}
@OnClick(R.id.stopBtn)
public void stopClicked() {
if (isPlaylistEmpty())
return;
stopMusic();
}
public void stopMusic() {
if (musicService != null) {
musicService.stopMusic();
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.play));
}
}
public void resumePauseSong() {
if (songs.isEmpty() || musicService == null)
return;
if (musicService.isPlaying()) {
pauseSong();
} else {
resumeSong();
}
// in case we just launched the app and pressed play, also update the song and artist name
if (artistTV.getText().toString().trim().isEmpty())
updateSongInfo(musicService.getCurrSong());
}
private void resumeSong() {
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.pause));
musicService.resumePlayer();
}
private void pauseSong() {
if (musicService == null)
return;
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.play));
musicService.pausePlayer();
}
private void playPreviousSong() {
if (songs.isEmpty() || musicService == null)
return;
musicService.playPrevious();
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.pause));
updateSongInfo(musicService.getCurrSong());
}
private void playNextSong() {
if (songs.isEmpty() || musicService == null)
return;
musicService.playNext();
playPauseBtn.setImageDrawable(getResources().getDrawable(R.mipmap.pause));
updateSongInfo(musicService.getCurrSong());
}
private boolean isPlaylistEmpty() {

View file

@ -44,10 +44,6 @@ public class MusicService extends Service
player.setOnErrorListener(this);
}
public void playNext() {
setSong(getNewSongId(), true);
}
private int getNewSongId() {
final int cnt = songs.size();
if (cnt == 0) {
@ -65,6 +61,50 @@ public class MusicService extends Service
}
}
public boolean isPlaying() {
return player != null && player.isPlaying();
}
public void playPrevious() {
if (player == null)
initMediaPlayer();
// remove the latest song from the list
if (playedSongIDs.size() > 1) {
playedSongIDs.remove(playedSongIDs.size() - 1);
setSong(playedSongIDs.get(playedSongIDs.size() - 1), false);
}
}
public void pausePlayer() {
if (player == null)
initMediaPlayer();
player.pause();
}
public void resumePlayer() {
if (player == null)
initMediaPlayer();
if (currSong == null)
playNext();
else
player.start();
}
public void playNext() {
setSong(getNewSongId(), true);
}
public void stopMusic() {
if (player != null) {
// .stop() seems to misbehave weirdly
player.pause();
player.seekTo(0);
}
}
public void setSong(int songId, boolean addNewSong) {
if (player == null)
initMediaPlayer();
@ -85,6 +125,10 @@ public class MusicService extends Service
player.prepareAsync();
}
public Song getCurrSong() {
return currSong;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {

View file

@ -22,4 +22,13 @@ public class Song {
public String getTitle() {
return title;
}
@Override
public String toString() {
return "Song{"
+ "id=" + getId()
+ ", title=" + getTitle()
+ ", artist=" + getArtist()
+ "}";
}
}

View file

@ -54,7 +54,6 @@
android:layout_marginRight="@dimen/activity_margin"
android:layout_marginTop="@dimen/activity_margin"
android:gravity="center_horizontal"
android:text="Song Title"
android:textSize="18sp"/>
<TextView
@ -64,7 +63,6 @@
android:layout_below="@+id/songTitle"
android:layout_marginRight="@dimen/activity_margin"
android:gravity="center_horizontal"
android:text="Song Artist"
android:textSize="16sp"/>
<ListView