Skip to content

Commit

Permalink
Couple of fixes
Browse files Browse the repository at this point in the history
Metadata - so it works in Samsung and other external media clients
Autorewind
  • Loading branch information
izderadicka committed May 23, 2018
1 parent bed843e commit a59785d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 40 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "eu.zderadicka.audioserve"
minSdkVersion 21
targetSdkVersion 27
versionCode 13
versionName "0.6.6"
versionCode 14
versionName "0.6.7"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<service
android:name=".AudioService"
android:enabled="true"
android:exported="false">
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
Expand Down
12 changes: 9 additions & 3 deletions app/src/main/java/eu/zderadicka/audioserve/AudioService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ class AudioService : MediaBrowserServiceCompat() {

}

override fun onSeekTo(player: Player?, position: Long) {
previousPositionUpdateTime = 0
super.onSeekTo(player, position)
}

override fun onPlay(player: Player) {
Log.d(LOG_TAG, "Playback started")
val autoRewind = calcAutoRewind()
Expand Down Expand Up @@ -184,7 +189,8 @@ class AudioService : MediaBrowserServiceCompat() {
previousPositionUpdateTime = 0
val updatedBefore = System.currentTimeMillis() - prevPos
Log.d(LOG_TAG,"Determine autorewind for item ${currentMediaItem}, updated before${updatedBefore}")
return if (updatedBefore < 5* MINUTE_IN_MILLIS) 2000
return if (updatedBefore < 10_000) 0
else if (updatedBefore < 5* MINUTE_IN_MILLIS) 2000
else if (updatedBefore < 30 * MINUTE_IN_MILLIS) 15_000
else if (updatedBefore < YEAR_IN_MILLIS) 30_000
else 0
Expand Down Expand Up @@ -477,7 +483,7 @@ class AudioService : MediaBrowserServiceCompat() {
preparer.sourceFactory = null
}
"pref_autorewind" -> {
enableAutoRewind = sharedPreferences.getBoolean("pref_autorewind", false)
enableAutoRewind = sharedPreferences.getBoolean("pref_autorewind", true)
}
}
}
Expand All @@ -504,7 +510,7 @@ class AudioService : MediaBrowserServiceCompat() {
super.onCreate()
preloadFiles = PreferenceManager.getDefaultSharedPreferences(this).getString("pref_preload","2").toInt()
isOffline = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_offline",false)
enableAutoRewind = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_autorewind", false)
enableAutoRewind = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("pref_autorewind", true)
session = MediaSessionCompat(this, LOG_TAG)
session.controller.registerCallback(sessionCallback)
player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen
}
"pref_autorewind" -> {
if (pref !is CheckBoxPreference) return
val checked = sps.getBoolean("pref_autorewind", false)
val checked = sps.getBoolean("pref_autorewind", true)
if (checked) {
pref.summary=getString(R.string.pref_autorewind_selected_title)

Expand Down
9 changes: 8 additions & 1 deletion app/src/main/java/eu/zderadicka/audioserve/data/Model.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import eu.zderadicka.audioserve.utils.splitExtension
import java.io.File

const val METADATA_KEY_DURATION = MediaMetadataCompat.METADATA_KEY_DURATION
const val METADATA_KEY_TITLE = MediaMetadataCompat.METADATA_KEY_TITLE
const val METADATA_KEY_ARTIST = MediaMetadataCompat.METADATA_KEY_ARTIST
const val METADATA_KEY_LAST_POSITION = "eu.zderadicka.audioserve.last_position"
const val METADATA_KEY_BITRATE = "eu.zderadicka.audioserve.bitrate"
const val METADATA_KEY_TRANSCODED = "eu.zderadicka.audioserve.transcoded"
Expand Down Expand Up @@ -88,10 +90,11 @@ class AudioFolder(name: String, path: String, val subfolders: ArrayList<Subfolde
val extras = Bundle()
val mediaId = prefixPath(f.path, ITEM_TYPE_AUDIOFILE)
val (name, ext) = splitExtension(f.name)
val album = File(f.path).parent
val descBuilder = MediaDescriptionCompat.Builder()
.setMediaId(mediaId)
.setTitle(name)
.setSubtitle(File(f.path).parent)
.setSubtitle(album)
if (ext != null) {
extras.putString(METADATA_KEY_EXTENSION, ext)
}
Expand All @@ -102,6 +105,10 @@ class AudioFolder(name: String, path: String, val subfolders: ArrayList<Subfolde
if (cache != null && cache.isCached(mediaId)) {
extras.putBoolean(METADATA_KEY_CACHED, true)
}
//this is needed for proper presentation to other MediaSession clients as Lock screen ...
extras.putString(METADATA_KEY_TITLE, name)
extras.putString(METADATA_KEY_ARTIST, album)

descBuilder.setExtras(extras)
val item = MediaItem(descBuilder.build(), MediaItem.FLAG_PLAYABLE)
if (cache?.shouldTranscode(item) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,46 @@ import eu.zderadicka.audioserve.data.METADATA_KEY_MEDIA_ID

class NotificationsManager(private val mService: AudioService) {

private val playAction: NotificationCompat.Action
private val pauseAction: NotificationCompat.Action
private val nextAction: NotificationCompat.Action
private val prevAction: NotificationCompat.Action
private val playAction= NotificationCompat.Action(
R.drawable.ic_play_white,
mService.getString(R.string.label_play),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_PLAY))
private val pauseAction = NotificationCompat.Action(
R.drawable.ic_pause_white,
mService.getString(R.string.label_pause),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_PAUSE))
private val nextAction = NotificationCompat.Action(
R.drawable.ic_next_white,
mService.getString(R.string.label_next),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_SKIP_TO_NEXT))
private val prevAction = NotificationCompat.Action(
R.drawable.ic_previous_white,
mService.getString(R.string.label_previous),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS))
private val rewindAction= NotificationCompat.Action(
R.drawable.ic_rewind_white,
mService.getString(R.string.label_rewind),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_REWIND))
private val forwardAction= NotificationCompat.Action(
R.drawable.ic_forward_white,
mService.getString(R.string.label_forward),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_FAST_FORWARD))

