Simple video player in QT playing audio only - qmainwindow

I written a program that is supposed to show video as well as audio using QT. However I cannot work out why the video is not playing, while the audio works fine.
vidpb.pro
QT += core gui multimedia multimediawidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = vidpb
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
CONFIG += mobility
MOBILITY = multimedia
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QMediaPlayer;
class QVideoWidget;
class MainWindow : public QMainWindow
{
Q_OBJECT
QMediaPlayer *player;
QVideoWidget *widget;
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMediaPlayer>
#include <QVideoWidget>
#include <QtMultimedia>
#include <qvideowidget.h>
#include "qmediaplayer.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
player = new QMediaPlayer(this);
player->setMedia(QUrl::fromLocalFile("/sdcard/Movies/video1.mp4"));
widget = new QVideoWidget(this);
player->setVideoOutput(widget);
widget->show();
setCentralWidget(widget);
this->show();
player->play();
}
MainWindow::~MainWindow()
{
delete ui;
}

Related

Qt app (with while loop) still running after closing MainWindow

I have this sample code that reproduces the problem I am dealing with.
//main.cpp
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
widget w;
w.show();
return a.exec();
}
--
//widget.h
#pragma once
#include <QMainWindow>
#include <QPushButton>
#include <QApplication>
#include <QLayout>
class widget : public QMainWindow
{
Q_OBJECT
public:
QApplication* application;
widget(QWidget *parent = nullptr);
QVBoxLayout *mainLayout;
QPushButton *startButton;
QWidget *centralWidget;
~widget();
public slots:
void onStartButtonPressed();
private:
void start();
bool loop;
};
--
//widget.cpp
#include "widget.h"
widget::widget(QWidget *parent)
: QMainWindow(parent)
{
application = static_cast<QApplication*>(QApplication::instance());
mainLayout = new QVBoxLayout();
loop = false;
startButton = new QPushButton("START");
mainLayout->addWidget(startButton);
connect(startButton, SIGNAL(clicked()),this,SLOT(onStartButtonPressed()));
centralWidget = new QWidget();
centralWidget->setLayout(mainLayout);
this->setCentralWidget(centralWidget);
this->showMaximized();
this->setWindowTitle("Widget");
}
void widget::onStartButtonPressed()
{
loop = !loop;
startButton->setText(loop? "STOP" : "START");
start();
}
void widget::start()
{
while(loop)
{
application->processEvents();
}
}
widget::~widget()
{
loop = false;
}
This is a simple Qt application with a QPushButton that when pressed set a boolean flag to true and starts a loop. When the main widget is closed the destructor sets boolean flag to false and the loop should break.
However when loop is running the destructor seems not been called, while when loop is not running it is called normally and the app quit accordingly.
Of course I call application->processEvents() to prevent freezing, but I don't know why the destructor is not called at loop running.

ESP8266Audio with multiple .wav file encoded in hex

I'm working on a script that will play multiple.wav encoded files in the ESP8266 using the ESP8266Audio library. I need to play multiple audio streams with different time stamps; that's why I am asking for it. Please do note that the hardware is ESP32-8266 and TIP 120 transistor.
#include <Arduino.h>
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2SNoDAC.h"
#include <ESP8266WiFi.h>
#include <SPI.h>
#include <SD.h>
// VIOLA sample taken from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html
#include "viola.h"
#include "viola02.h"
AudioGeneratorWAV *wav;
AudioFileSourcePROGMEM *file;
AudioOutputI2SNoDAC *out;
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.printf("WAV start\n");
audioLogger = &Serial;
out = new AudioOutputI2SNoDAC();
// out02 = new AudioOutputI2SNoDAC();
// out -> SetGain(0.08);
}
void Play(const void *data, uint32_t len) {
file = new AudioFileSourcePROGMEM( data, len );
wav = new AudioGeneratorWAV();
wav->begin(file, out);
delay(3000);
while (true) {
if (wav->isRunning()) {
if (!wav->loop()) wav->stop();
} else {
Serial.printf("WAV done\n");
delay(1000);
break;
}
}
}
void loop()
{
Play(viola,sizeof(viola));
Play(viola02,sizeof(viola02));
}
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/yIKml.png

How Use QTimer Command

