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:
@@ -41,13 +41,33 @@
|
||||
#include "player.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QCommandLineParser>
|
||||
#include <QCommandLineOption>
|
||||
#include <QDir>
|
||||
|
||||
int main(int argc, char *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;
|
||||
|
||||
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)
|
||||
player.setAttribute(Qt::WA_LockLandscapeOrientation);
|
||||
player.showMaximized();
|
||||
|
||||
@@ -167,7 +167,7 @@ Player::Player(QWidget *parent)
|
||||
|
||||
setLayout(layout);
|
||||
|
||||
if (!player->isAvailable()) {
|
||||
if (!isPlayerAvailable()) {
|
||||
QMessageBox::warning(this, tr("Service not available"),
|
||||
tr("The QMediaPlayer object does not have a valid service.\n"\
|
||||
"Please check the media service plugins are installed."));
|
||||
@@ -182,38 +182,47 @@ Player::Player(QWidget *parent)
|
||||
}
|
||||
|
||||
metaDataChanged();
|
||||
|
||||
QStringList arguments = qApp->arguments();
|
||||
arguments.removeAt(0);
|
||||
addToPlaylist(arguments);
|
||||
}
|
||||
|
||||
Player::~Player()
|
||||
{
|
||||
}
|
||||
|
||||
void Player::open()
|
||||
bool Player::isPlayerAvailable() const
|
||||
{
|
||||
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Open Files"));
|
||||
addToPlaylist(fileNames);
|
||||
return player->isAvailable();
|
||||
}
|
||||
|
||||
void Player::addToPlaylist(const QStringList& fileNames)
|
||||
void Player::open()
|
||||
{
|
||||
foreach (QString const &argument, fileNames) {
|
||||
QFileInfo fileInfo(argument);
|
||||
if (fileInfo.exists()) {
|
||||
QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
|
||||
if (fileInfo.suffix().toLower() == QLatin1String("m3u")) {
|
||||
QFileDialog fileDialog(this);
|
||||
fileDialog.setAcceptMode(QFileDialog::AcceptOpen);
|
||||
fileDialog.setWindowTitle(tr("Open Files"));
|
||||
QStringList supportedMimeTypes = player->supportedMimeTypes();
|
||||
if (!supportedMimeTypes.isEmpty()) {
|
||||
supportedMimeTypes.append("audio/x-m3u"); // MP3 playlists
|
||||
fileDialog.setMimeTypeFilters(supportedMimeTypes);
|
||||
}
|
||||
fileDialog.setDirectory(QStandardPaths::standardLocations(QStandardPaths::MoviesLocation).value(0, QDir::homePath()));
|
||||
if (fileDialog.exec() == QDialog::Accepted)
|
||||
addToPlaylist(fileDialog.selectedUrls());
|
||||
}
|
||||
|
||||
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
|
||||
else
|
||||
playlist->addMedia(url);
|
||||
} else {
|
||||
QUrl url(argument);
|
||||
if (url.isValid()) {
|
||||
playlist->addMedia(url);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -69,6 +69,10 @@ public:
|
||||
Player(QWidget *parent = 0);
|
||||
~Player();
|
||||
|
||||
bool isPlayerAvailable() const;
|
||||
|
||||
void addToPlaylist(const QList<QUrl> urls);
|
||||
|
||||
signals:
|
||||
void fullScreenChanged(bool fullScreen);
|
||||
|
||||
@@ -93,7 +97,6 @@ private slots:
|
||||
#ifndef PLAYER_NO_COLOROPTIONS
|
||||
void showColorDialog();
|
||||
#endif
|
||||
void addToPlaylist(const QStringList &fileNames);
|
||||
|
||||
private:
|
||||
void setTrackInfo(const QString &info);
|
||||
|
||||
Reference in New Issue
Block a user