private val notificationManager: NotificationManager = mService.getSystemService(Context.NOTIFICATION_SERVICE)!! as NotificationManager

init {

playAction = NotificationCompat.Action(
R.drawable.ic_play_white,
mService.getString(R.string.label_play),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_PLAY))
pauseAction = NotificationCompat.Action(
R.drawable.ic_pause_white,
mService.getString(R.string.label_pause),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_PAUSE))
nextAction = NotificationCompat.Action(
R.drawable.ic_next_white,
mService.getString(R.string.label_next),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_SKIP_TO_NEXT))
prevAction = NotificationCompat.Action(
R.drawable.ic_previous_white,
mService.getString(R.string.label_previous),
MediaButtonReceiver.buildMediaButtonPendingIntent(
mService,
PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS))

// Cancel all notifications to handle the case where the Service was killed and
// restarted by the system.
notificationManager.cancelAll()
Expand Down Expand Up @@ -105,9 +112,18 @@ class NotificationsManager(private val mService: AudioService) {
playIndex+=1
}

if (state.actions and PlaybackStateCompat.ACTION_SEEK_TO != 0L) {
builder.addAction(rewindAction)
playIndex+=1
}

builder.addAction(if (state.state != PlaybackStateCompat.STATE_PLAYING) playAction else pauseAction)

// If skip to prev action is enabled.
if (state.actions and PlaybackStateCompat.ACTION_SEEK_TO != 0L) {
builder.addAction(forwardAction)
}


if (state.actions and PlaybackStateCompat.ACTION_SKIP_TO_NEXT != 0L) {
builder.addAction(nextAction)
}
Expand Down Expand Up @@ -137,6 +153,7 @@ class NotificationsManager(private val mService: AudioService) {
mService, PlaybackStateCompat.ACTION_STOP))
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

return builder
}
Expand All @@ -149,7 +166,7 @@ class NotificationsManager(private val mService: AudioService) {
val name = "Audioserve"
// The user-visible description of the channel.
val description = "Audioserve playback notification channel"
val importance = NotificationManager.IMPORTANCE_LOW
val importance = NotificationManager.IMPORTANCE_DEFAULT
val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
// Configure the notification channel.
mChannel.description = description
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,6 @@
<string name="pref_autorewind_title">Auto Rewind</string>
<string name="pref_autorewind_unselected_title">No auto rewind, playback will start exactly where it was paused</string>
<string name="pref_autorewind_selected_title">Before continuing playback it\'\'ll be rewind by a small amount (2s-30s), depending how long it was paused</string>
<string name="label_rewind">Rewind</string>
<string name="label_forward">Forward</string>
</resources>

0 comments on commit a59785d

Please sign in to comment.