File indexing completed on 2024-04-28 04:43:21
0001 /* This file is part of the KDE project 0002 Copyright (C) 2010 Trever Fischer <tdfischer@fedoraproject.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Lesser General Public 0006 License as published by the Free Software Foundation; either 0007 version 2.1 of the License, or (at your option) version 3, or any 0008 later version accepted by the membership of KDE e.V. (or its 0009 successor approved by the membership of KDE e.V.), Nokia Corporation 0010 (or its successors, if any) and the KDE Free Qt Foundation, which shall 0011 act as a proxy defined in Section 6 of version 3 of the license. 0012 0013 This library is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 Lesser General Public License for more details. 0017 0018 You should have received a copy of the GNU Lesser General Public 0019 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0020 */ 0021 #include "player.h" 0022 0023 #include <QFileDialog> 0024 #include <QHBoxLayout> 0025 #include <QMessageBox> 0026 #include <QPushButton> 0027 #include <QVBoxLayout> 0028 0029 #include <phonon/AudioOutput> 0030 #include <phonon/MediaObject> 0031 #include <phonon/Mrl> 0032 #include <phonon/SeekSlider> 0033 #include <phonon/VideoWidget> 0034 0035 Player::Player(QWidget* parent, Qt::WindowFlags flags) 0036 : QWidget(parent, flags) 0037 { 0038 m_media = new Phonon::MediaObject(this); 0039 0040 //Some platforms (i.e. Linux) provide a mechanism for a user to view a system-wide 0041 //history of content interactions. This is opt-in, and done via setting the 0042 //PlaybackTracking property to true. 0043 m_media->setProperty("PlaybackTracking", true); 0044 0045 Phonon::AudioOutput* audioOut = new Phonon::AudioOutput(Phonon::VideoCategory, this); 0046 Phonon::VideoWidget* videoOut = new Phonon::VideoWidget(this); 0047 0048 videoOut->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 0049 //By default, there is no minimum size on a video widget. While the default 0050 //size is controlled by Qt's layouting system it makes sense to provide a 0051 //minimum size, so that the widget does not disappear, when the user resizes 0052 //the window. 0053 videoOut->setMinimumSize(64, 64); 0054 0055 //After a MediaSource is loaded, this signal will be emitted to let us know 0056 //if a video stream was found. 0057 connect(m_media, SIGNAL(hasVideoChanged(bool)), videoOut, SLOT(setVisible(bool))); 0058 0059 //Link the media object to our audio and video outputs. 0060 Phonon::createPath(m_media, audioOut); 0061 Phonon::createPath(m_media, videoOut); 0062 0063 //This widget will contain the stop/pause buttons 0064 QWidget *buttonBar = new QWidget(this); 0065 0066 m_playPause = new QPushButton(tr("Play"), buttonBar); 0067 m_stop = new QPushButton(tr("Stop"), buttonBar); 0068 0069 Phonon::SeekSlider *seekSlider = new Phonon::SeekSlider(this); 0070 seekSlider->setMediaObject(m_media); 0071 0072 QVBoxLayout *layout = new QVBoxLayout(this); 0073 layout->addWidget(videoOut); 0074 layout->addWidget(seekSlider); 0075 layout->addWidget(buttonBar); 0076 setLayout(layout); 0077 0078 QHBoxLayout *buttonLayout = new QHBoxLayout(buttonBar); 0079 buttonLayout->addWidget(m_stop); 0080 buttonLayout->addWidget(m_playPause); 0081 buttonBar->setLayout(buttonLayout); 0082 0083 m_stop->setEnabled(false); 0084 0085 connect(m_stop, SIGNAL(clicked(bool)), m_media, SLOT(stop())); 0086 connect(m_playPause, SIGNAL(clicked(bool)), this, SLOT(playPause())); 0087 0088 //The mediaStateChanged slot will update the GUI elements to reflect what 0089 //the user can do next 0090 connect(m_media, SIGNAL(stateChanged(Phonon::State,Phonon::State)), this, SLOT(mediaStateChanged(Phonon::State,Phonon::State))); 0091 } 0092 0093 void Player::playPause() 0094 { 0095 if (m_media->state() == Phonon::PlayingState) { 0096 m_media->pause(); 0097 } else { 0098 if (m_media->currentSource().type() == Phonon::MediaSource::Empty) 0099 load(); 0100 m_media->play(); 0101 } 0102 } 0103 0104 void Player::load(const Phonon::Mrl &mrl) 0105 { 0106 if (mrl.scheme().isEmpty()) 0107 m_media->setCurrentSource(Phonon::Mrl::fromLocalFile(mrl.toString())); 0108 else 0109 m_media->setCurrentSource(mrl); 0110 m_media->play(); 0111 } 0112 0113 void Player::load() 0114 { 0115 QString url = QFileDialog::getOpenFileName(this); 0116 if (url.isEmpty()) 0117 return; 0118 load(Phonon::Mrl::fromLocalFile(url)); 0119 } 0120 0121 void Player::mediaStateChanged(Phonon::State newState, Phonon::State oldState) 0122 { 0123 Q_UNUSED(oldState); 0124 switch(newState) { 0125 case Phonon::LoadingState: 0126 break; 0127 case Phonon::StoppedState: 0128 m_playPause->setText(tr("Play")); 0129 m_stop->setEnabled(false); 0130 break; 0131 case Phonon::PlayingState: 0132 m_playPause->setText(tr("Pause")); 0133 m_stop->setEnabled(true); 0134 break; 0135 case Phonon::BufferingState: 0136 break; 0137 case Phonon::PausedState: 0138 m_playPause->setText(tr("Play")); 0139 break; 0140 case Phonon::ErrorState: 0141 QMessageBox::critical(this, tr("Error"), tr("Error while playing media: ") + m_media->errorString()); 0142 break; 0143 } 0144 }