qtmobility/examples/player/player.cpp
changeset 14 6fbed849b4f4
parent 11 06b8e2af4411
child 15 1f895d8a5b2b
equal deleted inserted replaced
11:06b8e2af4411 14:6fbed849b4f4
    69     , mediaKeysObserver(0)
    69     , mediaKeysObserver(0)
    70     , playlistDialog(0)
    70     , playlistDialog(0)
    71     , toggleAspectRatio(0)
    71     , toggleAspectRatio(0)
    72     , showYoutubeDialog(0)
    72     , showYoutubeDialog(0)
    73     , youtubeDialog(0)
    73     , youtubeDialog(0)
       
    74 #else
    74     , audioEndpointSelector(0)
    75     , audioEndpointSelector(0)
    75 #else
       
    76     , colorDialog(0)
    76     , colorDialog(0)
    77 #endif
    77 #endif
    78 {
    78 {
       
    79 //! [create-objs]
    79     player = new QMediaPlayer(this);
    80     player = new QMediaPlayer(this);
    80     // owerd by PlaylistModel
    81     // owned by PlaylistModel
    81     playlist = new QMediaPlaylist();
    82     playlist = new QMediaPlaylist();
    82     playlist->setMediaObject(player);
    83     player->setPlaylist(playlist);
       
    84 //! [create-objs]
    83 
    85 
    84     connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    86     connect(player, SIGNAL(durationChanged(qint64)), SLOT(durationChanged(qint64)));
    85     connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
    87     connect(player, SIGNAL(positionChanged(qint64)), SLOT(positionChanged(qint64)));
    86     connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
    88     connect(player, SIGNAL(metaDataChanged()), SLOT(metaDataChanged()));
    87     connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
    89     connect(playlist, SIGNAL(currentIndexChanged(int)), SLOT(playlistPositionChanged(int)));
    88     connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
    90     connect(player, SIGNAL(mediaStatusChanged(QMediaPlayer::MediaStatus)),
    89             this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    91             this, SLOT(statusChanged(QMediaPlayer::MediaStatus)));
    90     connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
    92     connect(player, SIGNAL(bufferStatusChanged(int)), this, SLOT(bufferingProgress(int)));
    91     connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
    93     connect(player, SIGNAL(error(QMediaPlayer::Error)), this, SLOT(displayErrorMessage()));
    92 
    94 
       
    95 //! [2]
    93     videoWidget = new VideoWidget(this);
    96     videoWidget = new VideoWidget(this);
    94     videoWidget->setMediaObject(player);
    97     player->setVideoOutput(videoWidget);
    95 
    98 
    96     playlistModel = new PlaylistModel(this);
    99     playlistModel = new PlaylistModel(this);
    97     playlistModel->setPlaylist(playlist);
   100     playlistModel->setPlaylist(playlist);
       
   101 //! [2]
    98 
   102 
    99     playlistView = new QListView(this);
   103     playlistView = new QListView(this);
   100     playlistView->setModel(playlistModel);
   104     playlistView->setModel(playlistModel);
   101     playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0));
   105     playlistView->setCurrentIndex(playlistModel->index(playlist->currentIndex(), 0));
   102 
   106 
   105     slider = new QSlider(Qt::Horizontal, this);
   109     slider = new QSlider(Qt::Horizontal, this);
   106     slider->setRange(0, player->duration() / 1000);
   110     slider->setRange(0, player->duration() / 1000);
   107 
   111 
   108     connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
   112     connect(slider, SIGNAL(sliderMoved(int)), this, SLOT(seek(int)));
   109     
   113     
   110     audioEndpointSelector = qobject_cast<QAudioEndpointSelector*>(player->service()->control(QAudioEndpointSelector_iid));
   114     QMediaService *service = player->service();
   111     connect(audioEndpointSelector, SIGNAL(activeEndpointChanged(const QString&)), this, SLOT(handleAudioOutputChangedSignal(const QString&)));
   115     if (service) {
       
   116         QMediaControl *control = service->requestControl(QAudioEndpointSelector_iid);
       
   117         if (control) {
       
   118             audioEndpointSelector = qobject_cast<QAudioEndpointSelector*>(control);
       
   119             if (audioEndpointSelector) {
       
   120                 connect(audioEndpointSelector, SIGNAL(activeEndpointChanged(const QString&)),
       
   121                         this, SLOT(handleAudioOutputChangedSignal(const QString&)));
       
   122             } else {
       
   123                 service->releaseControl(control);
       
   124             }
       
   125         }
       
   126     }
   112 
   127 
   113 #ifndef Q_OS_SYMBIAN
   128 #ifndef Q_OS_SYMBIAN
   114     QPushButton *openButton = new QPushButton(tr("Open"), this);
   129     QPushButton *openButton = new QPushButton(tr("Open"), this);
   115 
   130 
   116     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
   131     connect(openButton, SIGNAL(clicked()), this, SLOT(open()));
   224 
   239 
   225     setLayout(layout);
   240     setLayout(layout);
   226 
   241 
   227     metaDataChanged();
   242     metaDataChanged();
   228 
   243 
   229     QStringList fileNames = qApp->arguments();
   244     QStringList arguments = qApp->arguments();
   230     fileNames.removeAt(0);
   245     arguments.removeAt(0);
   231     foreach (QString const &fileName, fileNames) {
   246     foreach (QString const &argument, arguments) {
   232         if (fileName.startsWith(QLatin1String("http://")))
   247         QFileInfo fileInfo(argument);
   233             playlist->addMedia(QUrl(fileName));
   248         if (fileInfo.exists()) {
   234         else if (QFileInfo(fileName).exists())
   249             QUrl url = QUrl::fromLocalFile(fileInfo.absoluteFilePath());
   235             playlist->addMedia(QUrl::fromLocalFile(fileName));
   250             if (fileInfo.suffix().toLower() == QLatin1String("m3u")) {
       
   251                 playlist->load(url);
       
   252             } else
       
   253                 playlist->addMedia(url);
       
   254         } else {
       
   255             QUrl url(argument);
       
   256             if (url.isValid()) {
       
   257                 playlist->addMedia(url);
       
   258             }
       
   259         }
   236     }
   260     }
   237 }
   261 }
   238 
   262 
   239 Player::~Player()
   263 Player::~Player()
   240 {
   264 {
   264 #ifdef Q_OS_SYMBIAN
   288 #ifdef Q_OS_SYMBIAN
   265     if (player->isMetaDataAvailable()) {
   289     if (player->isMetaDataAvailable()) {
   266         setTrackInfo(QString("(%1/%2) %3 - %4")
   290         setTrackInfo(QString("(%1/%2) %3 - %4")
   267                 .arg(playlist->currentIndex()+1)
   291                 .arg(playlist->currentIndex()+1)
   268                 .arg(playlist->mediaCount())
   292                 .arg(playlist->mediaCount())
   269                 .arg(player->metaData(QtMediaServices::AlbumArtist).toString())
   293                 .arg(player->metaData(QtMultimedia::AlbumArtist).toString())
   270                 .arg(player->metaData(QtMediaServices::Title).toString()));
   294                 .arg(player->metaData(QtMultimedia::Title).toString()));
   271 
   295 
   272         if (!player->isVideoAvailable()) {
   296         if (!player->isVideoAvailable()) {
   273             QUrl uri = player->metaData(QtMediaServices::CoverArtUrlLarge).value<QUrl>();
   297             QUrl uri = player->metaData(QtMultimedia::CoverArtUrlLarge).value<QUrl>();
   274             QPixmap pixmap = NULL;
   298             QPixmap pixmap = NULL;
   275 
   299 
   276             if (uri.isEmpty()) {
   300             if (uri.isEmpty()) {                
   277                 QVariant picture = player->extendedMetaData("attachedpicture");
   301                 QVariant picture = player->metaData(QtMultimedia::CoverArtImage);
   278                 // Load picture from metadata
   302                 // Load picture from metadata
   279                 if (!picture.isNull() && picture.canConvert<QByteArray>())
   303                 if (!picture.isNull() && picture.canConvert<QByteArray>())
   280                     pixmap.loadFromData(picture.value<QByteArray>());
   304                     pixmap.loadFromData(picture.value<QByteArray>());
   281 
   305 
   282                 // Load some jpg from same dir as media
   306                 // Load some jpg from same dir as media
   298                    pixmap.load(path);
   322                    pixmap.load(path);
   299                 }
   323                 }
   300                 // Load picture from file pointed by uri
   324                 // Load picture from file pointed by uri
   301             } else
   325             } else
   302                 pixmap.load(uri.toString());
   326                 pixmap.load(uri.toString());
   303 
   327             
   304             coverLabel->setPixmap((!pixmap.isNull())?pixmap:QPixmap());
   328             coverLabel->setPixmap((!pixmap.isNull())?pixmap:QPixmap());
       
   329             coverLabel->setAlignment(Qt::AlignCenter);            
       
   330             coverLabel->setScaledContents(true);
   305             }
   331             }
   306     hideOrShowCoverArt();
   332     hideOrShowCoverArt();
   307     }
   333     }
   308 #else
   334 #else
   309     //qDebug() << "update metadata" << player->metaData(QtMediaServices::Title).toString();
   335     //qDebug() << "update metadata" << player->metaData(QtMultimedia::Title).toString();
   310     if (player->isMetaDataAvailable()) {
   336     if (player->isMetaDataAvailable()) {
   311         setTrackInfo(QString("%1 - %2")
   337         setTrackInfo(QString("%1 - %2")
   312                 .arg(player->metaData(QtMediaServices::AlbumArtist).toString())
   338                 .arg(player->metaData(QtMultimedia::AlbumArtist).toString())
   313                 .arg(player->metaData(QtMediaServices::Title).toString()));
   339                 .arg(player->metaData(QtMultimedia::Title).toString()));
   314 
   340 
   315         if (coverLabel) {
   341         if (coverLabel) {
   316             QUrl url = player->metaData(QtMediaServices::CoverArtUrlLarge).value<QUrl>();
   342             QUrl url = player->metaData(QtMultimedia::CoverArtUrlLarge).value<QUrl>();
   317 
   343 
   318             coverLabel->setPixmap(!url.isEmpty()
   344             coverLabel->setPixmap(!url.isEmpty()
   319                     ? QPixmap(url.toString())
   345                     ? QPixmap(url.toString())
   320                     : QPixmap());
   346                     : QPixmap());
   321         }
   347         }