Phonon: Difference between revisions

    From KDE TechBase
    Line 20: Line 20:


    = Using the phonon API =
    = Using the phonon API =
    The following example is taken from http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/phonon/html/index.html. It lets you choose a multimedia file and tries to play it. To change the sound device that is used by default, use the command <code>systemsettings</code>.
    The following example is taken from http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/phonon/html/index.html. It lets you choose a multimedia file and tries to play it. To change the sound device that is used by default, use the command <tt>systemsettings</tt>.


    == tutorial2.cpp ==
    == tutorial2.cpp ==

    Revision as of 08:34, 15 October 2008

    In order to make the revision process clear, I introduce a confident index, "0" means nobody except me has reviewed the text. I please the reviewers to increment the counter to prove the validity of the tutorials.

    You can get help on #phonon on irc.freenode.org.


    Introduction

    Confident Index =0

    The following is based on the documentation available here Phonon API.

    By getting involved with Phonon, you can choose between three different main tasks.

    • Using the Phonon API, which allows you to develop your own multimedia application. In fact any application which needs sound can take benefits from the phonon API. In the tutorials showing the API we will learn how to set up the development environment with kdevelop 3.4.
    • Hacking the Phonon library, (try to develop more this topic).
    • Writing Phonon backend, this consists in writing interfaces that allows Phonon to use different sound/video engine. This usually requires good skills and knowledge of the engine you interface with.

    Beginners will probably be more interested by the first task.

    Using the phonon API

    The following example is taken from http://www.englishbreakfastnetwork.org/apidocs/apidox-kde-4.0/kdelibs-apidocs/phonon/html/index.html. It lets you choose a multimedia file and tries to play it. To change the sound device that is used by default, use the command systemsettings.

    tutorial2.cpp

    /*   This file is part of the KDE project
         Copyright (C) 2007 Matthias Kretz <[email protected]>
         adapted 2008 by Thorsten Staerk
     
         Permission to use, copy, modify, and distribute this software
         and its documentation for any purpose and without fee is hereby
         granted, provided that the above copyright notice appear in all
         copies and that both that the copyright notice and this
         permission notice and warranty disclaimer appear in supporting
         documentation, and that the name of the author not be used in
         advertising or publicity pertaining to distribution of the
         software without specific, written prior permission.
     
         The author disclaim all warranties with regard to this
         software, including all implied warranties of merchantability
         and fitness.  In no event shall the author be liable for any
         special, indirect or consequential damages or any damages
         whatsoever resulting from loss of use, data or profits, whether
         in an action of contract, negligence or other tortious action,
         arising out of or in connection with the use or performance of
         this software.
    */
    
    #include <phonon/mediaobject.h>
    #include <phonon/path.h>
    #include <phonon/audiooutput.h>
    
    #include <QtGui/QApplication>
    #include <QtGui/QMainWindow>
    #include <QtGui/QDirModel>
    #include <QtGui/QColumnView>
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
        public:
            MainWindow();
    
        private slots:
            void play(const QModelIndex &index);
    
        private:
            void delayedInit();
    
            QColumnView m_fileView;
            QDirModel m_model;
    
            Phonon::MediaObject *m_media;
    };
    
    MainWindow::MainWindow()
        : m_fileView(this),
        m_media(0)
    {
        setCentralWidget(&m_fileView);
        m_fileView.setModel(&m_model);
        m_fileView.setFrameStyle(QFrame::NoFrame);
    
        connect(&m_fileView, SIGNAL(updatePreviewWidget(const QModelIndex &)), SLOT(play(const QModelIndex &)));
    }
    
    void MainWindow::play(const QModelIndex &index)
    {
        delayedInit();
        m_media->setCurrentSource(m_model.filePath(index));
        m_media->play();
    }
    
    void MainWindow::delayedInit()
    {
         if (!m_media) {
            m_media = new Phonon::MediaObject(this);
            Phonon::AudioOutput *audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
            createPath(m_media, audioOutput);
        }
    }
    
    int main(int argc, char **argv)
    {
        QApplication app(argc, argv);
        QApplication::setApplicationName("Phonon Tutorial 2");
        MainWindow mw;
        mw.show();
        return app.exec();
    }
    
    #include "tutorial2.moc"
    

    Compile and run it

    moc tutorial2.cpp > tutorial2.moc
    g++ -o tutorial -lphonon tutorial2.cpp && ./tutorial