Skip to content

Commit

Permalink
start / stop stream in lifecycle FoKE-Developers#9
Browse files Browse the repository at this point in the history
  • Loading branch information
ing03201 committed Oct 5, 2024
1 parent 38c6fe8 commit 372fb12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
Expand All @@ -35,18 +36,7 @@ fun CameraScreen(
viewModel: CameraViewModel = hiltViewModel()
) {
val TAG = "CameraScreen"
LifecycleEventEffect(Lifecycle.Event.ON_START) {
viewModel.setCaptureTimer { navigateToShare() }
AppLog.d(TAG, "ON_START")
}
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
AppLog.d(TAG, "ON_RESUME")
viewModel.startCaptureTimer()
}
LifecycleEventEffect(Lifecycle.Event.ON_STOP) {
viewModel.stopCaptureTimer()
AppLog.d(TAG, "ON_STOP")
}
var mjpegView: MjpegView? = null
ConstraintLayout(
modifier = Modifier.fillMaxSize()
) {
Expand Down Expand Up @@ -103,6 +93,7 @@ fun CameraScreen(
.aspectRatio(1.5f),
factory = { context ->
MjpegView(context).apply {
mjpegView = this
mode = MjpegView.MODE_BEST_FIT
isAdjustHeight = true
supportPinchZoomAndPan = false
Expand Down Expand Up @@ -135,7 +126,6 @@ fun CameraScreen(
// test url
// TODO : change url in viewmodel
setUrl("http://10.32.100.37:5000/preview")
startStream()
}
},
)
Expand All @@ -153,6 +143,21 @@ fun CameraScreen(
fontSize = 24.sp,
)
}
LifecycleEventEffect(Lifecycle.Event.ON_START) {
viewModel.setCaptureTimer { navigateToShare() }
AppLog.d(TAG, "ON_START")
}
LifecycleEventEffect(Lifecycle.Event.ON_RESUME) {
AppLog.d(TAG, "ON_RESUME")
viewModel.startCaptureTimer()
mjpegView?.startStream()
}
LifecycleEventEffect(Lifecycle.Event.ON_STOP) {
viewModel.stopCaptureTimer()
AppLog.d(TAG, "ON_STOP")
mjpegView?.stopStream()
}
LocalContext
}

@Preview(showBackground = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,16 @@ class CameraViewModel @Inject constructor(
captureTimer = object : CountDownTimer(5000, 10) {
override fun onTick(millisUntilFinished: Long) {
_progressState.floatValue = 1f - (millisUntilFinished.toFloat() / 5000)
AppLog.d("CameraViewModel", "onTick: ${_progressState.floatValue}")
}
override fun onFinish() {
_progressState.floatValue = 1f
if(_captureCount.intValue < 4){
_captureCount.intValue += 1
startCaptureTimer()
AppLog.d("CameraViewModel", "onFinish: ${_captureCount.intValue}")
}
else {
stopCaptureTimer()
_captureCount.intValue = 1
AppLog.d("CameraViewModel", "onFinish: ${_captureCount.intValue}")
nextNavigate()
}
}
Expand Down

0 comments on commit 372fb12

Please sign in to comment.