Results 1 to 2 of 2

Thread: can't get o/p by playing mp3 file in Phonon

  1. #1
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: can't get o/p by playing mp3 file in Phonon

    Hi guys,

    I have tried to make a simple mp3 player by getting the help from the given example in Qt manual. After some small modification in the code I can now able to play .mp3 file means its showing that it's playing but cant get the O/P sound. I am making this for NOKIA smart phone.

    This is my 1st assignment. Need some help,plz.

    Here I am attaching the code

    #include <QtGui>

    #include "mainwindow.h"

    int main(int argv, char **args)
    {
    QApplication app(argv, args);
    app.setApplicationName("Music Player");
    app.setQuitOnLastWindowClosed(true);

    MainWindow window;
    window.show();

    return app.exec();
    }

    //////////////////////// mainwindow header file //////////////////////////////////////

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H


    #include <QMainWindow>

    #include <Phonon/AudioOutput>
    #include <Phonon/SeekSlider>
    #include <Phonon/MediaObject>
    #include <Phonon/VolumeSlider>
    #include <Phonon/backendcapabilities.h>
    #include <QList>

    class QAction;
    class QTableWidget;
    class QLCDNumber;


    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow();
    /*
    QSize sizeHint() const
    {
    return QSize(300, 400);
    }
    */
    private slots:
    void addFiles();
    // void about();
    void stateChanged(Phonon::State newState, Phonon::State oldState);
    void tick(qint64 time);
    void sourceChanged(const Phonon::MediaSource &source);
    void metaStateChanged(Phonon::State newState, Phonon::State oldState);
    void aboutToFinish();
    void tableClicked(int row, int column);

    private:
    void setupActions();
    // void setupMenus();
    void setupUi();

    Phonon::SeekSlider *seekSlider;
    Phonon::MediaObject *mediaObject;
    Phonon::MediaObject *metaInformationResolver;
    Phonon::AudioOutput *audioOutput;
    Phonon::VolumeSlider *volumeSlider;
    QList<Phonon::MediaSource> sources;

    QAction *playAction;
    QAction *pauseAction;
    QAction *stopAction;
    QAction *nextAction;
    QAction *previousAction;
    QAction *addFilesAction;
    QAction *exitAction;
    QAction *aboutAction;
    QAction *aboutQtAction;
    QLCDNumber *timeLcd;
    QTableWidget *musicTable;
    };

    #endif

    mainLayout->addLayout(playbackLayout);

    QWidget *widget = new QWidget;
    widget->setLayout(mainLayout);

    setCentralWidget(widget);
    // setWindowTitle("Phonon Music Player");
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    #include <QtGui>
    #include<QWidget>
    #include "mainwindow.h"

    MainWindow::MainWindow()
    {
    audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    mediaObject = new Phonon::MediaObject(this);
    Phonon::createPath(mediaObject,audioOutput);

    metaInformationResolver = new Phonon::MediaObject(this);

    mediaObject->setTickInterval(1000);
    connect(mediaObject, SIGNAL(tick(qint64)), this, SLOT(tick(qint64)));
    connect(mediaObject, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
    this, SLOT(stateChanged(Phonon::State,Phonon::State)));
    connect(metaInformationResolver, SIGNAL(stateChanged(Phonon::State,Phonon::State)),
    this, SLOT(metaStateChanged(Phonon::State,Phonon::State) ));
    connect(mediaObject, SIGNAL(currentSourceChanged(Phonon::MediaSource)),
    this, SLOT(sourceChanged(Phonon::MediaSource)));
    connect(mediaObject, SIGNAL(aboutToFinish()), this, SLOT(aboutToFinish()));

    Phonon::createPath(mediaObject, audioOutput);

    setupActions();
    //setupMenus();
    setupUi();
    timeLcd->display("00:00");
    }

    void MainWindow::addFiles()
    {
    QStringList files = QFileDialog::getOpenFileNames(this, tr("Select Music Files"),
    QDesktopServices::storageLocation(QDesktopServices ::MusicLocation),"Musics(*.mp3 *.wmv *.mp4)");

    if (files.isEmpty())
    return;

    int index = sources.size();
    qDebug()<<index;
    foreach (QString string, files)
    {
    Phonon::MediaSource source(string);
    // qDebug()<<sources;
    sources.append(source);
    }
    if (!sources.isEmpty())
    metaInformationResolver->setCurrentSource(sources.at(index));

    }


    void MainWindow::stateChanged(Phonon::State newState, Phonon::State /* oldState */)
    {
    switch (newState)
    {
    case Phonon::ErrorState:
    if (mediaObject->errorType() == Phonon::FatalError)
    {
    QMessageBox::warning(this, tr("Fatal Error"),
    mediaObject->errorString());
    }
    else
    {
    QMessageBox::warning(this, tr("Error"),
    mediaObject->errorString());
    }
    break;
    case Phonon::PlayingState:
    playAction->setEnabled(false);
    pauseAction->setEnabled(true);
    stopAction->setEnabled(true);
    break;
    case Phonon::StoppedState:
    stopAction->setEnabled(false);
    playAction->setEnabled(true);
    pauseAction->setEnabled(false);
    timeLcd->display("00:00");
    break;
    case Phonon::PausedState:
    pauseAction->setEnabled(false);
    stopAction->setEnabled(true);
    playAction->setEnabled(true);
    break;
    case Phonon::BufferingState:
    break;
    default:
    ;
    }
    }

    void MainWindow::tick(qint64 time)
    {
    QTime displayTime(0, (time / 60000) % 60, (time / 1000) % 60);

    timeLcd->display(displayTime.toString("mm:ss"));
    }


    void MainWindow::tableClicked(int row, int /* column */)
    {
    bool wasPlaying = mediaObject->state() == Phonon::PlayingState;

    // mediaObject->stop();
    // mediaObject->clearQueue();

    if (row >= sources.size())
    return;

    mediaObject->setCurrentSource(sources[row]);

    if (wasPlaying)
    mediaObject->play();
    else
    mediaObject->stop();
    }


    void MainWindow::sourceChanged(const Phonon::MediaSource &source)
    {
    musicTable->selectRow(sources.indexOf(source));
    timeLcd->display("00:00");
    }

    void MainWindow::metaStateChanged(Phonon::State newState, Phonon::State /* oldState */)
    {
    if (newState == Phonon::ErrorState)
    {
    QMessageBox::warning(this, tr("Error opening files"),
    metaInformationResolver->errorString());
    while (!sources.isEmpty() &&
    !(sources.takeLast() == metaInformationResolver->currentSource())) {} /* loop */;
    return;
    }

    if (newState != Phonon::StoppedState && newState != Phonon::PausedState)
    return;

    if (metaInformationResolver->currentSource().type() == Phonon::MediaSource::Invalid)
    return;

    QMap<QString, QString> metaData = metaInformationResolver->metaData();

    QString title = metaData.value("TITLE");
    if (title == "")
    title = metaInformationResolver->currentSource().fileName();

    QTableWidgetItem *titleItem = new QTableWidgetItem(title);
    titleItem->setFlags(titleItem->flags() ^ Qt::ItemIsEditable);
    QTableWidgetItem *artistItem = new QTableWidgetItem(metaData.value("ARTIST"));
    artistItem->setFlags(artistItem->flags() ^ Qt::ItemIsEditable);
    QTableWidgetItem *albumItem = new QTableWidgetItem(metaData.value("ALBUM"));
    albumItem->setFlags(albumItem->flags() ^ Qt::ItemIsEditable);
    QTableWidgetItem *yearItem = new QTableWidgetItem(metaData.value("DATE"));
    yearItem->setFlags(yearItem->flags() ^ Qt::ItemIsEditable);

    int currentRow = musicTable->rowCount();
    musicTable->insertRow(currentRow);
    musicTable->setItem(currentRow, 0, titleItem);
    musicTable->setItem(currentRow, 1, artistItem);
    musicTable->setItem(currentRow, 2, albumItem);
    musicTable->setItem(currentRow, 3, yearItem);

    if (musicTable->selectedItems().isEmpty())
    {
    musicTable->selectRow(0);
    mediaObject->setCurrentSource(metaInformationResolver->currentSource());
    }

    Phonon::MediaSource source = metaInformationResolver->currentSource();
    int index = sources.indexOf(metaInformationResolver->currentSource()) + 1;
    if (sources.size() > index)
    {
    metaInformationResolver->setCurrentSource(sources.at(index));
    }
    else
    {
    musicTable->resizeColumnsToContents();
    if (musicTable->columnWidth(0) > 300)
    musicTable->setColumnWidth(0, 300);
    }
    }

    void MainWindow::aboutToFinish()
    {
    int index = sources.indexOf(mediaObject->currentSource()) + 1;
    if (sources.size() > index)
    {
    mediaObject->enqueue(sources.at(index));
    }
    }


    void MainWindow::setupActions()
    {
    playAction = new QAction(style()->standardIcon(QStyle::SP_MediaPlay), tr("Play"), this);
    playAction->setShortcut(tr("Ctrl+P"));
    playAction->setDisabled(true);
    pauseAction = new QAction(style()->standardIcon(QStyle::SP_MediaPause), tr("Pause"), this);
    pauseAction->setShortcut(tr("Ctrl+A"));
    pauseAction->setDisabled(true);
    stopAction = new QAction(style()->standardIcon(QStyle::SP_MediaStop), tr("Stop"), this);
    stopAction->setShortcut(tr("Ctrl+S"));
    stopAction->setDisabled(true);

    connect(playAction, SIGNAL(triggered()), mediaObject, SLOT(play()));
    connect(pauseAction, SIGNAL(triggered()), mediaObject, SLOT(pause()) );
    connect(stopAction, SIGNAL(triggered()), mediaObject, SLOT(stop()));

    }


    /*
    void MainWindow::setupMenus()
    {
    QMenu *fileMenu = menuBar()->addMenu(tr("&File"));
    fileMenu->addAction(addFilesAction);
    fileMenu->addSeparator();
    fileMenu->addAction(exitAction);

    QMenu *aboutMenu = menuBar()->addMenu(tr("&Help"));
    aboutMenu->addAction(aboutAction);
    aboutMenu->addAction(aboutQtAction);
    }
    */
    void MainWindow::setupUi()
    {
    QToolBar *bar = new QToolBar;

    bar->addAction(playAction);
    bar->addAction(pauseAction);
    bar->addAction(stopAction);

    seekSlider = new Phonon::SeekSlider(this);
    seekSlider->setMediaObject(mediaObject);

    volumeSlider = new Phonon::VolumeSlider(this);
    volumeSlider->setAudioOutput(audioOutput);
    volumeSlider->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);

    QLabel *volumeLabel = new QLabel;
    volumeLabel->setPixmap(QPixmap("images/volume.png"));

    QPalette palette;
    palette.setBrush(QPalette::Light, Qt::darkGray);

    timeLcd = new QLCDNumber;
    timeLcd->setPalette(palette);

    QStringList headers;
    headers <<tr("Title") <<tr("Artist") <<tr("Album") ;

    musicTable = new QTableWidget(0, 3);
    musicTable->setHorizontalHeaderLabels(headers);
    musicTable->setSelectionMode(QAbstractItemView::SingleSelecti on);
    musicTable->setSelectionBehavior(QAbstractItemView::SelectRow s);
    connect(musicTable, SIGNAL(cellPressed(int,int)),this, SLOT(tableClicked(int,int)));


    QPushButton *add= new QPushButton("add");
    connect(add,SIGNAL(clicked()),this,SLOT(addFiles() ));


    QHBoxLayout *seekerLayout = new QHBoxLayout;
    seekerLayout->addWidget(seekSlider);
    seekerLayout->addWidget(timeLcd);

    QHBoxLayout *playbackLayout = new QHBoxLayout;
    playbackLayout->addWidget(bar);
    playbackLayout->addWidget(add);
    playbackLayout->addWidget(volumeLabel);
    playbackLayout->addWidget(volumeSlider);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(musicTable);
    mainLayout->addLayout(seekerLayout);


    Added after 53 minutes:


    QWidget *widget = new QWidget;
    widget->setLayout(mainLayout);

    setCentralWidget(widget);

    }
    Last edited by dibyendu; 15th February 2011 at 11:41.

  2. #2
    Join Date
    Feb 2011
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: can't get o/p by playing mp3 file in Phonon

    [QUOTE=dibyendu;178102]Hi guys,

    I have tried to make a simple mp3 player by getting the help from the given example in Qt manual. After some small modification in the code I can now able to play .mp3 file means its showing that it's playing but cant get the O/P sound. I am making this for NOKIA smart phone.

    This is my 1st assignment. Need some help,plz.

Similar Threads

  1. Playing file with Phonon while stil downloading it.
    By alexandernst in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2011, 11:25
  2. Playing Ogg Vorbis with Qt Phonon on Symbian^1
    By freemind in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th September 2010, 14:11
  3. Playing only some channels with Phonon
    By jfrousval in forum Qt Programming
    Replies: 0
    Last Post: 10th March 2009, 13:30
  4. Playing a stream using phonon module
    By ram136682 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 28th August 2008, 14:42
  5. Phonon video not playing
    By SidGBF in forum Qt Programming
    Replies: 2
    Last Post: 11th July 2008, 15:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.