首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Jetpack组合中使用TextureView和MediaPlayer

在Jetpack组合中使用TextureView和MediaPlayer
EN

Stack Overflow用户
提问于 2022-07-23 13:05:17
回答 1查看 185关注 0票数 2

我的源代码是用Java编写的,现在我想转换成编写

我如何转换上面的代码来组成?

Java

代码语言:javascript
运行
复制
class VideoCropActivity extends Activity implements TextureView.SurfaceTextureListener {

    private MediaPlayer mPlayer;
    private Surface mSurface;
    private TextureView textureview;
    
    public void initSurface() {
        textureview = (TextureView) findViewById(R.id.aliyun_video_textureview);
        textureview.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

        if (mPlayer == null) {
            try {
                mSurface = new Surface(surface);
                mPlayer = new MediaPlayer();
                mPlayer.setSurface(mSurface);
            } catch (Exception e) {
                e.printStackTrace();
            }
            }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

    }
}

我如何转换上面的代码来组成?

我的源代码是用Java编写的,现在我想转换成编写

,这是我现在使用的方式,但它不能工作

代码语言:javascript
运行
复制
AndroidView(factory = {
        val view = LayoutInflater.from(it).inflate(R.layout.aliyun_video_player, null)
        val textureView = view.findViewById<TextureView>(R.id.videoTextureView)
        textureView.apply {

            object : TextureView.SurfaceTextureListener {
                override fun onSurfaceTextureAvailable(p0: SurfaceTexture, p1: Int, p2: Int) {
                    // TODO("Not yet implemented")
                    println("=================")
                }

                override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture, p1: Int, p2: Int) {
                    TODO("Not yet implemented")
                }

                override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {
                    TODO("Not yet implemented")
                }

                override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {
                    TODO("Not yet implemented")
                }
            }
        }
    }, update = {

        val s = it.surfaceTexture
        if (s != null) {
            val surface = android.view.Surface(it.surfaceTexture)
            val mediaPlayer = MediaPlayer()
            mediaPlayer.setSurface(surface)
            println(it)

        }

    })

我的源代码是用Java编写的,现在我想转换成编写

我的源代码是用Java编写的,现在我想转换成编写

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-08 22:36:32

如果您移动到完全使用撰写,那么我认为您可以这样做:创建您的自定义视图并在撰写中调用它

代码语言:javascript
运行
复制
class VideoCrop extends TextureView implements TextureView.SurfaceTextureListener {

private MediaPlayer mPlayer;
private Surface mSurface;
private TextureView textureview;

public VideoCrop(Context context) {
   super(context);
   setSurfaceTextureListener(this);
   setOpaque(false);
}

@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

    if (mPlayer == null) {
        try {
            mSurface = new Surface(surface);
            mPlayer = new MediaPlayer();
            mPlayer.setSurface(mSurface);
        } catch (Exception e) {
            e.printStackTrace();
        }
        }
}

@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

}

@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    return false;
}

@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {

}
}

在写作中

代码语言:javascript
运行
复制
@Composable
fun CustomView(){
    val context = LocalContext.current

    AndroidView(factory = {
           VideoCrop(context).apply {
               layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
           }
      })
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73090997

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档