Private
Public Access
1
0

Add paused state to playlist to help give right play/pause state to ui. Implement skeleton of playlist fragment.

This commit is contained in:
Kevin Whitaker
2016-01-17 12:19:15 -05:00
parent 55d189f95c
commit 2ae6a5b368
4 changed files with 209 additions and 3 deletions

View File

@@ -1,29 +1,181 @@
package com.magnatune.eyecreate.companionformagnatune.fragments;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import com.magnatune.eyecreate.companionformagnatune.R;
import com.magnatune.eyecreate.companionformagnatune.service.PlayerService;
import com.magnatune.eyecreate.companionformagnatune.service.Playlist;
public class PlaylistFragment extends Fragment {
ListView playlistUI;
ProgressBar position;
FloatingActionButton playPause;
ImageButton prev;
ImageButton next;
Playlist playlist;
//TODO:implement BRs
BroadcastReceiver receivePlaylist = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver playCalled = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver pauseCalled = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver playStarted = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver nextCalled = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver prevCalled = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver trackQueued = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver trackDequeued = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
BroadcastReceiver playlistCleared = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
}
};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_playlist, container, false);
playlistUI = (ListView) v.findViewById(R.id.list);
//TODO:finish getting UI set up.
position = (ProgressBar) v.findViewById(R.id.player_pos);
playPause = (FloatingActionButton) v.findViewById(R.id.play_pause);
prev = (ImageButton) v.findViewById(R.id.prev);
next = (ImageButton) v.findViewById(R.id.next);
//set up click events
playPause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent playPause = new Intent(getActivity(),PlayerService.class);
if(playlist.isPaused()) {
playPause.setAction(PlayerService.ACTION_TRACK_PLAY);
} else {
playPause.setAction(PlayerService.ACTION_TRACK_PAUSE);
}
getActivity().startService(playPause);
}
});
prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent prev = new Intent(getActivity(),PlayerService.class);
prev.setAction(PlayerService.ACTION_TRACK_PREV);
getActivity().startService(prev);
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(isLastSong()) {
playlist.setPaused(true);
//TODO:have play button go back to paused state
}
Intent next = new Intent(getActivity(),PlayerService.class);
next.setAction(PlayerService.ACTION_TRACK_NEXT);
getActivity().startService(next);
}
});
return v;
}
@Override
public void onPause() {
super.onPause();
//Deregister BRs to prevent calling when no longer visible.
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receivePlaylist);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(playCalled);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(pauseCalled);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(playStarted);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(nextCalled);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(prevCalled);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(trackQueued);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(trackDequeued);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(playlistCleared);
}
@Override
public void onResume() {
super.onResume();
//Register BRs to act upon service calls.
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receivePlaylist, new IntentFilter(PlayerService.ACTION_TRACK_LIST));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(playCalled, new IntentFilter(PlayerService.ACTION_TRACK_PLAY));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(pauseCalled, new IntentFilter(PlayerService.ACTION_TRACK_PAUSE));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(playStarted, new IntentFilter(PlayerService.ACTION_PLAYING_STARTED));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(nextCalled, new IntentFilter(PlayerService.ACTION_TRACK_NEXT));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(prevCalled, new IntentFilter(PlayerService.ACTION_TRACK_PREV));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(trackQueued, new IntentFilter(PlayerService.ACTION_TRACK_QUEUE));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(trackDequeued, new IntentFilter(PlayerService.ACTION_TRACK_DEQUEUE));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(playlistCleared, new IntentFilter(PlayerService.ACTION_PLAYLIST_CLEAR));
//Request playlist from service to set up/sync rest of control data.
Intent getPlaylist = new Intent(getActivity(),PlayerService.class);
getPlaylist.setAction(PlayerService.ACTION_TRACK_LIST);
getActivity().startService(getPlaylist);
}
private boolean isLastSong() {
//TODO:implement
return false;
}
//TODO:put in functions for buttons to send to player service and try to keep updated to it's state.
}

View File

