最近自己用QT搞一个监控平台的程序UI,然后后台一些视频处理是使用FFMPEG和SDL来做的。
在关闭播放窗口SDL_DestroyWindow后,再次去显示视频就会发现显示不出来。代码如下
int SDLPlayer::SDLPlay()
{SDL_Window *sdlScreen{ NULL };//窗口SDL_Renderer* sdlRenderer{ NULL };//渲染器SDL_Texture* sdlTexture{ NULL };//纹理SDL_Init(SDL_INIT_VIDEO);sdlScreen = SDL_CreateWindowFrom(m_handle);//sdlScreen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,//1024, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);if (sdlScreen == 0){printf("SDL: could not create SDL_Window - exiting:%s\n", SDL_GetError());return -1;}sdlRenderer = SDL_CreateRenderer(sdlScreen, -1, SDL_RENDERER_ACCELERATED);if (sdlRenderer == NULL){printf("SDL: could not create SDL_Renderer - exiting:%s\n", SDL_GetError());return -1;}sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, 1920, 1080);if (sdlTexture == NULL){printf("SDL: could not create SDL_Texture - exiting:%s\n", SDL_GetError());return -1;}m_bInitWindow = true;AVFrame* yuvframe = av_frame_alloc();while (getPlayFlag()){Sleep(40);if (GetDisplayFlag() && m_bInitWindow){GetYUVFrame(yuvframe);if (yuvframe == NULL || yuvframe->data[0] == NULL){continue;}SDL_UpdateYUVTexture(sdlTexture, NULL,yuvframe->data[0], yuvframe->linesize[0],yuvframe->data[1], yuvframe->linesize[1],yuvframe->data[2], yuvframe->linesize[2]);SDL_RenderClear(sdlRenderer);SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);SDL_RenderPresent(sdlRenderer);}}SDL_DestroyWindow(sdlScreen);SDL_DestroyTexture(sdlTexture);SDL_Quit();//ShowWindow(m_handle, true);return 0;
}
也就是第二次调用时发现视频出不来了,但是程序什么的都是正常运行的。细心的就会发现界面上的窗口都不见了。
SDL_DestroyWindow(sdlScreen);
这一句代码会将窗口隐藏起来,所以我们在最后再将窗口显示出来就好了。
ShowWindow(m_handle, true);
加 ShowWindow不让窗口隐藏。