Polish the QtWidgets/Player example.

Point the file dialog to the Movies folder.
Add command proper command line handling.
Change the logic to use QUrl everywhere.

Change-Id: I1e54e600187153f52a55e3a381a24e4f2eeda3ab
Reviewed-by: Yoann Lopes <yoann.lopes@theqtcompany.com>
This commit is contained in:
Friedemann Kleint
2016-03-01 14:58:18 +01:00
parent 1816f89b6f
commit 787211c1d2
3 changed files with 56 additions and 24 deletions

View File

@@ -41,13 +41,33 @@
#include "player.h" #include "player.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QDir>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QApplication app(argc, argv); QApplication app(argc, argv);
QCoreApplication::setApplicationName("Player Example");
QCoreApplication::setOrganizationName("QtProject");
QCoreApplication::setApplicationVersion(QT_VERSION_STR);
QCommandLineParser parser;
parser.setApplicationDescription("Qt MultiMedia Player Example");
parser.addHelpOption();
parser.addVersionOption();
parser.addPositionalArgument("url", "The URL to open.");
parser.process(app);
Player player; Player player;
if (!parser.positionalArguments().isEmpty() && player.isPlayerAvailable()) {
QList<QUrl> urls;
foreach (const QString &a, parser.positionalArguments())
urls.append(QUrl::fromUserInput(a, QDir::currentPath(), QUrl::AssumeLocalFile));
player.addToPlaylist(urls);
}
#if defined(Q_WS_SIMULATOR) #if defined(Q_WS_SIMULATOR)
player.setAttribute(Qt::WA_LockLandscapeOrientation); player.setAttribute(Qt::WA_LockLandscapeOrientation);
player.showMaximized(); player.showMaximized();

View File

@@ -167,7 +167,7 @@ Player::Player(QWidget *parent)
setLayout(layout); setLayout(layout);
if (!player->isAvailable()) { if (!isPlayerAvailable()) {
QMessageBox::warning(this, tr("Service not available"), QMessageBox::warning(this, tr("Service not available"),
tr("The QMediaPlayer object does not have a valid service.\n"\ tr("The QMediaPlayer object does not have a valid service.\n"\
"Please check the media service plugins are installed.")); "Please check the media service plugins are installed."));
@@ -182,38 +182,47 @@ Player::Player(QWidget *parent)
} }
metaDataChanged(); metaDataChanged();
QStringList arguments = qApp->arguments();
arguments.removeAt(0);
addToPlaylist(arguments);
} }
Player::~Player() Player::~Player()
{ {
} }
void Player::open() bool Player::isPlayerAvailable() const
{ {
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files")); return player->isAvailable();
addToPlaylist(fileNames);
} }
void Player::addToPlaylist(const QStringList& fileNames) void Player::open()
{ {
foreach (QString const &argument, fileNames) { QFileDialog fileDialog(this);
QFileInfo fileInfo(argument); fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
if (fileInfo.exists()) { fileDialog.setWindowTitle(tr("Open Files"));
QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath()); QStringList supportedMimeTypes = player->supportedMimeTypes();
if (fileInfo.suffix().toLower() == QLatin1String("m3u")) { if (!supportedMimeTypes.isEmpty()) {
playlist->load(url); supportedMimeTypes.append("audio/x-m3u"); // MP3 playlists
} else fileDialog.setMimeTypeFilters(supportedMimeTypes);
playlist->addMedia(url); }
} else { fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));
QUrl url(argument); if (fileDialog.exec() == QDialog::Accepted)
if (url.isValid()) { addToPlaylist(fileDialog.selectedUrls());
playlist->addMedia(url); }
}
} static bool isPlaylist(const QUrl &url) // Check for ".m3u" playlists.
{
if (!url.isLocalFile())
return false;
const QFileInfo fileInfo(url.toLocalFile());
return fileInfo.exists() && !fileInfo.suffix().compare(QLatin1String("m3u"), Qt::CaseInsensitive);
}
void Player::addToPlaylist(const QList<QUrl> urls)
{
foreach (const QUrl &url, urls) {
if (isPlaylist(url))
playlist->load(url);
else
playlist->addMedia(url);
} }
} }

View File

@@ -69,6 +69,10 @@ public:
Player(QWidget *parent = 0); Player(QWidget *parent = 0);
~Player(); ~Player();
bool isPlayerAvailable() const;
void addToPlaylist(const QList<QUrl> urls);
signals: signals:
void fullScreenChanged(bool fullScreen); void fullScreenChanged(bool fullScreen);
@@ -93,7 +97,6 @@ private slots:
#ifndef PLAYER_NO_COLOROPTIONS #ifndef PLAYER_NO_COLOROPTIONS
void showColorDialog(); void showColorDialog();
#endif #endif
void addToPlaylist(const QStringList &fileNames);
private: private:
void setTrackInfo(const QString &info); void setTrackInfo(const QString &info);