Skip to content

Commit 7a4ca38

Browse files
Fixes all media playback bugs
Turns out I was wrong; the API I thought didn't work, actually works great, but you have to unduck apps otherwise they'll ignore it. Release audio focus request first; then call media button.
1 parent c552e9c commit 7a4ca38

4 files changed

Lines changed: 9 additions & 49 deletions

File tree

USER_GUIDE.md

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,14 @@ This will stop your music.
4242

4343
- We first ask nicely, then force the app to stop if it didn't comply.
4444
- Hobby will close automatically after this action is complete.
45-
- Better complete control is available if I program it, but
46-
it would require a dangerous permission you really should
47-
never grant to an app, so this workaround will stick around
48-
if you don't enable said permission
4945

5046
## Resume current media
5147

5248
Say
5349

5450
Play
5551

56-
- We can only ask nicely to play music, if the app refuses, we can't do anything about it. Music Player GO respects this command.
57-
- Launches the music player if no app responds to our play request.
58-
- Like stop, better control is possible if we accept a dangerous
59-
permission.
52+
- resumes playback of last played media
6053
- Hobby quits on complete
6154

6255
## Skip to next song
@@ -65,8 +58,7 @@ Say
6558

6659
Next
6760

68-
- We can only ask nicely, if the app refuses, we can't do anything about it. Music Player GO respects this command.
69-
- Better control if we use more dangerous permissions
61+
- Plays next media
7062
- Hobby quits on complete
7163

7264
## Skip to previous song
@@ -75,8 +67,7 @@ Say
7567

7668
Previous
7769

78-
- We can only ask nicely, if the app refuses, we can't do anything about it. Music Player GO respects this command.
79-
- Better control if we use more dangerous permissions
70+
- Plays previous media
8071
- Hobby quits on complete
8172

8273
## Launch a music player from search
Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,8 @@
11
--[[resume playing of current song
22
;; play -> resume current audio
3-
;; possibly in future I will redirect to the "music" command as default if
4-
;; play fails?
53
]]
6-
require("lua/lib/say")
7-
require("lua/lib/intents")
8-
require("lua/lib/util")
9-
did_ok = false
10-
first = false
114
function main()
12-
jcall("sendMediaButtonActionCallback", {button=4, down=br(false), up=br(true)})
13-
return "ok"
14-
end
15-
16-
function launchPlay()
17-
say("no app responded to play press, starting music activity")
18-
local intent = {
19-
action="android.intent.action.MAIN",
20-
categories={"android.intent.category.LAUNCHER", "android.intent.category.APP_MUSIC"},
21-
}
22-
startActivity(intent)
5+
jcall("sendMediaButtonAction", 4)
236
jcall("quit")
24-
end
25-
26-
function br(up)
27-
function broadcastReceiver(code, data, intent)
28-
if code == -1 then
29-
did_ok = true
30-
jcall("quit")
31-
elseif first then
32-
first = false
33-
elseif up then
34-
launchPlay()
35-
end
36-
end
7+
return "ok"
378
end

app/src/main/java/org/hobby/dispatcher/IntentDispatcher.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ class IntentDispatcher {
7070
@JvmStatic fun sendMediaButtonAction(action: Long) {
7171
val context = MainActivity.context!!
7272
val keyCode = PlaybackStateCompat.toKeyCode(action)
73-
val intent = Intent(Intent.ACTION_MEDIA_BUTTON)
74-
intent.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, keyCode))
75-
context.sendOrderedBroadcast(intent, null)
76-
intent.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_UP, keyCode))
77-
context.sendOrderedBroadcast(intent, null)
73+
val am = context.getSystemService(Context.AUDIO_SERVICE) as AudioManager
74+
am.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_DOWN, keyCode))
75+
am.dispatchMediaKeyEvent(KeyEvent(KeyEvent.ACTION_UP, keyCode))
7876
}
7977

8078
@JvmStatic fun stopMusic() {

app/src/main/java/org/hobby/dispatcher/LuaDispatcher.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class LuaDispatcher(id: Int) {
115115
fun copyAllResourcesToFiles() {
116116
val versionName = "version.txt"
117117
val updateRequired = BuildConfig.VERSION_NAME != readFile(versionName)
118-
if (updateRequired) {
118+
if (BuildConfig.DEBUG || updateRequired) {
119119
if (!updateRequired && BuildConfig.DEBUG) {
120120
LOG.severe("Copying initial files due to DEBUG flag");
121121
}

0 commit comments

Comments
 (0)