VideoView中的onSurfaceTextureAvailable永远不会被调用...
我有持有textureview的此 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是在片段的xml中设置的。
如果我将VideoView放置在片段内,则一切正常。
但是,如果它位于片段的mymediaplayer中,则永远不会调用onSurfaceTextureAvailable。
最佳答案
如果onSurfaceTextureAvailable
已经可用,似乎没有调用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());
}