Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Commit

Permalink
Add option to ignore audio focus
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanritscher committed Dec 28, 2021
1 parent 523d417 commit 6b62eb6
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/org/y20k/transistor/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ object Keys {
const val CMD_DISMISS_NOTIFICATION: String = "DISMISS_NOTIFICATION"
const val CMD_NEXT_STATION: String = "NEXT_STATION"
const val CMD_PREVIOUS_STATION: String = "PREVIOUS_STATION"
const val CMD_UPDATE_AUDIO_FOCUS_SETTING: String = "UPDATE_AUDIO_FOCUS_SETTING"


// preferences
Expand All @@ -91,6 +92,7 @@ object Keys {
const val PREF_PLAYER_STATE_BOTTOM_SHEET_STATE: String = "PLAYER_STATE_BOTTOM_SHEET_STATE"
const val PREF_PLAYER_STATE_SLEEP_TIMER_STATE: String = "PLAYER_STATE_SLEEP_TIMER_STATE"
const val PREF_PLAYER_METADATA_HISTORY: String = "PREF_PLAYER_METADATA_HISTORY"
const val PREF_HANDLE_AUDIO_FOCUS: String = "PREF_HANDLE_AUDIO_FOCUS"

// states
const val STATE_SLEEP_TIMER_STOPPED: Int = 0
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/org/y20k/transistor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package org.y20k.transistor

import android.content.SharedPreferences
import android.os.Bundle
import android.support.v4.media.session.MediaControllerCompat
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.navigation.fragment.NavHostFragment
Expand Down Expand Up @@ -90,6 +91,9 @@ class MainActivity: AppCompatActivity() {
Keys.PREF_THEME_SELECTION -> {
AppThemeHelper.setTheme(PreferencesHelper.loadThemeSelection())
}
Keys.PREF_HANDLE_AUDIO_FOCUS -> {
MediaControllerCompat.getMediaController(this).sendCommand(Keys.CMD_UPDATE_AUDIO_FOCUS_SETTING, null, null)
}
}
}
/*
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/org/y20k/transistor/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
}
}

// set up "Handle Audio Focus" preference
val preferenceHandleAudioFocus: CheckBoxPreference = CheckBoxPreference(activity as Context)
preferenceHandleAudioFocus.title = getString(R.string.pref_handle_audio_focus_title)
preferenceHandleAudioFocus.key = Keys.PREF_HANDLE_AUDIO_FOCUS
preferenceHandleAudioFocus.summary = getString(R.string.pref_handle_audio_focus_summary)
preferenceHandleAudioFocus.setDefaultValue(true)


// set up "Update Station Images" preference
val preferenceUpdateStationImages: Preference = Preference(activity as Context)
preferenceUpdateStationImages.title = getString(R.string.pref_update_station_images_title)
Expand Down Expand Up @@ -155,6 +163,7 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
val preferenceCategoryGeneral: PreferenceCategory = PreferenceCategory(activity as Context)
preferenceCategoryGeneral.title = getString(R.string.pref_general_title)
preferenceCategoryGeneral.contains(preferenceThemeSelection)
preferenceCategoryGeneral.contains(preferenceHandleAudioFocus)

val preferenceCategoryMaintenance: PreferenceCategory = PreferenceCategory(activity as Context)
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
Expand All @@ -171,6 +180,7 @@ class SettingsFragment: PreferenceFragmentCompat(), YesNoDialog.YesNoDialogListe
// setup preference screen
screen.addPreference(preferenceCategoryGeneral)
screen.addPreference(preferenceThemeSelection)
screen.addPreference(preferenceHandleAudioFocus)
screen.addPreference(preferenceCategoryMaintenance)
screen.addPreference(preferenceUpdateStationImages)
screen.addPreference(preferenceUpdateCollection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,8 @@ object PreferencesHelper {
return sharedPreferences.getBoolean(Keys.PREF_DOWNLOAD_OVER_MOBILE, Keys.DEFAULT_DOWNLOAD_OVER_MOBILE)
}

/* Loads handling of audio focus */
fun loadHandleAudioFocus(): Boolean {
return sharedPreferences.getBoolean(Keys.PREF_HANDLE_AUDIO_FOCUS, true)
}
}
14 changes: 14 additions & 0 deletions app/src/main/java/org/y20k/transistor/playback/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class PlayerService(): MediaBrowserServiceCompat() {
private lateinit var sleepTimer: CountDownTimer
private var sleepTimerTimeRemaining: Long = 0L
private var playbackRestartCounter: Int = 0
private var handleAudioFocus: Boolean = true

private val attributes = AudioAttributes.Builder()
.setContentType(C.CONTENT_TYPE_MUSIC)
Expand Down Expand Up @@ -132,6 +133,10 @@ class PlayerService(): MediaBrowserServiceCompat() {
// fetch the metadata history
metadataHistory = PreferencesHelper.loadMetadataHistory()

// fetch handle audio focus setting
handleAudioFocus = PreferencesHelper.loadHandleAudioFocus()
player.setAudioAttributes(attributes, handleAudioFocus)

// create a new MediaSession
createMediaSession()

Expand Down Expand Up @@ -800,6 +805,15 @@ class PlayerService(): MediaBrowserServiceCompat() {
notificationHelper.hideNotification()
return true
}
Keys.CMD_UPDATE_AUDIO_FOCUS_SETTING -> {
if (player is SimpleExoPlayer) {
handleAudioFocus = PreferencesHelper.loadHandleAudioFocus()
player.setAudioAttributes(attributes, handleAudioFocus)
return true
} else {
return false
}
}
else -> {
return false
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@
<string name="pref_theme_selection_mode_device_default">Identisch zum Gerät</string>
<string name="pref_theme_selection_mode_dark">Dunkler Modus</string>
<string name="pref_general_title">Allgemein</string>
<string name="pref_handle_audio_focus_title">Audio Focus berücksichtigen</string>
<string name="pref_handle_audio_focus_summary">Wiedergabe stoppen wenn andere Anwendung Audio ausgibt oder Telefonanruf gestartet wird</string>
<string name="pref_delete_all_title">Downloads löschen</string>
<string name="toastmessage_error_missing_storage_permission">Erteile die Berechtigung „Speicher lesen“, um diese Datei zu öffnen.</string>
<string name="dialog_yes_no_positive_button_update_covers">Aktualisieren</string>
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 @@ -76,6 +76,8 @@
<string name="pref_app_version_title">App Version</string>
<string name="pref_delete_all_title">Delete Downloads</string>
<string name="pref_general_title">General</string>
<string name="pref_handle_audio_focus_title">Handle Audio Focus</string>
<string name="pref_handle_audio_focus_summary">Stop playback when other app starts playback or phone call is started</string>
<string name="pref_maintenance_title">Maintenance</string>
<string name="pref_m3u_export_summary">Save your radio stations to an M3U playlist file that can be imported into other players.</string>
<string name="pref_m3u_export_title">Export M3U</string>
Expand Down

0 comments on commit 6b62eb6

Please sign in to comment.