Android Media3 PlayerView 监听 SurfaceTextureListener

Android Media3 PlayerView 监听 SurfaceTextureListener

  • 由于 PlayerView 内部已经设置过 SurfaceTextureListener 的监听了,不能直接设置
java 复制代码
//androidx.media3.ui.PlayerView 设置 app:surface_type="texture_view"
View videoSurfaceView = playerView.getVideoSurfaceView();
if (videoSurfaceView == null) {
    return;
}
if (videoSurfaceView instanceof TextureView) {
  TextureView textureView = (TextureView) videoSurfaceView;
  //先 textureView.getSurfaceTextureListener() 得到系统已设置的监听
  textureView.setSurfaceTextureListener(new SurfaceTextureProxy(textureView.getSurfaceTextureListener(), new SurfaceTextureProxy.Listener() {
      @Override
      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
          Log.e(TAG, "onSurfaceTextureAvailable: test");
      }
  }));
}
java 复制代码
public class SurfaceTextureProxy implements TextureView.SurfaceTextureListener {

    //源对象
    private TextureView.SurfaceTextureListener originListener;
    private Listener listener;

    public SurfaceTextureProxy(TextureView.SurfaceTextureListener originListener, Listener listener) {
        this.originListener = originListener;
        this.listener = listener;
    }

    @Override
    public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
        originListener.onSurfaceTextureAvailable(surface, width, height);
        Log.e("TAG", "onSurfaceTextureAvailable: zfq0221" + originListener);
        //
        listener.onSurfaceTextureAvailable(surface, width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {
        originListener.onSurfaceTextureSizeChanged(surface, width, height);
        //
        listener.onSurfaceTextureSizeChanged(surface, width, height);
    }

    @Override
    public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
        boolean result = originListener.onSurfaceTextureDestroyed(surface);
        //
        listener.onSurfaceTextureDestroyed(surface);
        return result;
    }

    @Override
    public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {
        originListener.onSurfaceTextureUpdated(surface);
        //
        listener.onSurfaceTextureUpdated(surface);
    }


    public interface Listener {

        default void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        }

        default void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        }

        default void onSurfaceTextureDestroyed(SurfaceTexture surface) {
        }

        default void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    }
}
相关推荐
朱涛的自习室6 小时前
我给 Gemini 4 和 DeepSeek 搞了套「末位淘汰」,AI 干活会更猛吗?(Day 3)
android·前端·后端
应用市场12 小时前
离线小游戏做“每日挑战”:日期当随机种子,整局存成 JSON,再加一个“我不是机器人”
android·kotlin·游戏开发
一铭1119913 小时前
海豹办公 1.9.15 跨端办公软件实践 文档兼容 Access 数据库与移动 WebView 排错
android·数据库·electron·开源软件·access
geats人山人海13 小时前
java 7异常
android·java·开发语言
2601_9620710014 小时前
Kimi K3 vs DeepSeek V4-Pro实测对比:2026年同一入口十道题,耗时与token消耗差在哪
android·java·数据库
右手吉他15 小时前
高通平台IA APN问题分析之二
android·物联网
夜雪一千16 小时前
MySQL 自增 ID 的底层原理与坑点
android·mysql·adb
不浪费一个字节16 小时前
一个纯 Python 实现的 APK 解析与设备操控工具箱
android
itmigrate16 小时前
PHP 导出大 CSV 时内存溢出的一次排查
android·开发语言·php
hai_android16 小时前
LiveEventBusCore 跨 Activity 通讯源码解析
android·kotlin·android jetpack