Qt mediaplayer C++ - c++

I'm trying to play music when the user is clicking a key. But music plays only once when I click first time. Help please.
#include <QApplication>
#include <QGraphicsView>
#include <QMediaPlayer>
#include <QAudioOutput>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QKeyEvent>
#include <QDebug>
class Rect : public QGraphicsRectItem
{
public:
Rect()
{
player = new QMediaPlayer();
output = new QAudioOutput();
player->setAudioOutput(output);
output->setVolume(50);
player->setSource(QUrl("qrc:/sounds/gun.mp3"));
player->play();
}
void keyPressEvent(QKeyEvent* ev)
{
player->play();
}
private:
QMediaPlayer* player;
QAudioOutput* output;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene* scene= new QGraphicsScene();
Rect* rect = new Rect();
rect->setFlags(QGraphicsItem::ItemIsFocusable);
rect->setFocus();
rect->setRect(0, 0, 300, 400);
scene->addItem(rect);
QGraphicsView* view = new QGraphicsView(scene);
view->show();
return a.exec();
delete view;
delete scene;
delete rect;
}
........................................................................................................................................

Qt has an official media player example. link
Note: the load of the media is asynchronous. play/stop/pause will control the position of the stream so you can replay it by controlling it. Please check the example project.

Related

Show main windows after the Splash Screen in C++ - QT

I have an application with a Splash Screen. I need to show the splash screen to appear before the main window appears and wait 3.5 seconds. Once it finished now I need to show the main window.
Here is the code what I have tried,
#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QTimer>
#include "game.h"
#include "player.h"
#include <QSplashScreen>
#include <QObject>
Game *game;
int main(int argc, char *argv[])
{
//Splash screen
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/images/Images/1.JPG"));
splash->show();
//main window
game = new Game();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
game->displayHome();
return a.exec();
}
Game Class
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0,0,800,600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800,600);
show();
qDebug() << "yoyoyoy";
score = new Score();
health = new Health();
connect(this->health,SIGNAL(crash()),this,SLOT(gameover3()));
}
void Game::displayHome()
{
logo = new Logo();
scene->addItem(logo);
playButton = new Menubuttons(QString("Play"));
int bxPos = this->width()/2 - playButton->boundingRect().width()/2;
int byPos = 275;
playButton->setPos(bxPos,byPos);
connect(playButton,SIGNAL(clicked()),this,SLOT(startGame()));
scene->addItem(playButton);
instructions = new Menubuttons(QString("Instructions"));
int axPos = this->width()/2 - instructions->boundingRect().width()/2;
int ayPos = 350;
instructions->setPos(axPos,ayPos);
connect(instructions,SIGNAL(clicked()),this,SLOT(instruct()));
scene->addItem(instructions);
quitButton = new Menubuttons(QString("Quit"));
int qxPos = this->width()/2 - quitButton->boundingRect().width()/2;
int qyPos = 425;
quitButton->setPos(qxPos,qyPos);
connect(quitButton,SIGNAL(clicked()),this,SLOT(close()));
scene->addItem(quitButton);
scene->removeItem(inst);
}
But it appears both at the same time. How can I fix that?
The behavior you describe is not reproduced by the complete example below
#include <QtWidgets/QApplication>
#include <qmainwindow.h>
#include <qwidget.h>
#include <qtimer.h>
#include <qsplashscreen.h>
QMainWindow* game;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QSplashScreen *splash = new QSplashScreen;
splash->setPixmap(QPixmap(":/StackOverflow/SplashScreen"));
splash->show();
game = new QMainWindow();
QTimer::singleShot(3500, splash, SLOT(close()));
QTimer::singleShot(3500, game, SLOT(show()));
return a.exec();
}
Running this displays a splash screen for 3.5 seconds followed by the main window. The issue may be your implementation for the Game class or the member function displayHome().
Edit
After your edit with the Game class definition and implementation it is clear the problem is calling show() at the end of the Game::Game() constructor. This causes game to display immediately upon construction, the subsequent call to show() in QTimer::singleShot(3500, game, SLOT(show())) is redundant upon the already visible object. To fix this simply remove show() from the Game constructor, i.e. it should be
Game::Game(QWidget *parent)
{
scene = new QGraphicsScene();
scene->setSceneRect(0, 0, 800, 600);
setBackgroundBrush(QBrush(QImage("img.png")));
setScene(scene);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setFixedSize(800, 600);
}

How do I make a QTreeView fill the whole dialog and resize with it?

