Phonon
Phonon Tutorial Part 1: a simple audio player
Overview | Application Example | Backend Development
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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"