2***@qq.com
2***@qq.com
  • 发布:2025-07-04 04:20
  • 更新:2025-07-04 04:20
  • 阅读:34

uts组件里面的new TextureViewListenerImpl((surface) => 获取不到surface

分类:uni-app x
newTextureView.setSurfaceTextureListener(  
                    new TextureViewListenerImpl((surface) => {  
                        // 当Surface可用时设置并通知  
                        this.surface = surface  
                        console.log("Surface已准备好")  

                    })  
                )

我这个surface一直为null
下面为完整代码 ,为什么那不到呢surface呢

<template>  
    <native-view ref="nativeViewRef" @init="onNativeViewInit"></native-view>  

</template>  

<script lang="uts">  
    import { TextureViewListenerImpl } from "@/uni_modules/tx-camera"  
    import FrameLayout from 'android.widget.FrameLayout'  
    import TextureView from "android.view.TextureView"  
    import Surface from 'android.view.Surface'  

    type SurfaceChangeCallback = (surface : Surface | null) => void  

    export default {  
        name: 'tx-camera',  
        props: {  
            cameraId: {  
                type: String,  
                default: "0"  
            }  
        },  
        data() {  
            return {  
                textureView: null as TextureView | null,  
                surfaceCallback: null as SurfaceChangeCallback | null,  
                surface: null as Surface | null,  
                isNativeViewReady: false,// 新增初始化状态标志  
                surfaceReadyCallback: null as (() => void) | null  
            }  
        },  
        methods: {  

            // NativeView初始化回调  
            onNativeViewInit(event : UniNativeViewInitEvent) {  
                const element = event.detail.element as UniNativeViewElement  
                const activity = element.getAndroidActivity()  
                console.log('AAAA', activity)  
                console.log('dddd', activity)  
                if (activity == null) return  
                console.log('CCCCC')  

                // 创建TextureView实例  
                const newTextureView = new TextureView(activity)  

                // 设置监听器(使用安全调用)  
                newTextureView.setSurfaceTextureListener(  
                    new TextureViewListenerImpl((surface) => {  
                        // 当Surface可用时设置并通知  
                        this.surface = surface  
                        console.log("Surface已准备好")  

                    })  
                )  

                console.log('surfacee', this.surface)  
                // 绑定原生视图  
                element.bindAndroidView(newTextureView)  

                // 设置布局参数  
                const layoutParams = new FrameLayout.LayoutParams(  
                    FrameLayout.LayoutParams.MATCH_PARENT,  
                    FrameLayout.LayoutParams.MATCH_PARENT  
                )  
                this.textureView?.setLayoutParams(layoutParams)  
                console.log('触发了吗', newTextureView)  

            },  

        },  

    }  
</script>
2025-07-04 04:20 负责人:无 分享
已邀请:

要回复问题请先登录注册