Very new to Android development ... actually trying to integrate an AR view in a Flutter app.
Been trying to set it up. Below is the ARActivity.kt and not sure why we keep getting this error
e: file:///Users/william/Documents/GitHub/game_native/android/app/src/main/kotlin/com/gps/app/ARActivity.kt:90:45 Unresolved reference: surfaceTexture
Maybe because the surfaceview is late and it's not declared when it's used?
private lateinit var surfaceView: SurfaceView
import android.content.Context
import android.app.Activity
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import android.view.View
import android.view.SurfaceView
import android.view.SurfaceHolder
import com.google.ar.core.Config
import com.google.ar.core.Session
import com.google.ar.core.ArCoreApk
import com.google.ar.core.exceptions.CameraNotAvailableException
class ARActivity : AppCompatActivity() {
private var arSession: Session? = null
private lateinit var surfaceView: SurfaceView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
// First, check if ARCore is available and installed
checkAndInstallARCore(this)
// Then, initialize and set up the SurfaceView
surfaceView = SurfaceView(this)
setContentView(surfaceView)
// Finally, set up the AR session
setupARSession()
}
private fun checkAndInstallARCore(activity: Activity) {
val availability = ArCoreApk.getInstance().checkAvailability(activity)
when (availability) {
ArCoreApk.Availability.SUPPORTED_INSTALLED -> {
println("ARCore is installed and supported")
}
ArCoreApk.Availability.SUPPORTED_NOT_INSTALLED -> {
println("ARCore is not installed but supported")
ArCoreApk.getInstance().requestInstall(activity, true)
}
ArCoreApk.Availability.UNSUPPORTED_DEVICE_NOT_CAPABLE -> {
println("This device does not support ARCore")
finish() // or show a message and close the activity
}
else -> {
println("ARCore availability check failed or unknown")
}
}
}
private fun setupARSession() {
try {
arSession = Session(this as Context)
val config = Config(arSession)
config.updateMode = Config.UpdateMode.LATEST_CAMERA_IMAGE
config.planeFindingMode = Config.PlaneFindingMode.HORIZONTAL
config.lightEstimationMode = Config.LightEstimationMode.ENVIRONMENTAL_HDR
arSession?.configure(config)
} catch (e: Exception) {
println("Error setting up AR session: ${e.message}")
finish()
return
}
surfaceView.holder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
try {
arSession?.resume()
checkAndSetTexture(holder)
} catch (e: CameraNotAvailableException) {
println("Camera not available on surfaceCreated: ${e.message}")
} catch (e: ClassCastException) {
println("Failed to cast holder to SurfaceHolder: ${e.message}")
}
}
private fun checkAndSetTexture(holder: SurfaceHolder) {
val surfaceTexture = holder.surfaceTexture
if (surfaceTexture.isNotEmpty) {
arSession?.setCameraTextureName(surfaceTexture.getTextureId())
println("Texture ID set: ${surfaceTexture.getTextureId()}")
} else {
println("SurfaceTexture not available, retrying in 100ms")
surfaceView.postDelayed({ checkAndSetTexture(holder) }, 100)
}
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
// This method can be used to adjust AR configuration if the surface size changes
}
override fun surfaceDestroyed(holder: SurfaceHolder) {
arSession?.pause()
}
})
}
// ... rest of your lifecycle methods ...
override fun onResume() {
super.onResume()
try {
arSession?.resume()
} catch (e: CameraNotAvailableException) {
println("Camera not available during onResume: ${e.message}")
}
}
override fun onPause() {
super.onPause()
arSession?.pause()
}
override fun onDestroy() {
super.onDestroy()
arSession?.close()
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745355645a4624099.html
评论列表(0条)