Skip to content
Advertisement

Using TextureView and MediaPlayer in Jetpack Compose

My source code was written in Java and now I want to convert to Compose

How can I convert the above code to Compose?

Java

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) {

    }
}


How can I convert the above code to Compose?

My source code was written in Java and now I want to convert to Compose

This is the way I use now, but it’s not working

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)

        }

    })

My source code was written in Java and now I want to convert to Compose

My source code was written in Java and now I want to convert to Compose

Advertisement

Answer

If you moved to use compose fully, then I think you can proceed like this: create you Custom view and call it in compose

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) {

}
}

and in compose

@Composable
fun CustomView(){
    val context = LocalContext.current

    AndroidView(factory = {
           VideoCrop(context).apply {
               layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
           }
      })
}
Advertisement