Skip to content

Commit

Permalink
Merge branch 'playback_speed'
Browse files Browse the repository at this point in the history
  • Loading branch information
daneren2005 committed Jul 27, 2016
2 parents add777d + aeb5cf0 commit 6377612
Show file tree
Hide file tree
Showing 27 changed files with 260 additions and 12 deletions.
15 changes: 8 additions & 7 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
compileSdkVersion 23
buildToolsVersion "23.0.3"
useLibrary 'org.apache.http.legacy'

defaultConfig {
applicationId "github.daneren2005.dsub"
Expand Down Expand Up @@ -42,11 +43,11 @@ android {
dependencies {
compile project(':Server Proxy')
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:support-v4:22.2.+'
compile 'com.android.support:appcompat-v7:22.2.+'
compile 'com.android.support:mediarouter-v7:22.2.+'
compile 'com.android.support:recyclerview-v7:22.2.+'
compile 'com.android.support:design:22.2.+'
compile 'com.android.support:support-v4:23.4.+'
compile 'com.android.support:appcompat-v7:23.4.+'
compile 'com.android.support:mediarouter-v7:23.4.+'
compile 'com.android.support:recyclerview-v7:23.4.+'
compile 'com.android.support:design:23.4.+'
compile 'com.google.android.gms:play-services-cast:8.1.0'
compile 'com.sothree.slidinguppanel:library:3.0.0'
compile 'de.hdodenhof:circleimageview:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected void onPostCreate(Bundle savedInstanceState) {
protected void createCustomActionBarView() {
actionBarSpinner = (Spinner) getLayoutInflater().inflate(R.layout.actionbar_spinner, null);
if((this instanceof SubsonicFragmentActivity || this instanceof SettingsActivity) && (Util.getPreferences(this).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true) || Util.getThemeRes(this) != R.style.Theme_DSub_Light_No_Color)) {
actionBarSpinner.setBackgroundResource(R.drawable.abc_spinner_mtrl_am_alpha);
actionBarSpinner.setBackgroundDrawable(DrawableTint.getTintedDrawableFromColor(this, R.drawable.abc_spinner_mtrl_am_alpha, android.R.color.white));
}
spinnerAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ViewFlipper;
Expand All @@ -59,7 +60,6 @@
import github.daneren2005.dsub.adapter.SectionAdapter;
import github.daneren2005.dsub.audiofx.EqualizerController;
import github.daneren2005.dsub.domain.Bookmark;
import github.daneren2005.dsub.domain.MusicDirectory;
import github.daneren2005.dsub.domain.PlayerState;
import github.daneren2005.dsub.domain.RepeatMode;
import github.daneren2005.dsub.domain.ServerInfo;
Expand All @@ -73,6 +73,7 @@
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.SilentBackgroundTask;
import github.daneren2005.dsub.adapter.DownloadFileAdapter;
import github.daneren2005.dsub.view.compat.CustomMediaRouteDialogFactory;
import github.daneren2005.dsub.view.FadeOutAnimation;
import github.daneren2005.dsub.view.FastScroller;
import github.daneren2005.dsub.view.UpdateView;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis
private ImageButton bookmarkButton;
private ImageButton rateBadButton;
private ImageButton rateGoodButton;
private ImageButton playbackSpeedButton;

private ScheduledExecutorService executorService;
private DownloadFile currentPlaying;
Expand Down Expand Up @@ -182,6 +184,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bu
bookmarkButton = (ImageButton) rootView.findViewById(R.id.download_bookmark);
rateBadButton = (ImageButton) rootView.findViewById(R.id.download_rating_bad);
rateGoodButton = (ImageButton) rootView.findViewById(R.id.download_rating_good);
playbackSpeedButton = (ImageButton) rootView.findViewById(R.id.download_playback_speed);
toggleListButton =rootView.findViewById(R.id.download_toggle_list);

playlistView = (RecyclerView)rootView.findViewById(R.id.download_list);
Expand Down Expand Up @@ -216,6 +219,7 @@ public boolean onTouch(View v, MotionEvent me) {
bookmarkButton.setOnTouchListener(touchListener);
rateBadButton.setOnTouchListener(touchListener);
rateGoodButton.setOnTouchListener(touchListener);
playbackSpeedButton.setOnTouchListener(touchListener);
emptyTextView.setOnTouchListener(touchListener);
albumArtImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
Expand Down Expand Up @@ -386,6 +390,49 @@ public void onClick(View view) {
}
});

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
playbackSpeedButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, v);
popup.getMenuInflater().inflate(R.menu.playback_speed_options, popup.getMenu());

popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
DownloadService downloadService = getDownloadService();
if (downloadService == null) {
return false;
}

float playbackSpeed = 1.0f;
switch (menuItem.getItemId()) {
case R.id.playback_speed_half:
playbackSpeed = 0.5f;
break;
case R.id.playback_speed_one_half:
playbackSpeed = 1.5f;
break;
case R.id.playback_speed_double:
playbackSpeed = 2.0f;
break;
case R.id.playback_speed_tripple:
playbackSpeed = 3.0f;
break;
}

downloadService.setPlaybackSpeed(playbackSpeed);
updateTitle();
return true;
}
});
popup.show();
}
});
} else {
playbackSpeedButton.setVisibility(View.GONE);
}

toggleListButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Expand Down Expand Up @@ -465,7 +512,8 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
}

boolean equalizerAvailable = downloadService != null && downloadService.getEqualizerAvailable();
if(equalizerAvailable && !downloadService.isRemoteEnabled()) {
boolean isRemoteEnabled = downloadService != null && downloadService.isRemoteEnabled();
if(equalizerAvailable && !isRemoteEnabled) {
SharedPreferences prefs = Util.getPreferences(context);
boolean equalizerOn = prefs.getBoolean(Constants.PREFERENCES_EQUALIZER_ON, false);
if (equalizerOn && downloadService != null) {
Expand All @@ -477,10 +525,17 @@ public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
menu.removeItem(R.id.menu_equalizer);
}

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isRemoteEnabled) {
playbackSpeedButton.setVisibility(View.GONE);
} else {
playbackSpeedButton.setVisibility(View.VISIBLE);
}

if(downloadService != null) {
MenuItem mediaRouteItem = menu.findItem(R.id.menu_mediaroute);
if(mediaRouteItem != null) {
MediaRouteButton mediaRouteButton = (MediaRouteButton) MenuItemCompat.getActionView(mediaRouteItem);
mediaRouteButton.setDialogFactory(new CustomMediaRouteDialogFactory());
mediaRouteButton.setRouteSelector(downloadService.getRemoteSelector());
}
}
Expand Down Expand Up @@ -741,6 +796,7 @@ public void run() {
downloadService.addOnSongChangedListener(NowPlayingFragment.this, true);
}
updateRepeatButton();
updateTitle();
}
});
}
Expand Down Expand Up @@ -1202,6 +1258,7 @@ public void onSongChanged(DownloadFile currentPlaying, int currentPlayingIndex)
rewindButton.setVisibility(View.GONE);
fastforwardButton.setVisibility(View.GONE);
}
updateTitle();
}

private void setupSubtitle(int currentPlayingIndex) {
Expand Down Expand Up @@ -1412,6 +1469,27 @@ public void updateRepeatButton() {
break;
}
}
private void updateTitle() {
DownloadService downloadService = getDownloadService();
float playbackSpeed = downloadService.getPlaybackSpeed();

String title = context.getResources().getString(R.string.button_bar_now_playing);
int stringRes = -1;
if(playbackSpeed == 0.5f) {
stringRes = R.string.download_playback_speed_half;
} else if(playbackSpeed == 1.5f) {
stringRes = R.string.download_playback_speed_one_half;
} else if(playbackSpeed == 2.0f) {
stringRes = R.string.download_playback_speed_double;
} else if(playbackSpeed == 3.0f) {
stringRes = R.string.download_playback_speed_tripple;
}

if(stringRes != -1) {
title += " (" + context.getResources().getString(stringRes) + ")";
}
setTitle(title);
}

