Skip to content

Commit c91ea81

Browse files
authored
Play sound when pressing buzzer (#223)
Closes #192
1 parent b84c526 commit c91ea81

7 files changed

Lines changed: 47 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ Translations have to be approved before being merged into the app. To become a t
2828

2929
* Feature graphic by https://unsplash.com/@accrualbowtie
3030
* Some icons by Google and https://materialdesignicons.com/
31-
* Some audio files from https://soundbible.com/
31+
* Sound for the timer by https://soundbible.com/
32+
* Sound for buzzer by https://mixkit.co

app/src/main/java/com/github/muellerma/tabletoptools/ui/fragments/BuzzersFragment.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.github.muellerma.tabletoptools.ui.fragments
22

3+
import android.media.AudioAttributes
4+
import android.media.AudioManager
5+
import android.media.MediaPlayer
36
import android.os.*
47
import android.util.Log
58
import android.view.*
@@ -11,6 +14,7 @@ import androidx.core.view.isVisible
1114
import androidx.lifecycle.Lifecycle
1215
import com.github.muellerma.tabletoptools.R
1316
import com.github.muellerma.tabletoptools.databinding.FragmentBuzzersBinding
17+
import com.github.muellerma.tabletoptools.utils.Prefs
1418
import com.google.android.material.dialog.MaterialAlertDialogBuilder
1519
import kotlinx.coroutines.delay
1620
import kotlinx.coroutines.launch
@@ -96,6 +100,18 @@ class BuzzersFragment : AbstractBaseFragment() {
96100
vibrator.vibrate(500)
97101
}
98102

103+
val sound = Prefs(requireContext()).buzzerSound
104+
if (sound != null) {
105+
val audioManager = requireContext().getSystemService<AudioManager>()!!
106+
val player = MediaPlayer.create(
107+
requireContext(),
108+
sound,
109+
AudioAttributes.Builder().build(),
110+
audioManager.generateAudioSessionId()
111+
)
112+
player.start()
113+
}
114+
99115
buzzers.forEach {
100116
if (it != pressedButton) {
101117
it.isEnabled = false

app/src/main/java/com/github/muellerma/tabletoptools/utils/Prefs.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ import com.github.muellerma.tabletoptools.R
88
import com.github.muellerma.tabletoptools.ui.fragments.TimerFragment
99

1010
class Prefs(private val context: Context) {
11-
var sharedPrefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
12-
private set
11+
private var sharedPrefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
1312

1413
val maxDiceCount: Int
1514
get() = sharedPrefs.getString("dices_max_count", "10")?.toInt() ?: 10
@@ -56,4 +55,11 @@ class Prefs(private val context: Context) {
5655
}
5756
}
5857

58+
val buzzerSound: Int?
59+
get() {
60+
return when (sharedPrefs.getString("buzzer_sound", null)) {
61+
"buzzer" -> R.raw.buzzer
62+
else -> null
63+
}
64+
}
5965
}

app/src/main/res/raw/buzzer.mp3

12.8 KB
Binary file not shown.

app/src/main/res/values/array.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@
2525
<item>@string/menu_timer_value</item>
2626
<item>@string/menu_buzzers_value</item>
2727
</string-array>
28+
29+
<string-array name="buzzer_sound">
30+
<item>@string/buzzer_sound_none</item>
31+
<item>@string/buzzer_sound_buzzer</item>
32+
</string-array>
33+
<string-array name="buzzer_sound_values" translatable="false">
34+
<item>none</item>
35+
<item>buzzer</item>
36+
</string-array>
2837
</resources>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
<string name="menu_preferences">Settings</string>
7979
<string name="keep_screen_on_enabled">Keep screen on</string>
8080
<string name="keep_screen_on_disabled">Don\'t keep screen on</string>
81+
<string name="buzzer_sound">Sound</string>
82+
<string name="buzzer_sound_none">None</string>
83+
<string name="buzzer_sound_buzzer">Buzzer</string>
8184

8285
<!-- About menu -->
8386
<string name="about">About</string>

app/src/main/res/xml/pref_main.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@
2929
android:title="@string/dice_show_roll_inc_slider"
3030
android:defaultValue="false" />
3131
</PreferenceCategory>
32+
<PreferenceCategory android:title="@string/menu_buzzers">
33+
<ListPreference
34+
android:key="buzzer_sound"
35+
android:title="@string/buzzer_sound"
36+
android:entryValues="@array/buzzer_sound_values"
37+
android:entries="@array/buzzer_sound"
38+
android:defaultValue="none"
39+
android:summary="%s" />
40+
</PreferenceCategory>
3241
<PreferenceCategory>
3342
<Preference
3443
android:key="about"

0 commit comments

Comments
 (0)