说明

  • 默认的sdl播放文件会单独弹出一个窗口,如果嵌在qt的界面上需要修改代码如下

    1. screen = SDL_CreateWindowFrom((void *)label->winId());
    2. //screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
    3. // 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; }

  1. SDL_Window* screen;
  2. //SDL 2.0 Support for multiple windows
  3. screen = SDL_CreateWindowFrom((void *)label->winId());
  4. //screen = SDL_CreateWindow("Simplest Video Play SDL2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
  5. // screen_w, screen_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
  6. if (!screen) {
  7. printf("SDL: could not create window - exiting:%s\n", SDL_GetError());
  8. return;
  9. }
  10. SDL_Renderer* sdlRenderer = SDL_CreateRenderer(screen, -1, 0);
  11. Uint32 pixformat = 0;
  12. //IYUV: Y + U + V (3 planes)
  13. //YV12: Y + V + U (3 planes)
  14. pixformat = SDL_PIXELFORMAT_IYUV;
  15. SDL_Texture* sdlTexture1 = SDL_CreateTexture(sdlRenderer, pixformat, SDL_TEXTUREACCESS_STREAMING, pixel_w, pixel_h);
  16. FILE* fp = nullptr;
  17. fp = fopen("F:/qt-code/build-QtPlayerOne-Desktop_Qt_5_12_10_MinGW_64_bit-Debug/debug/sintel_640_360.yuv", "rb+");
  18. if (fp == nullptr)
  19. {
  20. std::cout << "fopen error" << std::endl;
  21. return;
  22. }
  23. if (fp == NULL) {
  24. printf("cannot open this file\n");
  25. return;
  26. }
  27. SDL_Rect sdlRect;
  28. while (1)
  29. {
  30. if (fread(buffer, 1, pixel_w * pixel_h * bpp / 8, fp) != pixel_w * pixel_h * bpp / 8) {
  31. // Loop
  32. fseek(fp, 0, SEEK_SET);
  33. fread(buffer, 1, pixel_w * pixel_h * bpp / 8, fp);
  34. }
  35. SDL_UpdateTexture(sdlTexture1, NULL, buffer, pixel_w);
  36. SDL_RenderClear(sdlRenderer);
  37. sdlRect.x = 0;
  38. sdlRect.y = 0;
  39. sdlRect.w = screen_w/2;
  40. sdlRect.h = screen_h/2;
  41. SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);
  42. sdlRect.x = screen_w / 2;
  43. sdlRect.y = 0;
  44. sdlRect.w = screen_w / 2;
  45. sdlRect.h = screen_h / 2;
  46. SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);
  47. sdlRect.x = 0;
  48. sdlRect.y = screen_h / 2;
  49. sdlRect.w = screen_w / 2;
  50. sdlRect.h = screen_h / 2;
  51. SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);
  52. sdlRect.x = screen_w / 2;
  53. sdlRect.y = screen_h / 2;
  54. sdlRect.w = screen_w / 2;
  55. sdlRect.h = screen_h / 2;
  56. SDL_RenderCopy(sdlRenderer, sdlTexture1, NULL, &sdlRect);
  57. SDL_RenderPresent(sdlRenderer);
  58. //Delay 40ms
  59. SDL_Delay(40);
  60. std::cout << "delay 40ms" << std::endl;
  61. }
  62. SDL_DestroyTexture(sdlTexture1);
  63. SDL_DestroyRenderer(sdlRenderer);
  64. SDL_DestroyWindow(screen);
  65. SDL_Quit();

} ```