gpt4 book ai didi

android - textureview onSurfaceTextureAvailable 从未在 fragment 内的 relativelayout 内调用

转载 作者:IT老高 更新时间:2023-10-28 23:08:57 30 4
gpt4 key购买 nike

永远不会调用 VideoView 中的 onSurfaceTextureAvailable...
我有这个持有纹理 View 的 View 。

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 是在 fragment 的 xml 中设置的。
如果我将 VideoView 放在 fragment 中,那么一切正常。
但是如果它在 mymediaplayer 里面的 Fragment onSurfaceTextureAvailable 永远不会被调用。

最佳答案

如果 SurfaceTexture 已经可用,则

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

textureView.setSurfaceTextureListener(this);

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

关于android - textureview onSurfaceTextureAvailable 从未在 fragment 内的 relativelayout 内调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693194/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com