@Override
protected List<Entry> getSelectedEntries() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,14 @@ protected MusicDirectory getMusicDirectory(String id, String name, boolean refre
}

protected void addToPlaylist(final List<Entry> songs) {
Iterator<Entry> it = songs.iterator();
while(it.hasNext()) {
Entry entry = it.next();
if(entry.isDirectory()) {
it.remove();
}
}

if(songs.isEmpty()) {
Util.toast(context, "No songs selected");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.PlaybackParams;
import android.media.audiofx.AudioEffect;
import android.net.wifi.WifiManager;
import android.os.Build;
Expand Down Expand Up @@ -106,6 +107,7 @@ public class DownloadService extends Service {
public static final String START_PLAY = "github.daneren2005.dsub.START_PLAYING";
public static final int FAST_FORWARD = 30000;
public static final int REWIND = 10000;
private static final long DEFAULT_DELAY_UPDATE_PROGRESS = 1000L;
private static final double DELETE_CUTOFF = 0.84;
private static final int REQUIRED_ALBUM_MATCHES = 4;
private static final int REMOTE_PLAYLIST_TOTAL = 3;
Expand Down Expand Up @@ -162,6 +164,7 @@ public class DownloadService extends Service {
private int cachedPosition = 0;
private boolean downloadOngoing = false;
private float volume = 1.0f;
private long delayUpdateProgress = DEFAULT_DELAY_UPDATE_PROGRESS;

private AudioEffectsController effectsController;
private RemoteControlState remoteState = LOCAL;
Expand Down Expand Up @@ -853,6 +856,9 @@ synchronized void setCurrentPlaying(DownloadFile currentPlaying, boolean showNot
if(this.currentPlaying != null) {
this.currentPlaying.setPlaying(false);
}
if(delayUpdateProgress != DEFAULT_DELAY_UPDATE_PROGRESS && !isNextPlayingSameAlbum(currentPlaying, this.currentPlaying)) {
resetPlaybackSpeed();
}
this.currentPlaying = currentPlaying;
if(currentPlaying == null) {
currentPlayingIndex = -1;
Expand Down Expand Up @@ -1507,7 +1513,7 @@ public void run() {
while(isRunning) {
try {
onSongProgress();
Thread.sleep(1000L);
Thread.sleep(delayUpdateProgress);
}
catch(Exception e) {
isRunning = false;
Expand Down Expand Up @@ -1549,7 +1555,7 @@ public void run() {
}
}
onSongProgress(cachedPosition < 2000 ? true: false);
Thread.sleep(1000L);
Thread.sleep(delayUpdateProgress);
}
catch(Exception e) {
Log.w(TAG, "Crashed getting current position", e);
Expand Down Expand Up @@ -1881,6 +1887,7 @@ public void onPrepared(MediaPlayer mediaPlayer) {
cachedPosition = position;

applyReplayGain(mediaPlayer, downloadFile);
applyPlaybackParams(mediaPlayer);

if (start || autoPlayStart) {
mediaPlayer.start();
Expand Down Expand Up @@ -1951,6 +1958,7 @@ public void onPrepared(MediaPlayer mp) {
}

applyReplayGain(nextMediaPlayer, downloadFile);
applyPlaybackParamsNext();
} catch (Exception x) {
handleErrorNext(x);
}
Expand Down Expand Up @@ -2604,6 +2612,54 @@ else if("2".equals(replayGainType)) {
}
}

public void setPlaybackSpeed(float playbackSpeed) {
Util.getPreferences(this).edit().putFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, playbackSpeed).commit();
applyPlaybackParamsMain();
if(nextMediaPlayer != null && nextPlayerState == PREPARED) {
applyPlaybackParamsNext();
}

delayUpdateProgress = Math.round(DEFAULT_DELAY_UPDATE_PROGRESS / playbackSpeed);
}
private void resetPlaybackSpeed() {
Util.getPreferences(this).edit().remove(Constants.PREFERENCES_KEY_PLAYBACK_SPEED).commit();
}

public float getPlaybackSpeed() {
return Util.getPreferences(this).getFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, 1.0f);
}

private synchronized void applyPlaybackParamsMain() {
applyPlaybackParams(mediaPlayer);
}
private synchronized void applyPlaybackParamsNext() {
if(isNextPlayingSameAlbum()) {
applyPlaybackParams(nextMediaPlayer);
}
}
private synchronized boolean isNextPlayingSameAlbum() {
return isNextPlayingSameAlbum(currentPlaying, nextPlaying);
}
private synchronized boolean isNextPlayingSameAlbum(DownloadFile currentPlaying, DownloadFile nextPlaying) {
if(currentPlaying == null || nextPlaying == null) {
return false;
} else {
return currentPlaying.getSong().getAlbum().equals(nextPlaying.getSong().getAlbum());
}
}

private synchronized void applyPlaybackParams(MediaPlayer mediaPlayer) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
float playbackSpeed = getPlaybackSpeed();

if(playbackSpeed != 1.0f || mediaPlayer.getPlaybackParams() != null) {
PlaybackParams playbackParams = new PlaybackParams();
playbackParams.setSpeed(playbackSpeed);
mediaPlayer.setPlaybackParams(playbackParams);
}
}
}

public void toggleStarred() {
final DownloadFile currentPlaying = this.currentPlaying;
if(currentPlaying == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public final class Constants {
public static final String PREFERENCES_KEY_CAST_STREAM_ORIGINAL = "castStreamOriginal";
public static final String PREFERENCES_KEY_HEADS_UP_NOTIFICATION = "headsUpNotification";
public static final String PREFERENCES_KEY_CAST_CACHE = "castCache";
public static final String PREFERENCES_KEY_PLAYBACK_SPEED = "playbackSpeed";

public static final String OFFLINE_SCROBBLE_COUNT = "scrobbleCount";
public static final String OFFLINE_SCROBBLE_ID = "scrobbleID";
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/github/daneren2005/dsub/util/DrawableTint.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.support.annotation.AttrRes;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.util.TypedValue;

Expand Down Expand Up @@ -48,6 +49,17 @@ public static Drawable getTintedDrawable(Context context, @DrawableRes int drawa
tintedDrawables.put(drawableRes, background);
return background;
}
public static Drawable getTintedDrawableFromColor(Context context, @DrawableRes int drawableRes, @ColorRes int colorRes) {
if(tintedDrawables.containsKey(drawableRes)) {
return tintedDrawables.get(drawableRes);
}

int color = context.getResources().getColor(colorRes);
Drawable background = context.getResources().getDrawable(drawableRes);
background.setColorFilter(color, PorterDuff.Mode.SRC_IN);
tintedDrawables.put(drawableRes, background);
return background;
}
public static int getColorRes(Context context, @AttrRes int colorAttr) {
int color;
if(attrMap.containsKey(colorAttr)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package github.daneren2005.dsub.view.compat;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.MediaRouteChooserDialog;
import android.support.v7.app.MediaRouteChooserDialogFragment;

import github.daneren2005.dsub.util.Util;

public class CustomMediaRouteChooserDialogFragment extends MediaRouteChooserDialogFragment {
@Override
public MediaRouteChooserDialog onCreateChooserDialog(Context context, Bundle savedInstanceState) {
return new MediaRouteChooserDialog(context, Util.getThemeRes(context));
}
}
Loading

0 comments on commit 6377612

Please sign in to comment.