I have a problem in using QTimer command.
I dont have any syntax error, but i have 2 error in qglobal.h and qobjectdefs_impl.h and i dont understand them.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
void MainWindow::updatewindow()
{
Mat frame;
capture >> frame;
cvtColor(frame, frame, cv::COLOR_BGR2RGB);
QImage image((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
QPixmap temp_img = QPixmap::fromImage(image);
ui->label2->setPixmap(temp_img);
}
void MainWindow::on_pushload_clicked()
{
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, SLOT(updatewindow()));
timer->start(20);
}
I have a problem in using QTimer command.
I dont have any syntax error, but i have 2 error in qglobal.h and qobjectdefs_impl.h and i dont understand them.
and mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QPixmap>
#include <QTimer>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
QPixmap Iblackwhite,IMG_Color{};
QImage image {};
cv::VideoCapture capture{};
private slots:
void updatewindow();
void on_pushload_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
please help me to solve my problem.
Use &MainWindow::updateWindow instead of SLOT(updatewindow()).

Webcam doesn't work with openCV 1.0 C (CodeBlocks)

I got a problem, I want a window with what my webcam displays; but what I have is a windows with ... a black
I tried to got image dimensions, but it's a 0x0
My webcam is good, It works with displaying of Windows!!
this's ma code
(Windows 7 x64 bits)
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main()
{
IplImage* img;
CvCapture* capture = cvCaptureFromCAM (CV_CAP_ANY);
if (!capture)
return 10;
cvNamedWindow("video", CV_WINDOW_AUTOSIZE);
char key = 'a';
if (!cvGrabFrame(capture))
return 20;
while (key != 'q'){
img = cvRetrieveFrame(capture);
cvShowImage("video", img);
key = cvWaitKey(60);
if (!cvGrabFrame(capture))
key = 'q';
}
cvDestroyAllWindows();
img = NULL;
cvReleaseCapture(&capture);
return 0;
}

shared library how to link to a symbol?

I have:
car.cc
#include "car.h"
#include <iostream>
using namespace std;
extern "C" Car* create_object()
{
return new Car;
}
Car::Car() {
this->maxGear = 2;
this->currentGear = 1;
this->speed = 0;
}
void Car::shift(int gear) {
if (gear < 1 || gear > maxGear) {
return;
}
currentGear = gear;
}
void Car::brake() {
speed -= (5 * this->getCurrentGear());
std::cout<<"THE SPEED IS:" <<speed<<std::endl;
}
extern "C" void destroy_object( Car* object )
{
delete object;
}
car.h
#ifndef VEHICLES_CAR_H
#define VEHICLES_CAR_H
// A very simple car class
class Car {
public:
Car();
void shift(int gear);
void accelerate();
void brake();
private:
int maxGear;
int currentGear;
int speed;
};
#endif /* VEHICLES_CAR_H */
test.cc
#include "/home/car.h"
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main()
{
/* on Linux, use "./myclass.so" */
void* handle = dlopen("/usr/lib/libCarTest.so", RTLD_LAZY);
int (*result)(int);
if (!handle)
{
}
/*dlsym(handle,"accelerate");
cout<<"IN HERE: "<<endl;
dlsym(handle,"brake");
dlclose(handle);*/
Car* (*create)();
void (*destroy)(Car*);
dlerror();
create = (Car* (*)())dlsym(handle, "create_object");
destroy = (void (*)(Car*))dlsym(handle, "destroy_object");
Car* carr = (Car*)create();
carr->brake();
destroy( carr );
dlclose(handle);
/*
Car carr;
carr.brake();
* compilation g++ test.cpp -o tst /path/libcar.so
*/
return 0;
}
After creating libMyLib.so and install it in /usr/lib i've tried to compile test.cc using: g++ test.cc -o tst -ldl. WHY do i need to include -lMyLib? is there a way to compile the code without libMyLib.so? Secondly why dlsym(handle,"brake") is not working? If i change dlsym (Car* (*).... with dlsym(handle,"brake") i get nothing. why?
Appreciate
WHY do i need to include -lMyLib?
Because you need to link to the Car::brake method.
Secondly why dlsym(handle,"brake") is not working?
Because there is no brake symbol. The method Car::brake has a complicated mangled (implementation-defined) name. You can see this in the output of nm -D.
AFAIK, you can solve it by
making all the methods of Car virtual (they will be called through a pointer, so no linking will be needed)
doing it the old C way, ie. export a free function brake() that would call the Car::brake method from the .so
making all the public methods of Car inline and defining them in the header.
emulating the virtual table approach (as we do it in C)
Combining the last two approaches:
class Car {
public:
void brake() { brake_impl(this); }
private:
void (*brake_impl)(Car*);
void do_brake(); // this would be the actual implementation
Car() : brake_impl([] (Car* c){ c->do_brake(); }) { ... }
};
Of course you could split the implementation and the interface so it's not such a mess.

Resources