textureView onSurfaceTextureAvailable从来没有在insidelay里面调用relativelayout

VideoView中的onSurfaceTextureAvailable从来没有被称为…
我有这个持有纹理视图的视图。

public class MyMediaPlayer extends RelativeLayout{ Context mContext; VideoView player;//this is a custom textureview that streams video public MyMediaPlayer(Context context) { super(context); mContext = context; init(); } public MyMediaPlayer(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; init(); } public MyMediaPlayer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } private void init() { setBackgroundColor(Color.BLACK); player = new VideoView(mContext); LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); addView(player,lp); } public void setURL(String url){ player.setVideoPath(url); } public void start(){ player.start(); } } 

VideoView的设置是这样的:

 public class VideoView extends TextureView { public VideoView(final Context context) { super(context); mContext = context; initVideoView(); } public VideoView(final Context context, final AttributeSet attrs) { super(context, attrs); mContext = context; initVideoView(); } public VideoView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; initVideoView(); } public void initVideoView() { Log.d(LOG_TAG, "Initializing video view."); videoHeight = 0; videoWidth = 0; setFocusable(false); setSurfaceTextureListener(surfaceTextureListener); } SurfaceTextureListener surfaceTextureListener = new SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) { Log.d(LOG_TAG, "Surface texture now avaialble."); surfaceTexture = surface; openVideo(); } @Override public void onSurfaceTextureSizeChanged(final SurfaceTexture surface, final int width, final int height) { Log.d(LOG_TAG, "Resized surface texture: " + width + '/' + height); surfaceWidth = width; surfaceHeight = height; boolean isValidState = (targetState == STATE_PLAYING); boolean hasValidSize = (videoWidth == width && videoHeight == height); if (mediaPlayer != null && isValidState && hasValidSize) { start(); } } @Override public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) { Log.i(LOG_TAG, "Destroyed surface number " + number); return false; } @Override public void onSurfaceTextureUpdated(final SurfaceTexture surface) { } }; } 

MyMediaPlayer安装在一个xml的片段中。
如果我把VideoView里面的片段,然后一切正常。
但如果是在mymediaplayer里面的片段onSurfaceTextureAvailable从来没有被调用。

Solutions Collecting From Web of "textureView onSurfaceTextureAvailable从来没有在insidelay里面调用relativelayout"

如果SurfaceTexture已经可用, SurfaceTexture似乎不会被调用。 你可以检查它是否可用,并调用你的onSurfaceTextureAvailable方法,如下所示。

 textureView.setSurfaceTextureListener(this); /* * onSurfaceTextureAvailable does not get called if it is already available. */ if (view.isAvailable()) { onSurfaceTextureAvailable(view.getSurfaceTexture(), view.getWidth(), view.getHeight()); } 

至于我,解决问题的关键是在AndroidManifest.xml中添加android:hardwareAccelerated =“true”。

只有当TextureView的hardwareAccelerated属性开启时,才能在Scrolling-Container(如ListView,ScrollView,ViewPager等)中使用TextureView。

这个博客的最后几个字提到

希望对你有效。

在我的情况下,我最初有我的TextureView不可见,然后加载后提取一些数据,这导致onSurfaceTextureAvailable不被调用。 我猜测, TextureView被认为是“可用的”,它必须是可见的。 我的解决scheme是从一开始就使TextureView可见。

我有同样的问题,奇怪的解决它与下面的代码

 videoPreview = (TextureView) linearLayout.findViewById(R.id.videoView); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)videoPreview.getLayoutParams(); params.height = 130; params.width = 508; videoPreview.setLayoutParams(params); videoPreview.setSurfaceTextureListener(this); 

更奇怪的是,如果我只设置宽度或高度dosent工作。

您需要启用硬件加速才能使用TextureView。 如果你不这样做,它的callback方法将不会被调用。 只要p嘴在你的活动的onCreate方法:

 getWindow().addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); 

它为我工作。

我遇到了同样的问题, onSurfaceTextureAvailable从来没有打电话。 最后我发现我的textureviewsetSurfaceTextureListener之前是不可见的。 所以首先检查一下textureview状态。