TextureView onSurfaceTextureAvailable回调不执行

TextureView必须工作在硬件加速条件, 否则什么都不执行.

因为需要 android:hardwareAccelerated=”true”或者 

Window w = activity.getWindow(); w.setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

如果是在Service里面,直接在AndroidManifest设置会不起作用,只能直接对Window设置flag
WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED

 

看官方文档

A TextureView can be used to display a content stream. Such a content stream can for instance be a video or an OpenGL scene. The content stream can come from the application's process as well as a remote process.
TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing.
Unlike SurfaceView , TextureView does not create a separate window but behaves as a regular View. This key difference allows a TextureView to be moved, transformed, animated, etc. For instance, you can make a TextureView semi-translucent by calling myView.setAlpha(0.5f).
Using a TextureView is simple: all you need to do is get its SurfaceTexture . The SurfaceTexture  can then be used to render content. The following example demonstrates how to render the camera preview into a TextureView:
  public class LiveCameraActivity extends Activity implements TextureView.SurfaceTextureListener {
      private Camera mCamera;
      private TextureView mTextureView;

      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);

          mTextureView = new TextureView(this);
          mTextureView.setSurfaceTextureListener(this);

          setContentView(mTextureView);
      }

      public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
          mCamera = Camera.open();

          try {
              mCamera.setPreviewTexture(surface);
              mCamera.startPreview();
          } catch (IOException ioe) {
              // Something bad happened
          }
      }

      public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
          // Ignored, Camera does all the work for us
      }

      public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
          mCamera.stopPreview();
          mCamera.release();
          return true;
      }

      public void onSurfaceTextureUpdated(SurfaceTexture surface) {
          // Invoked every time there's a new Camera preview frame
      }
  }
 
A TextureView's SurfaceTexture can be obtained either by invoking getSurfaceTexture()  or by using a TextureView.SurfaceTextureListener . It is important to know that a SurfaceTexture is available only after the TextureView is attached to a window (and onAttachedToWindow()  has been invoked.) It is therefore highly recommended you use a listener to be notified when the SurfaceTexture becomes available.
It is important to note that only one producer can use the TextureView. For instance, if you use a TextureView to display the camera preview, you cannot use lockCanvas()  to draw onto the TextureView at the same time.

 

 

以下转载自: https://blog.csdn.net/u010775683/article/details/78364141 

几个注意事项:

1、TextureView是Android 4.0之后加入的,低版本么这个类。TextureView必须工作在开启硬件加速的环境中,也即配置文件里Activity的设置项里:android:hardwareAccelerated="true" 默认的这个属性就是true,因此不用再写了。但如果写成false,可以看到onSurfaceTextureAvailable()这个回调就进不来了;

        2、有两点跟Surfaceview不同。第一,TextureView创建过程中没有进到onSurfaceTextureSizeChanged()这个函数里。而SurfaceView在创建过程中,从无到有的时候会进到大小发生变化回调里。第二,onSurfaceTextureUpdated()这个函数每上来一帧数据,这块就进来一次。这是跟Surfaceview相比,最伟大的一个地方。通过这个接口,可以将上来的SurfaceTexture送给OpenGL再去处理。这个回调是实时的,而非用Camera的PreviewCallback这种2次回调的方式。从时间看,基本上每32ms左右上来一帧数据,即每秒30帧,跟本手机的Camera的性能吻合。

        3、Camera再执行startPreview时必须保证TextureView的SurfaceTexture上来了,如果因为一些性能原因onSurfaceTextureAvailable()这个回调上不来就开预览,就开不了的。如果发生这种情况,就在onSurfaceTextureAvailable()回调里执行open和startPreview操作,保证万无一失。     


        4、SurfaceTexture和TextureView的关系:

如果说TextureView是一幅画的话,那SurfaceTexture就是画布,真正渲染的载体是SurfaceTexture。

        5、TextureView可以像一般View执行各种变化,其中有个textureView.setAlpha(1.0f);默认不写这句话,它的alpha也是1.0f,即不透明。如果设成透明0.0f,可以看到啥都看不到了,这一点跟Surfaceview刚好相反。Surfaceview的SurfaceHolder一般要设一下Transparent即透明。但TextureView因为是个view,任何一个png的照片透明度设成0肯定啥都看不到。

        6、如果认为预览个Camera这就是TextureView和SurfaceTexture的使命的话,就大错特错了,真正用意是和OpenGL无缝连接。
 

参考:

https://blog.csdn.net/WeLoveSunFlower/article/details/53423992

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
TextureViewsurfaceTextureAvailable 回调方法中,我们可以获取到一个 SurfaceTexture 对象,该对象可以将视频帧渲染到 TextureView 上。我们可以在这个回调方法中创建 MediaPlayer 对象,并将其与 SurfaceTexture 对象相关联,以便进行视频播放。 以下是一个示例代码,演示了如何在 TextureViewsurfaceTextureAvailable 回调方法中创建 MediaPlayer 对象并启动视频播放: ```java @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { // 创建 MediaPlayer 对象 MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setSurface(new Surface(surface)); try { // 设置要播放的视频文件路径 mediaPlayer.setDataSource("path/to/video/file"); mediaPlayer.prepareAsync(); // 异步准备 MediaPlayer mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { // 准备完成后开始播放视频 mediaPlayer.start(); } }); } catch (IOException e) { e.printStackTrace(); } } ``` 在这个示例代码中,我们首先创建了一个 MediaPlayer 对象,并通过 mediaPlayer.setSurface() 方法将其与 Surface 对象相关联。接着,我们设置了要播放的视频文件路径,并通过 mediaPlayer.prepareAsync() 方法异步准备 MediaPlayer 对象。在 MediaPlayer 准备完成后,我们通过 mediaPlayer.start() 方法开始播放视频。 需要注意的是,在 TextureView 销毁时,我们应该及时释放 MediaPlayer 对象,以避免内存泄漏。可以在 TextureViewsurfaceTextureDestroyed 回调方法中释放 MediaPlayer 对象,如下所示: ```java @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { mediaPlayer.release(); // 释放 MediaPlayer 对象 return true; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值