react-native-audio-api 0.11.0-alpha.3 → 0.11.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/cpp/audioapi/android/core/AndroidAudioRecorder.cpp +34 -6
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/FFmpegFileWriter.cpp +4 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/ptrs.hpp +8 -0
- package/android/src/main/cpp/audioapi/android/core/utils/ffmpegBackend/utils.cpp +4 -0
- package/android/src/main/cpp/audioapi/android/core/utils/miniaudioBackend/MiniAudioFileWriter.h +1 -0
- package/android/src/main/java/com/swmansion/audioapi/AudioAPIModule.kt +164 -16
- package/android/src/main/java/com/swmansion/audioapi/core/NativeAudioPlayer.kt +10 -8
- package/android/src/main/java/com/swmansion/audioapi/core/NativeAudioRecorder.kt +10 -8
- package/android/src/main/java/com/swmansion/audioapi/system/AudioFocusListener.kt +3 -4
- package/android/src/main/java/com/swmansion/audioapi/system/CentralizedForegroundService.kt +128 -0
- package/android/src/main/java/com/swmansion/audioapi/system/ForegroundServiceManager.kt +116 -0
- package/android/src/main/java/com/swmansion/audioapi/system/MediaSessionManager.kt +115 -107
- package/android/src/main/java/com/swmansion/audioapi/system/PermissionRequestListener.kt +2 -1
- package/android/src/main/java/com/swmansion/audioapi/system/notification/BaseNotification.kt +47 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/NotificationRegistry.kt +191 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/PlaybackNotification.kt +669 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/PlaybackNotificationReceiver.kt +33 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/RecordingNotification.kt +303 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/RecordingNotificationReceiver.kt +45 -0
- package/android/src/main/java/com/swmansion/audioapi/system/notification/SimpleNotification.kt +119 -0
- package/common/cpp/audioapi/core/utils/AudioFileWriter.h +1 -0
- package/common/cpp/audioapi/core/utils/AudioRecorderCallback.h +1 -0
- package/common/cpp/audioapi/utils/AudioFileProperties.h +17 -17
- package/ios/audioapi/ios/AudioAPIModule.h +2 -2
- package/ios/audioapi/ios/AudioAPIModule.mm +108 -18
- package/ios/audioapi/ios/core/IOSAudioRecorder.mm +8 -7
- package/ios/audioapi/ios/core/NativeAudioPlayer.m +1 -1
- package/ios/audioapi/ios/core/NativeAudioRecorder.m +9 -2
- package/ios/audioapi/ios/system/AudioEngine.h +2 -0
- package/ios/audioapi/ios/system/AudioEngine.mm +49 -6
- package/ios/audioapi/ios/system/AudioSessionManager.mm +12 -9
- package/ios/audioapi/ios/system/NotificationManager.mm +7 -4
- package/ios/audioapi/ios/system/notification/BaseNotification.h +58 -0
- package/ios/audioapi/ios/system/notification/NotificationRegistry.h +70 -0
- package/ios/audioapi/ios/system/notification/NotificationRegistry.mm +172 -0
- package/ios/audioapi/ios/system/notification/PlaybackNotification.h +27 -0
- package/ios/audioapi/ios/system/notification/PlaybackNotification.mm +427 -0
- package/lib/commonjs/api.js +72 -1
- package/lib/commonjs/api.js.map +1 -1
- package/lib/commonjs/api.web.js +27 -14
- package/lib/commonjs/api.web.js.map +1 -1
- package/lib/commonjs/specs/NativeAudioAPIModule.js.map +1 -1
- package/lib/commonjs/system/AudioManager.js +6 -9
- package/lib/commonjs/system/AudioManager.js.map +1 -1
- package/lib/commonjs/system/index.js +13 -0
- package/lib/commonjs/system/index.js.map +1 -1
- package/lib/commonjs/system/notification/PlaybackNotificationManager.js +135 -0
- package/lib/commonjs/system/notification/PlaybackNotificationManager.js.map +1 -0
- package/lib/commonjs/system/notification/RecordingNotificationManager.js +182 -0
- package/lib/commonjs/system/notification/RecordingNotificationManager.js.map +1 -0
- package/lib/commonjs/system/notification/SimpleNotificationManager.js +122 -0
- package/lib/commonjs/system/notification/SimpleNotificationManager.js.map +1 -0
- package/lib/commonjs/system/notification/index.js +45 -0
- package/lib/commonjs/system/notification/index.js.map +1 -0
- package/lib/commonjs/system/notification/types.js +6 -0
- package/lib/commonjs/system/notification/types.js.map +1 -0
- package/lib/commonjs/types.js +17 -17
- package/lib/commonjs/types.js.map +1 -1
- package/lib/commonjs/web-system/index.js +17 -0
- package/lib/commonjs/web-system/index.js.map +1 -0
- package/lib/commonjs/web-system/notification/PlaybackNotificationManager.js +34 -0
- package/lib/commonjs/web-system/notification/PlaybackNotificationManager.js.map +1 -0
- package/lib/commonjs/web-system/notification/RecordingNotificationManager.js +34 -0
- package/lib/commonjs/web-system/notification/RecordingNotificationManager.js.map +1 -0
- package/lib/commonjs/web-system/notification/index.js +21 -0
- package/lib/commonjs/web-system/notification/index.js.map +1 -0
- package/lib/module/api.js +4 -0
- package/lib/module/api.js.map +1 -1
- package/lib/module/api.web.js +3 -1
- package/lib/module/api.web.js.map +1 -1
- package/lib/module/specs/NativeAudioAPIModule.js.map +1 -1
- package/lib/module/system/AudioManager.js +6 -9
- package/lib/module/system/AudioManager.js.map +1 -1
- package/lib/module/system/index.js +1 -0
- package/lib/module/system/index.js.map +1 -1
- package/lib/module/system/notification/PlaybackNotificationManager.js +131 -0
- package/lib/module/system/notification/PlaybackNotificationManager.js.map +1 -0
- package/lib/module/system/notification/RecordingNotificationManager.js +178 -0
- package/lib/module/system/notification/RecordingNotificationManager.js.map +1 -0
- package/lib/module/system/notification/SimpleNotificationManager.js +118 -0
- package/lib/module/system/notification/SimpleNotificationManager.js.map +1 -0
- package/lib/module/system/notification/index.js +7 -0
- package/lib/module/system/notification/index.js.map +1 -0
- package/lib/module/system/notification/types.js +4 -0
- package/lib/module/system/notification/types.js.map +1 -0
- package/lib/module/types.js +17 -17
- package/lib/module/types.js.map +1 -1
- package/lib/module/web-system/index.js +4 -0
- package/lib/module/web-system/index.js.map +1 -0
- package/lib/module/web-system/notification/PlaybackNotificationManager.js +30 -0
- package/lib/module/web-system/notification/PlaybackNotificationManager.js.map +1 -0
- package/lib/module/web-system/notification/RecordingNotificationManager.js +30 -0
- package/lib/module/web-system/notification/RecordingNotificationManager.js.map +1 -0
- package/lib/module/web-system/notification/index.js +5 -0
- package/lib/module/web-system/notification/index.js.map +1 -0
- package/lib/typescript/api.d.ts +2 -0
- package/lib/typescript/api.d.ts.map +1 -1
- package/lib/typescript/api.web.d.ts +3 -1
- package/lib/typescript/api.web.d.ts.map +1 -1
- package/lib/typescript/events/types.d.ts +3 -3
- package/lib/typescript/events/types.d.ts.map +1 -1
- package/lib/typescript/specs/NativeAudioAPIModule.d.ts +16 -5
- package/lib/typescript/specs/NativeAudioAPIModule.d.ts.map +1 -1
- package/lib/typescript/system/AudioManager.d.ts +4 -5
- package/lib/typescript/system/AudioManager.d.ts.map +1 -1
- package/lib/typescript/system/index.d.ts +1 -0
- package/lib/typescript/system/index.d.ts.map +1 -1
- package/lib/typescript/system/notification/PlaybackNotificationManager.d.ts +22 -0
- package/lib/typescript/system/notification/PlaybackNotificationManager.d.ts.map +1 -0
- package/lib/typescript/system/notification/RecordingNotificationManager.d.ts +23 -0
- package/lib/typescript/system/notification/RecordingNotificationManager.d.ts.map +1 -0
- package/lib/typescript/system/notification/SimpleNotificationManager.d.ts +20 -0
- package/lib/typescript/system/notification/SimpleNotificationManager.d.ts.map +1 -0
- package/lib/typescript/system/notification/index.d.ts +5 -0
- package/lib/typescript/system/notification/index.d.ts.map +1 -0
- package/lib/typescript/system/notification/types.d.ts +65 -0
- package/lib/typescript/system/notification/types.d.ts.map +1 -0
- package/lib/typescript/system/types.d.ts +0 -16
- package/lib/typescript/system/types.d.ts.map +1 -1
- package/lib/typescript/types.d.ts +16 -16
- package/lib/typescript/types.d.ts.map +1 -1
- package/lib/typescript/web-system/index.d.ts +2 -0
- package/lib/typescript/web-system/index.d.ts.map +1 -0
- package/lib/typescript/web-system/notification/PlaybackNotificationManager.d.ts +19 -0
- package/lib/typescript/web-system/notification/PlaybackNotificationManager.d.ts.map +1 -0
- package/lib/typescript/web-system/notification/RecordingNotificationManager.d.ts +19 -0
- package/lib/typescript/web-system/notification/RecordingNotificationManager.d.ts.map +1 -0
- package/lib/typescript/web-system/notification/index.d.ts +3 -0
- package/lib/typescript/web-system/notification/index.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/api.ts +17 -0
- package/src/api.web.ts +7 -2
- package/src/events/types.ts +3 -4
- package/src/specs/NativeAudioAPIModule.ts +23 -7
- package/src/system/AudioManager.ts +10 -23
- package/src/system/index.ts +1 -0
- package/src/system/notification/PlaybackNotificationManager.ts +193 -0
- package/src/system/notification/RecordingNotificationManager.ts +242 -0
- package/src/system/notification/SimpleNotificationManager.ts +170 -0
- package/src/system/notification/index.ts +4 -0
- package/src/system/notification/types.ts +111 -0
- package/src/system/types.ts +0 -18
- package/src/types.ts +17 -17
- package/src/web-system/index.ts +1 -0
- package/src/web-system/notification/PlaybackNotificationManager.ts +60 -0
- package/src/web-system/notification/RecordingNotificationManager.ts +60 -0
- package/src/web-system/notification/index.ts +2 -0
- package/android/src/main/java/com/swmansion/audioapi/system/LockScreenManager.kt +0 -347
- package/android/src/main/java/com/swmansion/audioapi/system/MediaNotificationManager.kt +0 -273
- package/android/src/main/java/com/swmansion/audioapi/system/MediaReceiver.kt +0 -57
- package/android/src/main/java/com/swmansion/audioapi/system/MediaSessionCallback.kt +0 -61
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
package com.swmansion.audioapi.system
|
|
2
|
-
|
|
3
|
-
import android.Manifest
|
|
4
|
-
import android.annotation.SuppressLint
|
|
5
|
-
import android.app.Notification
|
|
6
|
-
import android.app.PendingIntent
|
|
7
|
-
import android.app.Service
|
|
8
|
-
import android.content.Intent
|
|
9
|
-
import android.content.pm.ServiceInfo
|
|
10
|
-
import android.content.res.Resources
|
|
11
|
-
import android.os.Build
|
|
12
|
-
import android.os.IBinder
|
|
13
|
-
import android.provider.ContactsContract
|
|
14
|
-
import android.support.v4.media.session.PlaybackStateCompat
|
|
15
|
-
import android.util.Log
|
|
16
|
-
import android.view.KeyEvent
|
|
17
|
-
import androidx.annotation.RequiresPermission
|
|
18
|
-
import androidx.core.app.NotificationCompat
|
|
19
|
-
import androidx.core.app.NotificationManagerCompat
|
|
20
|
-
import com.facebook.react.bridge.ReactApplicationContext
|
|
21
|
-
import com.swmansion.audioapi.R
|
|
22
|
-
import java.lang.ref.WeakReference
|
|
23
|
-
|
|
24
|
-
class MediaNotificationManager(
|
|
25
|
-
private val reactContext: WeakReference<ReactApplicationContext>,
|
|
26
|
-
) {
|
|
27
|
-
private var smallIcon: Int = R.drawable.logo
|
|
28
|
-
private var customIcon: Int = 0
|
|
29
|
-
|
|
30
|
-
private var play: NotificationCompat.Action? = null
|
|
31
|
-
private var pause: NotificationCompat.Action? = null
|
|
32
|
-
private var stop: NotificationCompat.Action? = null
|
|
33
|
-
private var next: NotificationCompat.Action? = null
|
|
34
|
-
private var previous: NotificationCompat.Action? = null
|
|
35
|
-
private var skipForward: NotificationCompat.Action? = null
|
|
36
|
-
private var skipBackward: NotificationCompat.Action? = null
|
|
37
|
-
|
|
38
|
-
companion object {
|
|
39
|
-
const val REMOVE_NOTIFICATION: String = "audio_manager_remove_notification"
|
|
40
|
-
const val PACKAGE_NAME: String = "com.swmansion.audioapi.system"
|
|
41
|
-
const val MEDIA_BUTTON: String = "audio_manager_media_button"
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
enum class ForegroundAction {
|
|
45
|
-
START_FOREGROUND,
|
|
46
|
-
STOP_FOREGROUND,
|
|
47
|
-
;
|
|
48
|
-
|
|
49
|
-
companion object {
|
|
50
|
-
fun fromAction(action: String?): ForegroundAction? = entries.firstOrNull { it.name == action }
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
@SuppressLint("RestrictedApi")
|
|
55
|
-
@Synchronized
|
|
56
|
-
fun prepareNotification(
|
|
57
|
-
builder: NotificationCompat.Builder,
|
|
58
|
-
isPlaying: Boolean,
|
|
59
|
-
): Notification {
|
|
60
|
-
builder.mActions.clear()
|
|
61
|
-
|
|
62
|
-
if (previous != null) {
|
|
63
|
-
builder.addAction(previous)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
if (skipBackward != null) {
|
|
67
|
-
builder.addAction(skipBackward)
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (play != null && !isPlaying) {
|
|
71
|
-
builder.addAction(play)
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (pause != null && isPlaying) {
|
|
75
|
-
builder.addAction(pause)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (stop != null) {
|
|
79
|
-
builder.addAction(stop)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
if (next != null) {
|
|
83
|
-
builder.addAction(next)
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
if (skipForward != null) {
|
|
87
|
-
builder.addAction(skipForward)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
builder.setSmallIcon(if (customIcon != 0) customIcon else smallIcon)
|
|
91
|
-
|
|
92
|
-
val packageName: String? = reactContext.get()?.packageName
|
|
93
|
-
val openApp: Intent? = reactContext.get()?.packageManager?.getLaunchIntentForPackage(packageName!!)
|
|
94
|
-
try {
|
|
95
|
-
builder.setContentIntent(
|
|
96
|
-
PendingIntent.getActivity(
|
|
97
|
-
reactContext.get(),
|
|
98
|
-
0,
|
|
99
|
-
openApp,
|
|
100
|
-
PendingIntent.FLAG_IMMUTABLE,
|
|
101
|
-
),
|
|
102
|
-
)
|
|
103
|
-
} catch (e: Exception) {
|
|
104
|
-
Log.w("AudioManagerModule", "Error creating content intent: ${e.message}")
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
val remove = Intent(REMOVE_NOTIFICATION)
|
|
108
|
-
remove.putExtra(PACKAGE_NAME, reactContext.get()?.applicationInfo?.packageName)
|
|
109
|
-
builder.setDeleteIntent(
|
|
110
|
-
PendingIntent.getBroadcast(
|
|
111
|
-
reactContext.get(),
|
|
112
|
-
0,
|
|
113
|
-
remove,
|
|
114
|
-
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
|
115
|
-
),
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
return builder.build()
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
@RequiresPermission(Manifest.permission.POST_NOTIFICATIONS)
|
|
122
|
-
@Synchronized
|
|
123
|
-
fun updateNotification(
|
|
124
|
-
builder: NotificationCompat.Builder?,
|
|
125
|
-
isPlaying: Boolean,
|
|
126
|
-
) {
|
|
127
|
-
NotificationManagerCompat.from(reactContext.get()!!).notify(
|
|
128
|
-
MediaSessionManager.NOTIFICATION_ID,
|
|
129
|
-
prepareNotification(builder!!, isPlaying),
|
|
130
|
-
)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
fun cancelNotification() {
|
|
134
|
-
NotificationManagerCompat.from(reactContext.get()!!).cancel(MediaSessionManager.NOTIFICATION_ID)
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
@Synchronized
|
|
138
|
-
fun updateActions(mask: Long) {
|
|
139
|
-
play = createAction("play", "Play", mask, PlaybackStateCompat.ACTION_PLAY, play)
|
|
140
|
-
pause = createAction("pause", "Pause", mask, PlaybackStateCompat.ACTION_PAUSE, pause)
|
|
141
|
-
stop = createAction("stop", "Stop", mask, PlaybackStateCompat.ACTION_STOP, stop)
|
|
142
|
-
next = createAction("next", "Next", mask, PlaybackStateCompat.ACTION_SKIP_TO_NEXT, next)
|
|
143
|
-
previous = createAction("previous", "Previous", mask, PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS, previous)
|
|
144
|
-
skipForward = createAction("skip_forward_15", "Skip Forward", mask, PlaybackStateCompat.ACTION_FAST_FORWARD, skipForward)
|
|
145
|
-
skipBackward = createAction("skip_backward_15", "Skip Backward", mask, PlaybackStateCompat.ACTION_REWIND, skipBackward)
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
private fun createAction(
|
|
149
|
-
iconName: String,
|
|
150
|
-
title: String,
|
|
151
|
-
mask: Long,
|
|
152
|
-
action: Long,
|
|
153
|
-
oldAction: NotificationCompat.Action?,
|
|
154
|
-
): NotificationCompat.Action? {
|
|
155
|
-
if ((mask and action) == 0L) {
|
|
156
|
-
return null
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (oldAction != null) {
|
|
160
|
-
return oldAction
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
val r: Resources? = reactContext.get()?.resources
|
|
164
|
-
val packageName: String? = reactContext.get()?.packageName
|
|
165
|
-
val icon = r?.getIdentifier(iconName, "drawable", packageName)
|
|
166
|
-
|
|
167
|
-
val keyCode = PlaybackStateCompat.toKeyCode(action)
|
|
168
|
-
val intent = Intent(MEDIA_BUTTON)
|
|
169
|
-
intent.putExtra(Intent.EXTRA_KEY_EVENT, KeyEvent(KeyEvent.ACTION_DOWN, keyCode))
|
|
170
|
-
intent.putExtra(ContactsContract.Directory.PACKAGE_NAME, packageName)
|
|
171
|
-
val i =
|
|
172
|
-
PendingIntent.getBroadcast(
|
|
173
|
-
reactContext.get(),
|
|
174
|
-
keyCode,
|
|
175
|
-
intent,
|
|
176
|
-
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
return NotificationCompat.Action(icon!!, title, i)
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
class AudioForegroundService : Service() {
|
|
183
|
-
private var notification: Notification? = null
|
|
184
|
-
private var isServiceStarted = false
|
|
185
|
-
private val serviceLock = Any()
|
|
186
|
-
|
|
187
|
-
override fun onBind(intent: Intent): IBinder? = null
|
|
188
|
-
|
|
189
|
-
private fun startForegroundService() {
|
|
190
|
-
synchronized(serviceLock) {
|
|
191
|
-
if (!isServiceStarted) {
|
|
192
|
-
try {
|
|
193
|
-
notification =
|
|
194
|
-
MediaSessionManager.mediaNotificationManager
|
|
195
|
-
.prepareNotification(
|
|
196
|
-
NotificationCompat.Builder(this, MediaSessionManager.CHANNEL_ID),
|
|
197
|
-
false,
|
|
198
|
-
)
|
|
199
|
-
|
|
200
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
201
|
-
startForeground(
|
|
202
|
-
MediaSessionManager.NOTIFICATION_ID,
|
|
203
|
-
notification!!,
|
|
204
|
-
ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST,
|
|
205
|
-
)
|
|
206
|
-
} else {
|
|
207
|
-
startForeground(
|
|
208
|
-
MediaSessionManager.NOTIFICATION_ID,
|
|
209
|
-
notification,
|
|
210
|
-
)
|
|
211
|
-
}
|
|
212
|
-
isServiceStarted = true
|
|
213
|
-
} catch (ex: Exception) {
|
|
214
|
-
Log.e("AudioManagerModule", "Error starting foreground service: ${ex.message}")
|
|
215
|
-
stopSelf()
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
override fun onStartCommand(
|
|
222
|
-
intent: Intent?,
|
|
223
|
-
flags: Int,
|
|
224
|
-
startId: Int,
|
|
225
|
-
): Int {
|
|
226
|
-
val action = ForegroundAction.fromAction(intent?.action)
|
|
227
|
-
|
|
228
|
-
when (action) {
|
|
229
|
-
ForegroundAction.START_FOREGROUND -> startForegroundService()
|
|
230
|
-
ForegroundAction.STOP_FOREGROUND -> stopForegroundService()
|
|
231
|
-
else -> startForegroundService()
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return START_NOT_STICKY
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
private fun stopForegroundService() {
|
|
238
|
-
synchronized(serviceLock) {
|
|
239
|
-
if (isServiceStarted) {
|
|
240
|
-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
241
|
-
stopForeground(STOP_FOREGROUND_REMOVE)
|
|
242
|
-
}
|
|
243
|
-
isServiceStarted = false
|
|
244
|
-
stopSelf()
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
override fun onTaskRemoved(rootIntent: Intent?) {
|
|
250
|
-
super.onTaskRemoved(rootIntent)
|
|
251
|
-
stopForegroundService()
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
override fun onDestroy() {
|
|
255
|
-
synchronized(serviceLock) {
|
|
256
|
-
notification = null
|
|
257
|
-
isServiceStarted = false
|
|
258
|
-
}
|
|
259
|
-
super.onDestroy()
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
override fun onTimeout(startId: Int) {
|
|
263
|
-
stopForegroundService()
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
override fun onTimeout(
|
|
267
|
-
startId: Int,
|
|
268
|
-
fgsType: Int,
|
|
269
|
-
) {
|
|
270
|
-
stopForegroundService()
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
package com.swmansion.audioapi.system
|
|
2
|
-
|
|
3
|
-
import android.content.BroadcastReceiver
|
|
4
|
-
import android.content.Context
|
|
5
|
-
import android.content.Intent
|
|
6
|
-
import android.media.AudioManager
|
|
7
|
-
import android.support.v4.media.session.MediaSessionCompat
|
|
8
|
-
import android.view.KeyEvent
|
|
9
|
-
import com.facebook.react.bridge.ReactApplicationContext
|
|
10
|
-
import com.swmansion.audioapi.AudioAPIModule
|
|
11
|
-
import java.lang.ref.WeakReference
|
|
12
|
-
|
|
13
|
-
class MediaReceiver(
|
|
14
|
-
private val reactContext: WeakReference<ReactApplicationContext>,
|
|
15
|
-
private val mediaSession: WeakReference<MediaSessionCompat>,
|
|
16
|
-
private val mediaNotificationManager: WeakReference<MediaNotificationManager>,
|
|
17
|
-
private val audioAPIModule: WeakReference<AudioAPIModule>,
|
|
18
|
-
) : BroadcastReceiver() {
|
|
19
|
-
override fun onReceive(
|
|
20
|
-
context: Context?,
|
|
21
|
-
intent: Intent?,
|
|
22
|
-
) {
|
|
23
|
-
val action = intent!!.action
|
|
24
|
-
|
|
25
|
-
if (MediaNotificationManager.REMOVE_NOTIFICATION == action) {
|
|
26
|
-
if (!checkApp(intent)) return
|
|
27
|
-
|
|
28
|
-
mediaNotificationManager.get()?.cancelNotification()
|
|
29
|
-
MediaSessionManager.stopForegroundServiceIfNecessary()
|
|
30
|
-
mediaSession.get()?.isActive = false
|
|
31
|
-
|
|
32
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("closeNotification", mapOf()) // add to ts events
|
|
33
|
-
} else if (MediaNotificationManager.MEDIA_BUTTON == action || Intent.ACTION_MEDIA_BUTTON == action) {
|
|
34
|
-
if (!intent.hasExtra(Intent.EXTRA_KEY_EVENT)) return
|
|
35
|
-
if (!checkApp(intent)) return
|
|
36
|
-
|
|
37
|
-
val keyEvent = intent.getParcelableExtra<KeyEvent>(Intent.EXTRA_KEY_EVENT)
|
|
38
|
-
mediaSession.get()?.controller?.dispatchMediaButtonEvent(keyEvent)
|
|
39
|
-
} else if (AudioManager.ACTION_AUDIO_BECOMING_NOISY == action) {
|
|
40
|
-
mediaSession
|
|
41
|
-
.get()
|
|
42
|
-
?.controller
|
|
43
|
-
?.transportControls
|
|
44
|
-
?.pause()
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
private fun checkApp(intent: Intent): Boolean {
|
|
49
|
-
if (intent.hasExtra(MediaNotificationManager.PACKAGE_NAME)) {
|
|
50
|
-
val name = intent.getStringExtra(MediaNotificationManager.PACKAGE_NAME)
|
|
51
|
-
if (!reactContext.get()?.packageName.equals(name)) {
|
|
52
|
-
return false
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
return true
|
|
56
|
-
}
|
|
57
|
-
}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
package com.swmansion.audioapi.system
|
|
2
|
-
|
|
3
|
-
import android.os.Bundle
|
|
4
|
-
import android.support.v4.media.session.MediaSessionCompat
|
|
5
|
-
import android.util.Log
|
|
6
|
-
import com.swmansion.audioapi.AudioAPIModule
|
|
7
|
-
import java.lang.ref.WeakReference
|
|
8
|
-
|
|
9
|
-
class MediaSessionCallback(
|
|
10
|
-
private val audioAPIModule: WeakReference<AudioAPIModule>,
|
|
11
|
-
private val mediaNotificationManager: WeakReference<MediaNotificationManager>,
|
|
12
|
-
) : MediaSessionCompat.Callback() {
|
|
13
|
-
override fun onPlay() {
|
|
14
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remotePlay", mapOf())
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
override fun onPause() {
|
|
18
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remotePause", mapOf())
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
override fun onStop() {
|
|
22
|
-
mediaNotificationManager.get()?.cancelNotification()
|
|
23
|
-
MediaSessionManager.stopForegroundServiceIfNecessary()
|
|
24
|
-
|
|
25
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remoteStop", mapOf())
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
override fun onSkipToNext() {
|
|
29
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remoteNextTrack", mapOf())
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
override fun onSkipToPrevious() {
|
|
33
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remotePreviousTrack", mapOf())
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
override fun onFastForward() {
|
|
37
|
-
val body = HashMap<String, Any>().apply { put("value", 15) }
|
|
38
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remoteSkipForward", body)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
override fun onRewind() {
|
|
42
|
-
val body = HashMap<String, Any>().apply { put("value", 15) }
|
|
43
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remoteSkipBackward", body)
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
override fun onSeekTo(pos: Long) {
|
|
47
|
-
val body = HashMap<String, Any>().apply { put("value", (pos.toDouble() / 1000)) }
|
|
48
|
-
audioAPIModule.get()?.invokeHandlerWithEventNameAndEventBody("remoteChangePlaybackPosition", body)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
override fun onCustomAction(
|
|
52
|
-
action: String?,
|
|
53
|
-
extras: Bundle?,
|
|
54
|
-
) {
|
|
55
|
-
when (action) {
|
|
56
|
-
"SkipForward" -> onFastForward()
|
|
57
|
-
"SkipBackward" -> onRewind()
|
|
58
|
-
else -> Log.w("MediaSessionCallback", "Unknown custom action: $action")
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|