I have a QTreeView and I can't find a way of making it fill the whole dialog window and resize with the window when it is resized.
Something like this:
#include <QApplication>
#include <QDialog>
#include <QHBoxLayout>
#include <QTreeView>
class MyDialog: public QDialog
{
public:
MyDialog()
{
QHBoxLayout* l = new QHBoxLayout(this);
setLayout(l);
QTreeView* v = new QTreeView(this);
l->addWidget(v);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyDialog d;
d.exec();
return a.exec();
}

Add button to QVideoWidget

everyone! I try to set a click property to a QMediaPlayer Element, but I can not find the mode to make it, and if I try to put a button in front to Video, the button puts behind to video, even with
button->raise();
videoWidget->lower();
And If I put a Button to fullscreen the screen turns in black and don't shows the video
this id the code of the video player
QMediaPlayer *player = new QMediaPlayer(this);
QVideoWidget *vw = new QVideoWidget(this);
QMediaPlaylist *PlayList = new QMediaPlaylist(this);
PlayList->addMedia(QUrl::fromLocalFile("/home/user/Videos/video.mp4"));
PlayList->setPlaybackMode(QMediaPlaylist::Loop);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(vw);
player->setVideoOutput(vw);
player->setPlaylist(PlayList);
vw->setGeometry(0,0,800,480);
vw->show();
player->play();
One possible solution is to create a widget where the QVideoWidget is placed through a layout, the button is also added and we change the position through the resizeEvent() event.
#include <QApplication>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QPushButton>
#include <QUrl>
#include <QVBoxLayout>
#include <QVideoWidget>
#include <QDebug>
class VideoWidgetButton: public QWidget{
QPushButton *btn;
QVideoWidget *vw;
QMediaPlayer *player;
public:
VideoWidgetButton(QWidget *parent=Q_NULLPTR):QWidget(parent){
setLayout(new QVBoxLayout);
layout()->setContentsMargins(0, 0, 0, 0);
vw = new QVideoWidget(this);
btn = new QPushButton(this);
btn->setIcon(QIcon(":/icons/tux.jpeg"));
btn->resize(QSize(128, 128));
btn->setIconSize(QSize(128, 128));
connect(btn, &QPushButton::clicked, [](){
qDebug()<<"clicked";
});
layout()->addWidget(vw);
player = new QMediaPlayer(this);
player->setVideoOutput(vw);
QMediaPlaylist *playList = new QMediaPlaylist(this);
playList->addMedia(QUrl("qrc:/video/SampleVideo_1280x720_1mb.mp4"));
playList->setPlaybackMode(QMediaPlaylist::Loop);
player->setPlaylist(playList);
player->play();
}
protected:
void resizeEvent(QResizeEvent *ev){
btn->move(rect().bottomRight()-btn->rect().bottomRight());
return QWidget::resizeEvent(ev);
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
VideoWidgetButton w;
w.resize(640, 480);
w.show();
return a.exec();
}
The complete example can be found in the following link.

Cannot play movie with QMediaPlayer

I try to play movie with Qt5 on OS X El Capitan v10.11.6.
I use QMediaPlayer, QMediaPlaylist, and QVideoWidget to play.
Write source code as same as Qt's documentation, but it show only black window and not play any movie.
Here is my source code.
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainWindow mainwindow;
mainwindow.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
class QMediaPlayer;
class QMediaPlaylist;
class QVideoWidget;
class MainWindow : public QWidget
{
Q_OBJECT
public:
MainWindow(QWidget* parent = 0);
private:
QMediaPlayer* player;
QMediaPlaylist* playlist;
QVideoWidget* videoWidget;
};
#endif
mainwindow.cpp
#include <QtWidgets>
#include <QMediaPlayer>
#include <QMediaPlaylist>
#include <QVideoWidget>
#include "mainwindow.h"
MainWindow::MainWindow(QWidget* parent)
: QWidget(parent)
{
player = new QMediaPlayer;
playlist = new QMediaPlaylist;
videoWidget = new QVideoWidget;
player->setPlaylist(playlist);
player->setVideoOutput(videoWidget);
playlist->addMedia(QUrl::fromLocalFile("box.mp4"));
videoWidget->show();
playlist->setCurrentIndex(1);
player->play();
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(videoWidget);
setLayout(mainLayout);
}
I check "box.mp4" exists in the same directory.
Where is problem? How should I fix source code to solve this problem?
Just modify media file path to full path in mainwindow.cpp.
Before
playlist->addMedia(QUrl::fromLocalFile("box.mp4"));
After
playlist->addMedia(QUrl::fromLocalFile("/path/to/box.mp4"));

in Qt Gui how to make a button randomly appear every click

Alright so is there any way to make this program randomly change the variables x and y every time the button is clicked i am new to programming...
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QtGUI>
#include <QWidget>
#include <cstdlib>
#include <ctime>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new QWidget;
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
QPushButton *MainInter = new QPushButton("Push me!",window);
QPropertyAnimation *animation = new QPropertyAnimation(MainInter, "pos");
animation->setDuration(0);
animation->setEndValue(QPoint(x,y));
Object::connect(MainInter,SIGNAL(released()),animation,SLOT(start()));
window->resize(900,500);
window->show();
return a.exec();
}
What you can do is, instead of connecting the released() signal of your button directly to your animations start() SLOT, you would create your own custom SLOT. Then you connect the button to it, handle the action, and call the animation.
First read up on how to create a custom QWidget, instead of creating top level object in your main(). Simple example here
A custom widget might look like this:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
class QPushButton;
class QPropertyAnimation;
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget(QWidget *parent = 0);
private:
QPushButton *button;
QPropertyAnimation *animation;
public slots:
void randomizeAnim();
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <ctime>
MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)
{
button = new QPushButton("Push me!", this);
animation = new QPropertyAnimation(button, "pos");
animation->setDuration(0);
QObject::connect(button, SIGNAL(released()), this, SLOT(randomizeAnim()));
}
void MyWidget::randomizeAnim()
{
srand(time(0));
int x = 1+(rand()%900);
int y = 1+(rand()%400);
animation->setEndValue(QPoint(x,y));
animation->start();
}
And now your main.cpp can be reduced to the boilerplate code:
#include <QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *window = new MyWidget;
window->resize(900,500);
window->show();
return a.exec();
}
Every time you click, your custom slot will handle the action and do the animation.