Phonon
Phonon Tutorial Part 1: a simple audio player
Overview | Application Example | Backend Development
00001 /* This file is part of the KDE project 00002 Copyright (C) 2007 Matthias Kretz <kretz@kde.org> 00003 00004 Permission to use, copy, modify, and distribute this software 00005 and its documentation for any purpose and without fee is hereby 00006 granted, provided that the above copyright notice appear in all 00007 copies and that both that the copyright notice and this 00008 permission notice and warranty disclaimer appear in supporting 00009 documentation, and that the name of the author not be used in 00010 advertising or publicity pertaining to distribution of the 00011 software without specific, written prior permission. 00012 00013 The author disclaim all warranties with regard to this 00014 software, including all implied warranties of merchantability 00015 and fitness. In no event shall the author be liable for any 00016 special, indirect or consequential damages or any damages 00017 whatsoever resulting from loss of use, data or profits, whether 00018 in an action of contract, negligence or other tortious action, 00019 arising out of or in connection with the use or performance of 00020 this software. 00021 00022 */ 00023 00024 #include <Phonon/MediaObject> 00025 #include <Phonon/Path> 00026 #include <Phonon/AudioOutput> 00027 #include <Phonon/Global> 00028 00029 #include <QtGui/QApplication> 00030 #include <QtGui/QMainWindow> 00031 #include <QtGui/QDirModel> 00032 #include <QtGui/QColumnView> 00033 00034 class MainWindow : public QMainWindow 00035 { 00036 Q_OBJECT 00037 public: 00038 MainWindow(); 00039 00040 private slots: 00041 void play(const QModelIndex &index); 00042 00043 private: 00044 void delayedInit(); 00045 00046 QColumnView m_fileView; 00047 QDirModel m_model; 00048 00049 Phonon::MediaObject *m_media; 00050 }; 00051 00052 MainWindow::MainWindow() 00053 : m_fileView(this), 00054 m_media(0) 00055 { 00056 setCentralWidget(&m_fileView); 00057 m_fileView.setModel(&m_model); 00058 m_fileView.setFrameStyle(QFrame::NoFrame); 00059 00060 connect(&m_fileView, SIGNAL(updatePreviewWidget(const QModelIndex &)), SLOT(play(const QModelIndex &))); 00061 } 00062 00063 void MainWindow::play(const QModelIndex &index) 00064 { 00065 delayedInit(); 00066 m_media->setCurrentSource(m_model.filePath(index)); 00067 m_media->play(); 00068 } 00069 00070 void MainWindow::delayedInit() 00071 { 00072 if (!m_media) { 00073 m_media = new Phonon::MediaObject(this); 00074 Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); 00075 createPath(m_media, audioOutput); 00076 } 00077 } 00078 00079 int main(int argc, char **argv) 00080 { 00081 QApplication app(argc, argv); 00082 QApplication::setApplicationName("Phonon Tutorial 2"); 00083 MainWindow mw; 00084 mw.show(); 00085 return app.exec(); 00086 } 00087 00088 #include "tutorial2.moc"
KDE 4.0 API Reference