From 80281e97403fa550e726981ca7c5071f0354a895 Mon Sep 17 00:00:00 2001
From: Dana <Dana_Lee1016@126.com>
Date: 星期二, 27 一月 2026 11:21:39 +0800
Subject: [PATCH] 1.设置数据源:setAudioSource() 和 setVideoSource()(必须在 setOutputFormat 之前)   设置输出格式:setOutputFormat()(必须在 setOutputFile 和编码器之前)   设置输出文件:setOutputFile()(必须在编码器之前)   设置编码器:setAudioEncoder() 和 setVideoEncoder()(必须在 setOutputFormat 之后)   设置编码参数:setAudioEncodingBitRate(), setAudioSamplingRate(), setVideoEncodingBitRate() 等(必须在编码器之后)   准备:prepare()

---
 app/src/main/java/com/safeluck/floatwindow/MainActivity.kt |   50 ++++++++++++++++++++++++++++----------------------
 1 files changed, 28 insertions(+), 22 deletions(-)

diff --git a/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt b/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
index 4d19696..b4c07f6 100644
--- a/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
+++ b/app/src/main/java/com/safeluck/floatwindow/MainActivity.kt
@@ -22,6 +22,9 @@
 
 class MainActivity : ComponentActivity() {
     private var mediaAidlInterface: IMediaAidlInterface? = null
+    private var serviceConnection: ServiceConnection? = null
+    private var isServiceBound = false
+    private val isServiceBoundState = mutableStateOf(false)
     
     private val callback = object : IMyCallback.Stub() {
         @Throws(RemoteException::class)
@@ -36,18 +39,18 @@
         super.onCreate(savedInstanceState)
         enableEdgeToEdge()
         
-        var isServiceBoundState by mutableStateOf(false)
-        
-        val serviceConnection = object : ServiceConnection {
+        serviceConnection = object : ServiceConnection {
             override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
                 mediaAidlInterface = IMediaAidlInterface.Stub.asInterface(service)
-                isServiceBoundState = true
+                isServiceBound = true
+                isServiceBoundState.value = true
                 Timber.d("FloatingService connected")
             }
             
             override fun onServiceDisconnected(name: ComponentName?) {
                 mediaAidlInterface = null
-                isServiceBoundState = false
+                isServiceBound = false
+                isServiceBoundState.value = false
                 Timber.d("FloatingService disconnected")
             }
         }
@@ -59,26 +62,16 @@
                     color = MaterialTheme.colorScheme.background
                 ) {
                     MainScreen(
-                        isServiceBound = isServiceBoundState,
+                        isServiceBound = isServiceBoundState.value,
                         onBindService = { 
-                            if (!isServiceBoundState) {
+                            if (!isServiceBoundState.value && serviceConnection != null) {
                                 val intent = Intent(this@MainActivity, FloatingService::class.java)
-                                bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)
+                                bindService(intent, serviceConnection!!, Context.BIND_AUTO_CREATE)
                                 Timber.d("Binding FloatingService")
                             }
                         },
                         onUnbindService = { 
-                            if (isServiceBoundState) {
-                                try {
-                                    mediaAidlInterface?.unregisterCallback(callback)
-                                } catch (e: RemoteException) {
-                                    Timber.e(e, "Error unregistering callback")
-                                }
-                                unbindService(serviceConnection)
-                                isServiceBoundState = false
-                                mediaAidlInterface = null
-                                Timber.d("Unbinding FloatingService")
-                            }
+                            unbindServiceInternal()
                         },
                         onStartAndroidRecord = { startAndroidRecord() },
                         onStopAndroidRecord = { stopAndroidRecord() },
@@ -210,16 +203,29 @@
         }
     }
     
-    override fun onDestroy() {
-        super.onDestroy()
-        if (mediaAidlInterface != null) {
+    private fun unbindServiceInternal() {
+        if (isServiceBound && serviceConnection != null) {
             try {
                 mediaAidlInterface?.unregisterCallback(callback)
             } catch (e: RemoteException) {
                 Timber.e(e, "Error unregistering callback")
             }
+            try {
+                unbindService(serviceConnection!!)
+                Timber.d("Unbinding FloatingService")
+            } catch (e: Exception) {
+                Timber.e(e, "Error unbinding service")
+            }
+            isServiceBound = false
+            isServiceBoundState.value = false
+            mediaAidlInterface = null
         }
     }
+    
+    override fun onDestroy() {
+        super.onDestroy()
+        unbindServiceInternal()
+    }
 }
 
 @Composable

--
Gitblit v1.8.0