onSurfaceTextureAvailable

onSurfaceTextureAvailable

本文介绍了textureview onSurfaceTextureAvailable从来没有所谓的内部RelativeLayout的片段内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onSurfaceTextureAvailable在VideoView不会被调用...
我有这样的观点,即持有的textureview。

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设置是这样的:

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是设置在一个片段的XML。
如果我把VideoView内的片段,然后一切工作正常。
但如果是在mymediaplayer片段内onSurfaceTextureAvailable永远不会被调用。

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.

推荐答案

onSurfaceTextureAvailable 似乎并没有被调用,如果表面纹理已经可用。您可以检查它是否可用,并打电话给你的 onSurfaceTextureAvailable 方法,像下面。

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

这篇关于textureview onSurfaceTextureAvailable从来没有所谓的内部RelativeLayout的片段内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 01:24