Skip to content

Commit 3ae0597

Browse files
committed
fix(android): workaround on orientation for Android
1 parent efd904d commit 3ae0597

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

android/src/main/java/video/api/reactnative/livestream/LiveStreamView.kt

+13
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import video.api.livestream.enums.CameraFacingDirection
1515
import video.api.livestream.interfaces.IConnectionListener
1616
import video.api.livestream.models.AudioConfig
1717
import video.api.livestream.models.VideoConfig
18+
import video.api.reactnative.livestream.utils.OrientationManager
1819
import video.api.reactnative.livestream.utils.permissions.PermissionsManager
1920
import video.api.reactnative.livestream.utils.permissions.SerialPermissionsManager
2021
import video.api.reactnative.livestream.utils.showDialog
@@ -33,6 +34,8 @@ class LiveStreamView @JvmOverloads constructor(
3334
PermissionsManager((context as ThemedReactContext).reactApplicationContext)
3435
)
3536

37+
private val orientationManager = OrientationManager(context)
38+
3639
// Connection listeners
3740
var onConnectionSuccess: (() -> Unit)? = null
3841
var onConnectionFailed: ((reason: String?) -> Unit)? = null
@@ -204,6 +207,15 @@ class LiveStreamView @JvmOverloads constructor(
204207
require(permissionsManager.hasPermission(Manifest.permission.CAMERA)) { "Missing permissions Manifest.permission.CAMERA" }
205208
require(permissionsManager.hasPermission(Manifest.permission.RECORD_AUDIO)) { "Missing permissions Manifest.permission.RECORD_AUDIO" }
206209

210+
/**
211+
* Workaround to reapply video config in case orientation has changed.
212+
* This happens because `configChanges` may be disabled in the AndroidManifest.xml of a RN
213+
* application.
214+
*/
215+
if (orientationManager.orientationHasChanged) {
216+
liveStream.videoConfig = liveStream.videoConfig
217+
}
218+
207219
url?.let { liveStream.startStreaming(streamKey, it) }
208220
?: liveStream.startStreaming(streamKey)
209221

@@ -219,6 +231,7 @@ class LiveStreamView @JvmOverloads constructor(
219231
}
220232

221233
override fun close() {
234+
orientationManager.close()
222235
liveStream.release()
223236
}
224237

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package video.api.reactnative.livestream.utils
2+
3+
import android.content.Context
4+
import android.view.OrientationEventListener
5+
import java.io.Closeable
6+
7+
class OrientationManager(context: Context) : Closeable {
8+
private val orientationEventListener = object : OrientationEventListener(context) {
9+
override fun onOrientationChanged(orientation: Int) {
10+
currentOrientation = (orientation + 45) / 90 * 90 % 360
11+
}
12+
}.apply {
13+
enable()
14+
}
15+
16+
private var currentOrientation: Int = OrientationEventListener.ORIENTATION_UNKNOWN
17+
set(value) {
18+
if (field != value) {
19+
if (field == OrientationEventListener.ORIENTATION_UNKNOWN) {
20+
// First time we get an orientation value, don't consider it as a change
21+
field = value
22+
return
23+
} else {
24+
field = value
25+
_orientationHasChanged = true
26+
}
27+
}
28+
}
29+
30+
31+
private var _orientationHasChanged: Boolean = false
32+
33+
val orientationHasChanged: Boolean
34+
get() {
35+
if (!orientationEventListener.canDetectOrientation()) {
36+
// Assume orientation has changed if we can't detect it
37+
return true
38+
}
39+
40+
val orientationHasChanged = _orientationHasChanged
41+
_orientationHasChanged = false
42+
return orientationHasChanged
43+
}
44+
45+
override fun close() {
46+
orientationEventListener.disable()
47+
}
48+
}

0 commit comments

Comments
 (0)