24

onSurfaceTextureAvailable in VideoView is never called...
I have this view that is holding the textureview.

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 is set up like this:

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 is setup in a xml of a fragment.
If i place the VideoView inside the fragment then everything works fine.
But if it is in mymediaplayer inside the fragment onSurfaceTextureAvailable is never called.

1
  • 2
    is it working now? could you share sample solution or example project?
    – keybee
    Jun 5, 2015 at 10:04

7 Answers 7

27

onSurfaceTextureAvailable doesn't seem to get called if the SurfaceTexture is already available. You can check if it is available and call your onSurfaceTextureAvailable method like below.

textureView.setSurfaceTextureListener(this);

/*
 * onSurfaceTextureAvailable does not get called if it is already available.
 */
if (view.isAvailable()) {
    onSurfaceTextureAvailable(view.getSurfaceTexture(), view.getWidth(), view.getHeight());
}
4
  • But how could the surface be available and onSurfaceTextureAvailable not being called? I had a look into the TextureView source code, and I can't see that to be possible. May 10, 2014 at 12:33
  • 3
    If you call setSurfaceTextureListener after the surface is available, you won't get the onSurfaceTextureAvailable callback. May 19, 2014 at 15:12
  • 8
    what is view in your context?
    – Danpe
    Jun 28, 2014 at 16:04
  • 6
    I think view is supposed to be the same as textureView on the first line.
    – yuzeh
    Jun 19, 2015 at 20:31
20

As for me ,the key to solve the problem is that add android:hardwareAccelerated="true" for the activity in the AndroidManifest.xml .

You can use TextureView in a Scrolling-Container(Such as ListView,ScrollView,ViewPager etc) only if the activity for the TextureView with hardwareAccelerated property on.

The last few words in this blog mentioned that

Hope it works for you.

1
  • 4
    My issue was fixed by manually calling onSurfaceTextureAvailable. I don't think setting hardwareAccelerated="true" will help many new projects, since hardware acceleration is enabled by default for Target API level >= 14.
    – VinceFior
    Feb 10, 2016 at 6:35
12

In my case I initially had my TextureView invisible and then loaded after fetching some data and this was causing onSurfaceTextureAvailable not to be called. I am guessing that for TextureView to be said to be 'available', it must be visible. My solution was to just make the TextureView visible from the start.

0
12

You need to enable hardware acceleration to use TextureView. If you don't, its callback methods will not be called. Just pout that in your activity's onCreate method:

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

It worked for me.

1
  • Years later, I come across with the same problem again, and your answer saved my life again :)
    – Piasy
    Jul 10, 2019 at 14:39
4

I have met the same issue, onSurfaceTextureAvailable never called. Finally I found my textureview is invisible before setSurfaceTextureListener. So check status of textureview at first.

1
  • 1
    Welcome to StackOverflow! Next time, try posting advice like this in the comments section Jul 13, 2016 at 11:27
3

i had the same problem , strangely resolved it with the following code

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

More strangely if i set only the width or the height that dosent work either.

3
  • But what is the reason of the solution? To place a random size? May 7, 2014 at 9:53
  • i really don't know .. i was experimenting and that's what worked out for me.
    – Rany Ishak
    May 8, 2014 at 7:17
  • 4
    This solution worked for me, but the situation was due to a bigger problem: the view was not laying itself out properly. The parent layout view (a custom ViewGroup) was not calling View.measure(...) correctly on the texture view's parent. I just had to fix up my ViewGroup control. Nov 14, 2014 at 1:17
0

try to put mTextureView.setSurfaceTextureListener() in onCreate().

RootCause is that: onCreate() -> drawUI -> getHardwareLayer() -> callback SurfaceTextureListener. So to fix this issue: setSurfaceTextureListener() before UI draw or redraw the UI.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.