File indexing completed on 2024-05-12 16:35:05
0001 /* This file is part of the KDE project 0002 * Copyright (C) 2010 C. Boemann <cbo@boemann.dk> 0003 * Copyright (C) 2012 Gopalakrishna Bhat A <gopalakbhat@gmail.com> 0004 * 0005 * This library is free software; you can redistribute it and/or 0006 * modify it under the terms of the GNU Library General Public 0007 * License as published by the Free Software Foundation; either 0008 * version 2 of the License, or (at your option) any later version. 0009 * 0010 * This library is distributed in the hope that it will be useful, 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0013 * Library General Public License for more details. 0014 * 0015 * You should have received a copy of the GNU Library General Public License 0016 * along with this library; see the file COPYING.LIB. If not, write to 0017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0018 * Boston, MA 02110-1301, USA. 0019 */ 0020 0021 #include "FullScreenPlayer.h" 0022 0023 #include <KoIcon.h> 0024 0025 #include <klocalizedstring.h> 0026 0027 #include <phonon/videowidget.h> 0028 #include <phonon/audiooutput.h> 0029 #include <phonon/mediaobject.h> 0030 #include <phonon/VolumeSlider> 0031 #include <phonon/SeekSlider> 0032 0033 #include <QUrl> 0034 #include <QVBoxLayout> 0035 #include <QHBoxLayout> 0036 #include <QKeyEvent> 0037 #include <QLabel> 0038 #include <QToolButton> 0039 0040 FullScreenPlayer::FullScreenPlayer(const QUrl &url) 0041 : QWidget(0) 0042 , m_seekSlider(new Phonon::SeekSlider(this)) 0043 , m_volumeSlider(new Phonon::VolumeSlider(this)) 0044 { 0045 m_mediaObject = new Phonon::MediaObject(); 0046 m_mediaObject->setTickInterval(1000); 0047 0048 m_videoWidget = new Phonon::VideoWidget(this); 0049 Phonon::createPath(m_mediaObject, m_videoWidget); 0050 0051 m_audioOutput = new Phonon::AudioOutput(Phonon::VideoCategory); 0052 connect(m_audioOutput, SIGNAL(mutedChanged(bool)), this, SLOT(muteStateChanged(bool))); 0053 0054 Phonon::createPath(m_mediaObject, m_audioOutput); 0055 0056 m_seekSlider->setMediaObject(m_mediaObject); 0057 m_seekSlider->setIconVisible(false); 0058 0059 m_volumeSlider->setAudioOutput(m_audioOutput); 0060 m_volumeSlider->setMuteVisible(false); 0061 m_volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum); 0062 0063 m_playbackTime = new QLabel(QString("00:00:00"), this); 0064 0065 m_play = new QToolButton(this); 0066 m_play->setIcon(koIcon("media-playback-start")); 0067 m_play->setToolTip(i18n("Play")); 0068 connect(m_play, SIGNAL(clicked()), this, SLOT(play())); 0069 0070 m_pause = new QToolButton(this); 0071 m_pause->setIcon(koIcon("media-playback-pause")); 0072 m_pause->setToolTip(i18n("Pause")); 0073 connect(m_pause, SIGNAL(clicked()), this, SLOT(pause())); 0074 0075 m_stop = new QToolButton(this); 0076 m_stop->setIcon(koIcon("media-playback-stop")); 0077 m_stop->setToolTip(i18n("Stop")); 0078 connect(m_stop, SIGNAL(clicked()), this, SLOT(stop())); 0079 0080 m_volumeIconMuted = new QToolButton(this); 0081 m_volumeIconMuted->setIcon(koIcon("audio-volume-muted")); 0082 m_volumeIconMuted->setToolTip(i18n("Unmute")); 0083 connect(m_volumeIconMuted, SIGNAL(clicked()), this, SLOT(unmute())); 0084 0085 m_volumeIconUnmuted = new QToolButton(this); 0086 m_volumeIconUnmuted->setIcon(koIcon("audio-volume-medium")); 0087 m_volumeIconUnmuted->setToolTip(i18n("Mute")); 0088 connect(m_volumeIconUnmuted, SIGNAL(clicked()), this, SLOT(mute())); 0089 0090 QHBoxLayout *playbackControls = new QHBoxLayout(); 0091 playbackControls->addWidget(m_play); 0092 playbackControls->addWidget(m_pause); 0093 playbackControls->addWidget(m_stop); 0094 playbackControls->addWidget(m_seekSlider); 0095 playbackControls->addWidget(m_playbackTime); 0096 playbackControls->addWidget(m_volumeIconMuted); 0097 playbackControls->addWidget(m_volumeIconUnmuted); 0098 playbackControls->addWidget(m_volumeSlider); 0099 playbackControls->setSizeConstraint(QLayout::SetFixedSize); 0100 0101 QVBoxLayout *layout = new QVBoxLayout; 0102 layout->addWidget(m_videoWidget); 0103 layout->addLayout(playbackControls); 0104 layout->setMargin(0); 0105 setLayout(layout); 0106 show(); 0107 setWindowState(Qt::WindowFullScreen); 0108 0109 m_mediaObject->setCurrentSource(url); 0110 connect(m_mediaObject, SIGNAL(finished()), this, SLOT(stop())); 0111 connect(m_mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)), 0112 this, SLOT(playStateChanged(Phonon::State,Phonon::State))); 0113 connect(m_mediaObject, SIGNAL(tick(qint64)), this, SLOT(updatePlaybackTime(qint64))); 0114 0115 play(); 0116 0117 mute(); 0118 unmute(); 0119 } 0120 0121 FullScreenPlayer::~FullScreenPlayer() 0122 { 0123 } 0124 0125 void FullScreenPlayer::play() 0126 { 0127 0128 m_mediaObject->play(); 0129 } 0130 0131 void FullScreenPlayer::pause() 0132 { 0133 m_mediaObject->pause(); 0134 } 0135 0136 void FullScreenPlayer::stop() 0137 { 0138 m_mediaObject->stop(); 0139 deleteLater(); 0140 } 0141 0142 void FullScreenPlayer::mute() 0143 { 0144 qreal volume = m_audioOutput->volume(); 0145 m_audioOutput->setMuted(true); 0146 m_audioOutput->setVolume(volume); // 0147 } 0148 0149 void FullScreenPlayer::unmute() 0150 { 0151 m_audioOutput->setMuted(false); 0152 } 0153 0154 void FullScreenPlayer::mousePressEvent(QMouseEvent *event) 0155 { 0156 Q_UNUSED(event) 0157 m_mediaObject->stop(); 0158 deleteLater(); 0159 } 0160 0161 void FullScreenPlayer::keyPressEvent(QKeyEvent *event) 0162 { 0163 if(event->key()==Qt::Key_Escape) { 0164 m_mediaObject->stop(); 0165 deleteLater(); 0166 } 0167 } 0168 0169 void FullScreenPlayer::playStateChanged(Phonon::State newState, Phonon::State oldState) 0170 { 0171 Q_UNUSED(oldState); 0172 0173 switch (newState) { 0174 case Phonon::PlayingState: 0175 m_play->setVisible(false); 0176 m_pause->setVisible(true); 0177 break; 0178 case Phonon::PausedState: 0179 m_play->setVisible(true); 0180 m_pause->setVisible(false); 0181 break; 0182 default: 0183 ; 0184 } 0185 } 0186 0187 void FullScreenPlayer::updatePlaybackTime(qint64 currentTime) 0188 { 0189 QString currentPlayTime = QString("%1:%2:%3") 0190 .arg((currentTime / 3600000) % 60, 2, 10, QChar('0')) 0191 .arg((currentTime / 60000) % 60, 2, 10, QChar('0')) 0192 .arg((currentTime / 1000) % 60, 2, 10, QChar('0')); 0193 0194 qint64 time = m_mediaObject->totalTime(); 0195 QString totalTime = QString("%1:%2:%3") 0196 .arg((time / 3600000) % 60, 2, 10, QChar('0')) 0197 .arg((time / 60000) % 60, 2, 10, QChar('0')) 0198 .arg((time / 1000) % 60, 2, 10, QChar('0')); 0199 0200 m_playbackTime->setText(QString("%1/%2").arg(currentPlayTime).arg(totalTime)); 0201 } 0202 0203 0204 void FullScreenPlayer::muteStateChanged(bool muted) 0205 { 0206 if (muted) { 0207 m_volumeIconMuted->setVisible(true); 0208 m_volumeIconUnmuted->setVisible(false); 0209 } else { 0210 m_volumeIconMuted->setVisible(false); 0211 m_volumeIconUnmuted->setVisible(true); 0212 } 0213 }