说明
默认的sdl播放文件会单独弹出一个窗口,如果嵌在qt的界面上需要修改代码如下
screen = SDL_CreateWindowFrom((void *)label->winId());//screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,// screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
代码
```cpp QtPlayerOne::QtPlayerOne(QWidget *parent) : QWidget(parent) , ui(new Ui::QtPlayerOne) { ui->setupUi(this); this->setFixedSize(661,465); label = new QLabel(this); label->resize(601,381); label->move(30,20); rect = label->geometry();//记录widget位置,恢复时使用 isplay = false;
start_btn = new QPushButton(“btn1” ,this); connect(start_btn,&QPushButton::clicked,this,&QtPlayerOne::start_play); }
QtPlayerOne::~QtPlayerOne() { delete start_btn; delete ui; }
const int bpp = 12; int screen_w = 640, screen_h = 360; const int pixel_w = 640, pixel_h = 360; unsigned char buffer[pixel_w pixel_h bpp / 8];
void QtPlayerOne::start_play() { if (SDL_Init(SDL_INIT_VIDEO)) { qDebug() << “Could not initialize SDL”; return; }
SDL_Window* screen;//SDL 2.0 Support for multiple windowsscreen = SDL_CreateWindowFrom((void *)label->winId());//screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,// screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);if (!screen) {printf("SDL: could not create window - exiting:%s\n", SDL_GetError());return;}SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);Uint32 pixformat = 0;//IYUV: Y + U + V (3 planes)//YV12: Y + V + U (3 planes)pixformat = SDL_PIXELFORMAT_IYUV;SDL_Texture* sdlTexture1 = SDL_CreateTexture(sdlRenderer, pixformat, SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);FILE* fp = nullptr;fp = fopen("F:/qt-code/build-QtPlayerOne-Desktop_Qt_5_12_10_MinGW_64_bit-Debug/debug/sintel_640_360.yuv", "rb+");if (fp == nullptr){std::cout << "fopen error" << std::endl;return;}if (fp == NULL) {printf("cannot open this file\n");return;}SDL_Rect sdlRect;while (1){if (fread(buffer, 1, pixel_w * pixel_h * bpp / 8, fp) != pixel_w * pixel_h * bpp / 8) {// Loopfseek(fp, 0, SEEK_SET);fread(buffer, 1, pixel_w * pixel_h * bpp / 8, fp);}SDL_UpdateTexture(sdlTexture1, NULL, buffer, pixel_w);SDL_RenderClear(sdlRenderer);sdlRect.x = 0;sdlRect.y = 0;sdlRect.w = screen_w/2;sdlRect.h = screen_h/2;SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);sdlRect.x = screen_w / 2;sdlRect.y = 0;sdlRect.w = screen_w / 2;sdlRect.h = screen_h / 2;SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);sdlRect.x = 0;sdlRect.y = screen_h / 2;sdlRect.w = screen_w / 2;sdlRect.h = screen_h / 2;SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);sdlRect.x = screen_w / 2;sdlRect.y = screen_h / 2;sdlRect.w = screen_w / 2;sdlRect.h = screen_h / 2;SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);SDL_RenderPresent(sdlRenderer);//Delay 40msSDL_Delay(40);std::cout << "delay 40ms" << std::endl;}SDL_DestroyTexture(sdlTexture1);SDL_DestroyRenderer(sdlRenderer);SDL_DestroyWindow(screen);SDL_Quit();
} ```
