basicmedia.cpp Example File
simpleplayer/basicmedia.cpp
#include "basicmedia.h"
#include <QContent>
#include <QMediaControlNotifier>
#include <QMediaVideoControl>
#include <QVBoxLayout>
#include <QApplication>
#include <QMediaControl>
#include <QResizeEvent>
#include <QSize>
#include <QDebug>
BasicMedia::BasicMedia(QWidget* parent)
: QWidget(parent), m_mediaContent(0), m_control(0), m_video(0), m_state(0), videoWidget(0)
{
layout = new QVBoxLayout;
layout->setMargin(0);
setLayout(layout);
context = new QMediaContentContext(this);
QMediaControlNotifier* notifier = new QMediaControlNotifier(QMediaControl::name(), this);
connect(notifier, SIGNAL(valid()), this, SLOT(mediaControlValid()));
context->addObject(notifier);
QMediaControlNotifier* video = new QMediaControlNotifier(QMediaVideoControl::name(), this);
connect(video, SIGNAL(valid()), this, SLOT(mediaVideoControlValid()));
context->addObject(video);
volume=100;
}
void BasicMedia::keyReleaseEvent( QKeyEvent *ke )
{
ke->ignore();
}
void BasicMedia::setFilename(QString f)
{
m_state = 1;
vfile = f;
}
void BasicMedia::mediaVideoControlValid()
{
if(m_state == 2) {
m_state = 3;
delete m_video;
delete videoWidget;
m_video = new QMediaVideoControl(m_mediaContent);
videoWidget = m_video->createVideoWidget(this);
if (!videoWidget) {
qWarning("Failed to create video widget");
return;
}
videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
layout->addWidget(videoWidget);
} else {
qWarning("(%d) ERROR BasicMedia::mediaVideoControlValid, should appear after mediaControlValid???",m_state);
}
}
void BasicMedia::mediaControlValid()
{
if(m_state == 1) {
m_state = 2;
delete m_control;
m_control = new QMediaControl(m_mediaContent);
m_control->start();
} else {
qWarning("(%d) ERROR BasicMedia::mediaControlValid, must call setFilename() before start()!!!",m_state);
}
}
BasicMedia::~BasicMedia()
{
delete m_mediaContent;
delete m_control;
}
void BasicMedia::stop()
{
if(m_state == 3) {
m_control->stop();
m_state=1;
} else if(m_control) {
m_control->stop();
m_state=1;
}
if(m_state > 1) {
if(m_video!=NULL) {
delete m_video;
delete videoWidget;
delete m_mediaContent;
m_video = NULL;
videoWidget = NULL;
m_mediaContent = NULL;
}
}
}
void BasicMedia::start()
{
if(m_state == 1) {
QContent content(vfile);
if (!content.isValid()) {
qWarning() << "Failed to load" << vfile;
return;
}
m_mediaContent = new QMediaContent(content);
context->setMediaContent(m_mediaContent);
} else {
qWarning("(%d) BasicMedia::start() must stop() and setFilename() before calling start!!!",m_state);
}
}
void BasicMedia::volumeup()
{
if(volume<90) {
volume=volume+10;
m_control->setVolume(volume);
}
}
void BasicMedia::volumedown()
{
if(volume>10) {
volume=volume-10;
m_control->setVolume(volume);
}
}
Copyright © 2009 Trolltech |
Trademarks |
Qt Extended 4.4.3 |