![](/img/trans.png)
[英]what is the difference between creating custom view and using jetpack compose in android
[英]Using TextureView and MediaPlayer in Jetpack Compose
我的源代碼是用 Java 編寫的,現在我想轉換為 Compose
如何將上述代碼轉換為 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) {
}
}
如何將上述代碼轉換為 Compose?
我的源代碼是用 Java 編寫的,現在我想轉換為 Compose
這是我現在使用的方式,但它不起作用
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 編寫的,現在我想轉換為 Compose
我的源代碼是用 Java 編寫的,現在我想轉換為 Compose
如果您開始完全使用 compose,那么我認為您可以這樣進行:創建自定義視圖並在 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) {
}
}
並在撰寫
@Composable
fun CustomView(){
val context = LocalContext.current
AndroidView(factory = {
VideoCrop(context).apply {
layoutParams = FrameLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT)
}
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.