From f03fe63ecd8603575b5e0ab8f9f8b6f6b3821f5b Mon Sep 17 00:00:00 2001 From: Scott Jackson Date: Sat, 23 Apr 2016 14:46:56 -0700 Subject: #170 Add Variable Playback Speed --- .../dsub/fragments/NowPlayingFragment.java | 46 +++++++++++++++++++++ .../daneren2005/dsub/service/DownloadService.java | 32 +++++++++++++- .../github/daneren2005/dsub/util/Constants.java | 1 + .../ic_action_playback_speed_dark.png | Bin 0 -> 1008 bytes .../ic_action_playback_speed_light.png | Bin 0 -> 1229 bytes .../ic_action_playback_speed_dark.png | Bin 0 -> 625 bytes .../ic_action_playback_speed_light.png | Bin 0 -> 739 bytes .../ic_action_playback_speed_dark.png | Bin 0 -> 1429 bytes .../ic_action_playback_speed_light.png | Bin 0 -> 1689 bytes .../ic_action_playback_speed_dark.png | Bin 0 -> 2377 bytes .../ic_action_playback_speed_light.png | Bin 0 -> 2793 bytes .../ic_action_playback_speed_dark.png | Bin 0 -> 3618 bytes .../ic_action_playback_speed_light.png | Bin 0 -> 4334 bytes app/src/main/res/layout-land/download.xml | 5 +++ app/src/main/res/layout-large-land/download.xml | 5 +++ app/src/main/res/layout-port/download.xml | 5 +++ app/src/main/res/menu/playback_speed_options.xml | 22 ++++++++++ app/src/main/res/values/attrs.xml | 1 + app/src/main/res/values/strings.xml | 5 +++ app/src/main/res/values/themes.xml | 2 + 20 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 app/src/main/res/drawable-hdpi/ic_action_playback_speed_dark.png create mode 100644 app/src/main/res/drawable-hdpi/ic_action_playback_speed_light.png create mode 100644 app/src/main/res/drawable-mdpi/ic_action_playback_speed_dark.png create mode 100644 app/src/main/res/drawable-mdpi/ic_action_playback_speed_light.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_action_playback_speed_dark.png create mode 100644 app/src/main/res/drawable-xhdpi/ic_action_playback_speed_light.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_dark.png create mode 100644 app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_light.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_dark.png create mode 100644 app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_light.png create mode 100644 app/src/main/res/menu/playback_speed_options.xml (limited to 'app/src/main') diff --git a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java index c8e99f51..e4984fce 100644 --- a/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java +++ b/app/src/main/java/github/daneren2005/dsub/fragments/NowPlayingFragment.java @@ -51,6 +51,7 @@ import android.view.animation.AnimationUtils; 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; @@ -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; @@ -182,6 +184,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis 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); @@ -216,6 +219,7 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis bookmarkButton.setOnTouchListener(touchListener); rateBadButton.setOnTouchListener(touchListener); rateGoodButton.setOnTouchListener(touchListener); + playbackSpeedButton.setOnTouchListener(touchListener); emptyTextView.setOnTouchListener(touchListener); albumArtImageView.setOnTouchListener(new View.OnTouchListener() { @Override @@ -386,6 +390,48 @@ public class NowPlayingFragment extends SubsonicFragment implements OnGestureLis } }); + 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); + return true; + } + }); + popup.show(); + } + }); + } else { + playbackSpeedButton.setVisibility(View.GONE); + } + toggleListButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java index 01007b68..988dea5f 100644 --- a/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java +++ b/app/src/main/java/github/daneren2005/dsub/service/DownloadService.java @@ -76,6 +76,7 @@ import android.content.Intent; 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; @@ -161,6 +162,7 @@ public class DownloadService extends Service { private int cachedPosition = 0; private boolean downloadOngoing = false; private float volume = 1.0f; + private long delayUpdateProgress = 1000L; private AudioEffectsController effectsController; private RemoteControlState remoteState = LOCAL; @@ -1503,7 +1505,7 @@ public class DownloadService extends Service { while(isRunning) { try { onSongProgress(); - Thread.sleep(1000L); + Thread.sleep(delayUpdateProgress); } catch(Exception e) { isRunning = false; @@ -1545,7 +1547,7 @@ public class DownloadService extends Service { } } onSongProgress(cachedPosition < 2000 ? true: false); - Thread.sleep(1000L); + Thread.sleep(delayUpdateProgress); } catch(Exception e) { Log.w(TAG, "Crashed getting current position", e); @@ -1877,6 +1879,7 @@ public class DownloadService extends Service { cachedPosition = position; applyReplayGain(mediaPlayer, downloadFile); + applyPlaybackParams(mediaPlayer); if (start || autoPlayStart) { mediaPlayer.start(); @@ -1947,6 +1950,7 @@ public class DownloadService extends Service { } applyReplayGain(nextMediaPlayer, downloadFile); + applyPlaybackParams(nextMediaPlayer); } catch (Exception x) { handleErrorNext(x); } @@ -2600,6 +2604,30 @@ public class DownloadService extends Service { } } + public void setPlaybackSpeed(float playbackSpeed) { + Util.getPreferences(this).edit().putFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, playbackSpeed).commit(); + applyPlaybackParams(mediaPlayer); + if(nextMediaPlayer != null && nextPlayerState == PREPARED) { + applyPlaybackParams(nextMediaPlayer); + } + + delayUpdateProgress = Math.round(1000L / playbackSpeed); + } + public float getPlaybackSpeed() { + return Util.getPreferences(this).getFloat(Constants.PREFERENCES_KEY_PLAYBACK_SPEED, 1.0f); + } + 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) { diff --git a/app/src/main/java/github/daneren2005/dsub/util/Constants.java b/app/src/main/java/github/daneren2005/dsub/util/Constants.java index ec3489ab..230cce16 100644 --- a/app/src/main/java/github/daneren2005/dsub/util/Constants.java +++ b/app/src/main/java/github/daneren2005/dsub/util/Constants.java @@ -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"; diff --git a/app/src/main/res/drawable-hdpi/ic_action_playback_speed_dark.png b/app/src/main/res/drawable-hdpi/ic_action_playback_speed_dark.png new file mode 100644 index 00000000..5f4d890a Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_action_playback_speed_dark.png differ diff --git a/app/src/main/res/drawable-hdpi/ic_action_playback_speed_light.png b/app/src/main/res/drawable-hdpi/ic_action_playback_speed_light.png new file mode 100644 index 00000000..4b481822 Binary files /dev/null and b/app/src/main/res/drawable-hdpi/ic_action_playback_speed_light.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_action_playback_speed_dark.png b/app/src/main/res/drawable-mdpi/ic_action_playback_speed_dark.png new file mode 100644 index 00000000..b05ecefd Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_action_playback_speed_dark.png differ diff --git a/app/src/main/res/drawable-mdpi/ic_action_playback_speed_light.png b/app/src/main/res/drawable-mdpi/ic_action_playback_speed_light.png new file mode 100644 index 00000000..392d2b4d Binary files /dev/null and b/app/src/main/res/drawable-mdpi/ic_action_playback_speed_light.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_dark.png b/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_dark.png new file mode 100644 index 00000000..2aeadf7e Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_dark.png differ diff --git a/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_light.png b/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_light.png new file mode 100644 index 00000000..ce3f561c Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/ic_action_playback_speed_light.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_dark.png b/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_dark.png new file mode 100644 index 00000000..a9acc5e6 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_dark.png differ diff --git a/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_light.png b/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_light.png new file mode 100644 index 00000000..dcd6fea8 Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/ic_action_playback_speed_light.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_dark.png b/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_dark.png new file mode 100644 index 00000000..c1e92342 Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_dark.png differ diff --git a/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_light.png b/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_light.png new file mode 100644 index 00000000..3dc5f32a Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/ic_action_playback_speed_light.png differ diff --git a/app/src/main/res/layout-land/download.xml b/app/src/main/res/layout-land/download.xml index 9451a587..1bae4ae2 100644 --- a/app/src/main/res/layout-land/download.xml +++ b/app/src/main/res/layout-land/download.xml @@ -64,6 +64,11 @@ style="@style/DownloadActionImageButton" android:src="@android:drawable/star_big_off"/> + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 2d3794d6..7cd447f0 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -42,6 +42,7 @@ + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4b3dc1c9..896f3dac 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -220,6 +220,11 @@ Thumbs Up Thumbs Down Batch Mode + 0.5x + 1.0x + 1.5x + 2.0x + 3.0x New podcasts available New songs in playlists diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index e86084b0..9e95fe9d 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -43,6 +43,7 @@ @drawable/ic_menu_radio_dark @drawable/ic_toggle_star_outline_light @drawable/ic_menu_download_dark + @drawable/ic_action_playback_speed_light @drawable/main_offline_light @drawable/ic_menu_library_light @drawable/ic_menu_playlist_light @@ -113,6 +114,7 @@ @drawable/ic_menu_radio_dark @drawable/ic_toggle_star_outline_dark @drawable/ic_menu_download_dark + @drawable/ic_action_playback_speed_dark @drawable/main_offline_dark @drawable/ic_menu_library_dark @drawable/ic_menu_playlist_dark -- cgit v1.2.3