@@ -92,6 +92,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
@Override
public void onCreate() {
super.onCreate();
Log.d(getClass().getSimpleName(),"service started.");
db = MagnatuneDBManager.getAlbumDB();
player = new MediaPlayer();
player.setOnPreparedListener(this);
@@ -109,6 +110,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
@Override
public void onDestroy() {
super.onDestroy();
Log.d(getClass().getSimpleName(),"service stopped.");
currentPlaylist.clearPlaylist();
LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(ACTION_PLAYLIST_CLEAR));
NotificationManagerCompat.from(this).cancel(notificationId);
@@ -199,6 +201,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
if(intent.hasExtra(EXTRA_TRACK_PLAYLIST_POSITION) && currentPlaylist.getPlaylistIds().size()>intent.getIntExtra(EXTRA_TRACK_PLAYLIST_POSITION,-1)) {
Track nextTrack = db.where(Track.class).equalTo("isrc", currentPlaylist.getPlaylistIds().get(currentPlaylist.getPlaylistIds().indexOf(intent.getIntExtra(EXTRA_TRACK_PLAYLIST_POSITION, -1)))).findFirst();
playThisTrack(nextTrack);
currentPlaylist.setPaused(false);
session.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING,player.getCurrentPosition(),0)
.setActions(PlaybackStateCompat.ACTION_PAUSE|PlaybackStateCompat.ACTION_SKIP_TO_NEXT|PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
@@ -209,6 +212,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
//Do nothing as currently loading or stopped.
} else {
player.start();
currentPlaylist.setPaused(false);
session.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING,player.getCurrentPosition(),0)
.setActions(PlaybackStateCompat.ACTION_PAUSE|PlaybackStateCompat.ACTION_SKIP_TO_NEXT|PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
@@ -220,6 +224,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
} else if(intent.getAction().equals(ACTION_TRACK_PAUSE)) {
if(player.isPlaying()) {
player.pause();
currentPlaylist.setPaused(true);
session.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PAUSED,player.getCurrentPosition(),0)
.setActions(PlaybackStateCompat.ACTION_PLAY|PlaybackStateCompat.ACTION_SKIP_TO_NEXT|PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS)
@@ -246,6 +251,7 @@ public class PlayerService extends Service implements MediaPlayer.OnPreparedList
}
} else if(intent.getAction().equals(ACTION_PLAYLIST_CLEAR)) {
player.reset();
currentPlaylist.setPaused(true);
mediaPrepared = false;
currentPlaylist.clearPlaylist();
currentPlaylist.setCurrentPositionMill(0);

View File

@@ -10,6 +10,7 @@ public class Playlist implements Parcelable {
private ArrayList<String> playlistIds;
private String currentlyPlayingId;
private int currentPositionMill;
private boolean paused = true;
public Playlist() {
playlistIds = new ArrayList<>();
@@ -19,6 +20,7 @@ public class Playlist implements Parcelable {
in.readStringList(getPlaylistIds());
setCurrentlyPlayingId(in.readString());
setCurrentPositionMill(in.readInt());
setPaused(in.readInt()==0?false:true);
}
public static final Creator<Playlist> CREATOR = new Creator<Playlist>() {
@@ -43,6 +45,7 @@ public class Playlist implements Parcelable {
parcel.writeStringList(getPlaylistIds());
parcel.writeString(getCurrentlyPlayingId());
parcel.writeInt(getCurrentPositionMill());
parcel.writeInt(isPaused()?1:0);
}
public ArrayList<String> getPlaylistIds() {
@@ -76,4 +79,12 @@ public class Playlist implements Parcelable {
public void setCurrentPositionMill(int currentPositionMill) {
this.currentPositionMill = currentPositionMill;
}
public boolean isPaused() {
return paused;
}
public void setPaused(boolean paused) {
this.paused = paused;
}
}

View File

@@ -1,10 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/list"/>
<!-- TODO:put in media controls here -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:id="@+id/player_pos"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_previous"
android:id="@+id/prev"/>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:elevation="10dp"
app:pressedTranslationZ="10dp"
android:clickable="true"
android:src="@android:drawable/ic_media_play"
android:id="@+id/play_pause"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_next"
android:id="@+id/next"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>