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 Feb 20, 2024
1 parent ed184a2 commit f3d73cd
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/org/y20k/transistor/Keys.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object Keys {
const val CMD_CANCEL_SLEEP_TIMER: String = "CANCEL_SLEEP_TIMER"
const val CMD_PLAY_STREAM: String = "PLAY_STREAM"

const val CMD_UPDATE_AUDIO_FOCUS_SETTING: String = "UPDATE_AUDIO_FOCUS_SETTING"

// preferences
const val PREF_RADIO_BROWSER_API: String = "RADIO_BROWSER_API"
const val PREF_ONE_TIME_HOUSEKEEPING_NECESSARY: String = "ONE_TIME_HOUSEKEEPING_NECESSARY_VERSIONCODE_72" // increment to current app version code to trigger housekeeping that runs only once
Expand All @@ -92,6 +94,7 @@ object Keys {
const val PREF_EDIT_STATIONS: String = "EDIT_STATIONS"
const val PREF_EDIT_STREAMS_URIS: String = "EDIT_STREAMS_URIS"

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 @@ -96,6 +97,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 @@ -209,6 +217,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 @@ -232,6 +241,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 @@ -281,4 +281,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 @@ -127,6 +128,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 @@ -740,6 +745,15 @@ class PlayerService(): MediaBrowserServiceCompat() {
preparePlayer(true)
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 @@ -87,6 +87,8 @@
<string name="pref_edit_station_stream_summary_disabled">Editing of streaming links disabled. </string>
<string name="pref_edit_station_stream_title">Edit Streaming Links</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
Empty file modified gradlew
100644 → 100755
Empty file.

0 comments on commit f3d73cd

Please sign in to comment.